TypeScript Version: 3.4.0-dev.20190326
Search Terms:
scoped package local definition
Code
tsconfig.json
{
"compilerOptions": {
"moduleResolution": "node",
"noEmit": true,
"typeRoots": [ "./node_modules/@types", "./types" ]
}
}
source/index.ts
import Foo from '@scope/scoped'
new Foo()
types/scope__scoped/index.d.ts
declare class Foo { }
export default Foo;
- npm install typescript@next
- npx tsc
- --> Notice error:
source/index.ts:1:17 - error TS2307: Cannot find module '@scope/scoped'.
1 import Foo from '@scope/scoped'
~~~~~~~~~~~~~~~
Found 1 error.
- move
types/scope__scoped/index.d.ts to node_modules/@types/scope__scoped/index.d.ts
- npx tsc
- --> Notice no error.
- move
node_modules/@types/scope__scoped/index.d.ts to types/@scope/scoped/index.d.ts
- --> Notice different error:
error TS2688: Cannot find type definition file for '@scope'.
error TS2688: Cannot find type definition file for 'scope__scoped'.
Found 2 errors.
Expected behavior:
A module located in any directory referenced by typeRoots compiler option behaves the same, whether it is in node_modules/@types or some other type root.
Actual behavior:
When the module is in node_modules/@types it can be resolved via the magic rename of scope__scoped to @scope/scoped. When the module is in any other typeRoot directory, it cannot be located.
Playground Link:
Related Issues:
Directly related to #15204 (but that is locked).
Related to #23999 as well.
I am aware that I can resolve this specific issue by wrapping my definition file in a declare module "@scope/scoped" { ... }. This issue is more for tracking the fact that there is a discrepancy that is unexpected from a user's point of view. I was writing this definition in preparation for uploading to DefinitelyTyped, and to get started I copied an existing scoped module from DefinitelyTyped into my project and ran into this problem (which took me quite a while to troubleshoot/isolate since I didn't expect node_modules/@types to be special/magic).
TypeScript Version: 3.4.0-dev.20190326
Search Terms:
scoped package local definition
Code
tsconfig.json{ "compilerOptions": { "moduleResolution": "node", "noEmit": true, "typeRoots": [ "./node_modules/@types", "./types" ] } }source/index.tstypes/scope__scoped/index.d.tstypes/scope__scoped/index.d.tstonode_modules/@types/scope__scoped/index.d.tsnode_modules/@types/scope__scoped/index.d.tstotypes/@scope/scoped/index.d.tsExpected behavior:
A module located in any directory referenced by
typeRootscompiler option behaves the same, whether it is innode_modules/@typesor some other type root.Actual behavior:
When the module is in
node_modules/@typesit can be resolved via the magic rename ofscope__scopedto@scope/scoped. When the module is in any other typeRoot directory, it cannot be located.Playground Link:
Related Issues:
Directly related to #15204 (but that is locked).
Related to #23999 as well.
I am aware that I can resolve this specific issue by wrapping my definition file in a
declare module "@scope/scoped" { ... }. This issue is more for tracking the fact that there is a discrepancy that is unexpected from a user's point of view. I was writing this definition in preparation for uploading to DefinitelyTyped, and to get started I copied an existing scoped module from DefinitelyTyped into my project and ran into this problem (which took me quite a while to troubleshoot/isolate since I didn't expectnode_modules/@typesto be special/magic).