Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requires declaration #184

Closed
dakom opened this issue Jul 13, 2017 · 17 comments
Closed

Requires declaration #184

dakom opened this issue Jul 13, 2017 · 17 comments

Comments

@dakom
Copy link

dakom commented Jul 13, 2017

Using the R typings requires adding a declare const R; at the top of the file, or importing R

Is this expected behavior? I don't encounter that with other packages which I need typings for but only import at runtime via cdn (e.g. PIXI)

@KiaraGrouwstra
Copy link
Member

To my knowledge import syntax (as opposed to just require) is idiomatic in TypeScript, yeah.

Looks like they're doing the following:

declare namespace PIXI {
  ...
}

declare namespace pixi {
  export const gl: typeof PIXI.glCore;
}
 
//tslint:disable-next-line:no-single-declare-module
declare module "pixi.js" {
  export = PIXI;
}

We're doing this:

declare var R: R.Static;
declare namespace R {
  ...
}
export = R;

I'm not sure exactly how you're using these typings or TS if not using imports -- I'm trying to figure out whether the behavior you're looking for is caused by their pixi namespace or by their module declaration -- probably the latter.

It's hard for me to verify my guess as I don't know how to reproduce your use-case. Would it be possible for you to try adding such a module declaration in your local ramda typings copy to verify what might fix this for you? I apologize I'm not too knowledgeable in this area.

@ikatyang
Copy link
Member

It seems the export as namespace R is missing in this repo, which marks the definition as UMD compatible.

@KiaraGrouwstra
Copy link
Member

Thanks, I tried adding that now. I'm guessing this means the functions would need to be declared/exported from the top level as well though?

@ikatyang
Copy link
Member

export as namespace R means that module.exports is named R in UMD, all you have to do is just adding an export as namespace R, since export = R is already done all things. :)

@KiaraGrouwstra
Copy link
Member

Great! If @dakom can confirm this as working I guess we're good then. :)

@dakom
Copy link
Author

dakom commented Jul 14, 2017

hmm with latest commit I'm still getting 'R' refers to a UMD global

@ikatyang
Copy link
Member

@dakom can you share some steps to reproduce?

@dakom
Copy link
Author

dakom commented Jul 14, 2017

From this work-in-progress repo: sodium-typescript-playground

If you comment out the declare const R, then you get the error

(to run the repo, after npm install it's npm run dev to spin up a local dev webpack server etc.)

@dakom
Copy link
Author

dakom commented Jul 14, 2017

OK, seems it's more of a general typescript issue: microsoft/TypeScript#10178

@ikatyang
Copy link
Member

ikatyang commented Jul 14, 2017

UMD is considered no import/exports.
module is considered import/exports.

In this case, R is considered global variable, so that you have to handle it yourself.

(tsconfig.json)

{
  "include": [
    "../src/**/*.ts",
    "../types/*" // <----
  ],
}

(./types/ramda.d.ts)

import * as _R from 'ramda';
declare global {
    const R: typeof _R; // <-- set R as global variable
}

@dakom As you mentioned, it currently not considered UMD as global variable, we have nothing to do for this issue, since we can't declare R as global variable, there is no R global variable if you're in a module.

@dakom
Copy link
Author

dakom commented Jul 14, 2017

Yep - your solution fixed it, thanks :-D

@dakom
Copy link
Author

dakom commented Jul 14, 2017

fwiw, VSCode is acting strange with it... compiles and completes fine, but VSCode still hilights the usage with an error/warning :\

@KiaraGrouwstra
Copy link
Member

might VSCode not be using the same TS version? That's one possibility, though I think I've otherwise seen discrepancies as well... pretty weird.

@ikatyang
Copy link
Member

ikatyang commented Jul 14, 2017

@dakom you have to put your tsconfig.json in the root folder, otherwise it'll just use the default settings. Or setup some config for that in vscode? I don't know if there is the option.

EDIT: There's probably no option for that.

@dakom
Copy link
Author

dakom commented Jul 14, 2017

ugh... now I tried it in a different project and got more errors again. I think I'm going to forget about trying to use it as a global, adding a declare or import at the top of each file isn't the end of the world

@KiaraGrouwstra
Copy link
Member

@dakom yeah, I do wish we had global imports as well.
I was doing Angular, and ng-cli did something for adding globals, though that'd also still just error within the actual files...
If you do find a solution I'd be pretty interested as well. I agree the boilerplate is annoying.

@KiaraGrouwstra
Copy link
Member

Closing as not having ways to solve this makes it sorta non-actionable; if we do find a way then let's retry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants