-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Importing untyped JS modules #3019
Comments
Just doing |
You need to declare a |
The TypeScript compiler needs to know the shape of your import to ensure any uses of the import are correctly typed. consider this: import foo from "module";
foo.a = 1; // does foo has an export "a", and if so is it really a number The compiler can not just look into the .js file, cause the .js file does not have any type annotations and thus most inferences will degenerate to Instead, you need a different means to tell the compiler the shape of the module, its exports, and their types. that is the .d.ts as @tinganho mentioned earlier. There is a community repository of definition files (.d.ts) for popular libraries on http://definitelytyped.org/; If your library is not there, you can hand edit a .d.ts file for it following this wiki page or use a tool like closure-ts that is d.ts generator from JSDoc typings. |
I believe @jonrimmer and I both are wanting to know how to silence that error, perhaps with some kind of Given the example
|
This article brings great solutions to the problem: https://www.stevefenton.co.uk/2013/01/complex-typescript-definitions-made-easy/ |
I have been out of the |
In what sense is Typescript a mere superset if it forces me to create type files for all my pre-existing javascript modules? The easy interoperability is basically a lie. This is especially so for modules I have only minified versions of. |
Why does it "force" you? |
@mhegazy If you don't declare those types, it won't compile. Try using any third-party JS in a TypeScript project and you'll see how it goes. Most developers end up declaring everything as "any" in the typings file just to make TypeScript happy. |
There does not seem to be any coercion involved here. the compiler needs to know that a name or is just a typo. No requirement to add declaration files to all your existing code. as a matter of fact, an import to a module with no .d.ts file will just result in an You can add declarations as you go, depending on where your project is at:
|
Force in the sense of perfectly good node modules having problem if just imported in TS unless I do a type file or, as I understand it so far, if I include it directly in my index.html and perhaps do a declare var thing.. What I lost was simple module import in ES6 world from such modules. I miss it. This is in a webpack using project and more specifically an Angular2 one. Of course it is possible I am missing something simple such I am on week two of fighting with typescript for first time. |
This should not be the case. If you have a sample I would be happy to look at. |
@mhegazy The issue really arises when you use a Javascript package that requires use of "new" or some kind of constructor function. For instance, epub-js requires to create an instance like this:
TypeScript required me to create typings in order for this to work. A strict superset of Javascript with optional typings should be totally fine with the above code. |
So you will need to add, in just one location,
A super set means that your JS code is valid TS code. and that is the case, the typescript compiler will |
@mhegazy here is my code: when I use webpack to bundle my code ,I get an error: |
I am using TS 2.2 and i am not seeing this error. what version are you using? |
I'm still observing this behavior with TS 2.2.1 and VSCode import d from "adal-node"; tsc.exe: 'Could not find a declaration file for module 'adal-node'. 'e:/proj/node_modules/adal-node/lib/adal.js' implicitly has an 'any' type.' I see all linked bugs are closed. Is this expected to be working now? |
Yes. Please file a new issue and provide a self contained reproduction of the issue. |
thanks, filed #15031 |
i solved by just prepending //@ts-ignore right before the untyped module import for example (for font-awesome 5): //@ts-ignore fontawesome.library.add(regular); plus, it works with webpack :) |
God has nothing to do with this |
|
I have a SFC that implements
ts reports
Is there a way to override the default generics in React.Component? |
I have been trying out the 1.5 beta, but I am confused about importing modules from 3rd-party JS libraries. As I understand it, TS is intended as a superset of JS so if, for example, I have a CommonJS library called foo.js, then in my app.ts, I should be able to do:
And this should be compiled (depending on my compile options) into a CommonJS require statement, which can then be consumed by Node, Browserify, etc.
This works in regular JavaScript (as compiled by Babel), but it does not work in TypeScript. I get a compiler error saying "Cannot find external module 'foo'".
I understand that I can maybe write a .ts.d file that describes this library, but what if I don't want to do this? What if I just want to import a module and use it, as I would in regular JS?
The text was updated successfully, but these errors were encountered: