-
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
Cannot import npm package with definition defined in typeroots #11137
Comments
According to the handbook I think you have to use ambient definition. I have a similar problem in #11136 , and the comments of the answer of this question help a lot |
@unhealthy I think the handbook has been updated for 2.0. If you look at the templates in the handbook for how you are supposed to write definition files, none of them uses ambient modules. Instead they say you should name the file index.d.ts and place it in a folder named like the library. To me this seem consistent with how the compiler option |
Also if I create a directory called |
In the chapter Module Resolution,
So according to this document, if you use the default resolve strategy, which is Node strategy, typescript will always resolve the non-relative module in a path like I have read the templates also and followed it to write declaration, and I met the same error as you. |
@unhealthy Aha, I see now what you mean, I was looking in the declaration files section of the handbook. In the module resolution section there is no mention of @types, but in reality that is also looked at while resolving a module in 2.0 (You can see that with the --traceResolution compiler option). So I suspect that section is not yet updated for 2.0. Also if I were to develop typings for a third-party package with the intention of later publishing them onto npm @types, they should not be ambient as you noted. Where should I put that definition file while working on it (before publishing to npm)? Putting it under node_modules/@types while working on it makes no sense to me so. |
Yes you're right. I have used |
typeRoots should be used in module resolution as a fall back option if we did not find the module in any other place. |
A good workaround as mentioned in #11329 is to have a folder, eg.
|
If you want to import things, you should |
just to fill in some details for future searches. the resolution for module imports follows that of node (using
in |
TypeScript Version: 2.0.3
I am trying to make a definition file for an existing npm package without using ambient definition such as
declare module 'foo' {...}
. In the example below I am trying to create typings for the npm package fibonacci.Code
package.json
:tsconfig.json
:test.ts
:types/fibonacci/index.d.ts
:Compile to see the error:
Expected behavior:
My expectation would be that
tsc
should pick up the definitions intypes/fibonacci/index.d.ts
because of"typeRoots": ["./types"]
intsconfig.json
.Actual behavior:
I get
error TS2307: Cannot find module 'fibonacci'.
. Using--traceResolution
I can see that tsc is finding the filetypes/fibonacci/index.d.ts
. If I move the filetypes/fibonacci/index.d.ts
intonode_modules/fibonacci/index.d.ts
it works as expected.The text was updated successfully, but these errors were encountered: