Skip to content

Rules for scoped packages with local definitions differs from @types definitions. #30599

@MicahZoltu

Description

@MicahZoltu

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;
  1. npm install typescript@next
  2. npx tsc
  3. --> Notice error:
    source/index.ts:1:17 - error TS2307: Cannot find module '@scope/scoped'.
    1 import Foo from '@scope/scoped'
                      ~~~~~~~~~~~~~~~
    Found 1 error.
    
  4. move types/scope__scoped/index.d.ts to node_modules/@types/scope__scoped/index.d.ts
  5. npx tsc
  6. --> Notice no error.
  7. move node_modules/@types/scope__scoped/index.d.ts to types/@scope/scoped/index.d.ts
  8. --> 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions