-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Better handling of untyped node module imports #11106
Comments
Is there anyway we can solve this in a non npm specific manner? I like the concept but tying it to |
Also, please, please, please improve documentation on "ambient modules". I've read the section seven times now, I still have no idea what the ambient modules are, how to define them and where to put the specs so that they are picked up by TS. Espec It would be awesome, if there was a small section "you want to import module X. you create a file X.d.ts here. Then you do this and that and this" |
I followed a chain of four closed tickets to get here. Glad to see the issue was addressed. Could someone please point to docs for this feature? How do I use it? For my sake, and for everyone else who may end up here. :) |
This is part of Now having an import like if you use hope that helps. |
That helps a lot, thank you. It seems like for now (v2.0.10) we can also use |
yes. that is Shorthand ambient module declarations |
Does this mean that if you've got a node module that doesn't use |
@radicaled We check for that too. |
And what about relatively referenced JS modules? In this case, Blah ends up as "type Blah" and TS tries its best to infer the shape of the exported object from blah.js, which in my case, was wrong (it only identified some of the properties on the object - others get errors of property key does not exist on value of type Blah). Hence, Blah is NOT of type any! I have to do (Blah as any).method() to get it to work. I would think many would be hitting this issue mixing TS and JS. We're using an ejected create-react-app with TS support added to the webpack config via ts-loader. I expected it to just work without fuss, sadly not the case. Longer explanation with code sample: http://stackoverflow.com/questions/43954320/es6-import-of-relative-path-js-module-type-not-fully-inferred-how-to-declare |
Hi @lvpro, if the file does not come from
declare const blah: any;
export default blah;
/** @type {Function} */
this.get = this.post = this.put = this.delete = undefined; And you will now get these methods in autocomplete, although you won't get signature help. |
The first suggestion would go in its own file, not in a |
Does quick fix 1 only work if blah.d.ts is in the same location as blah.js? It does work, but my team may not want .d.ts files polluting their code directories (we're slowly using Typescript on new areas of the system, 95% of it is an ES6 codebase). |
If you import the module with its name (i.e. if it is a relative import, then you need to put the .d.ts next to the .js |
So in instances where one imports relatively (this would be especially common in a create-react-app codebase), it seems these are the options;
I wish we could opt-in to having the same behavior as modules within node_modules, wherein if a typing is not found, the module could default to type any. I saw someone suggest |
One thing you could do is use an untyped re-export: // ts-module.ts
import foo from "./js-module";
export default foo as any; Then import from |
Consider importing an untyped module, eg. exceljs:
This results in an error that the module exceljs is not found. There is at least two ways to fix this:
allowJs: true
andmaxNodeModulesJsDepth: 1
I think it would be better if instead we update module resolution semantics such that when we find an appropriate package.json without a typings entry, we store it away. Then if module resolution fails (ie. we also can't find an @types/exceljs), we go back and import the untyped package.json as a JS import. Thoughts?
You might argue that this should only be allowed with --allowJs, but it might also make sense to just always use this behavior when using node resolution rules as you may still not want to allow JS in your own project while still allowing your dependencies to be authored in JS without typings.
The text was updated successfully, but these errors were encountered: