Skip to content

Module resolution behaves differently when typeRoots is specifiedΒ #59329

Closed as not planned
@wjhsf

Description

@wjhsf

πŸ”Ž Search Terms

typeRoots, module resolution, moduleResolution, bundler

πŸ•— Version & Regression Information

  • This changed between versions 5.0.4 and 5.1.3 (variants with Preserve since 5.4.2)

⏯ Playground Link

No response

πŸ’» Code

No response

πŸ™ Actual behavior

Setting typeRoots to ./node_modules/@types changes how modules are resolved when using the following compiler options:

  • moduleResolution: Node16, module: Node16
  • moduleResolution: Node10, module: Preserve
  • moduleResolution: Bundler, module: ES2015 | ES2020 | ES2022 | ESNext | Preserve

πŸ™‚ Expected behavior

Setting typeRoots to ./node_modules/@types should not change how modules are resolved.

Additional information about the issue

Clone this gist and execute setup.sh to see a minimal reproduction. It sets up a package with various export types, and tries to compile a file with corresponding imports using various module/moduleResolution combinations. Not all imports are expected to work for all combinations, but the errors are expected to be the same regardless of whether typeRoots is specified. If the errors are different, the script will log the errors along with the compiler options used. (No output from the script means there were no mismatched errors.)

Sample output:

{ module: 'Preserve', moduleResolution: 'Bundler' }
=== no typeRoots ===
index.ts(1,22): error TS2307: Cannot find module 'pkg' or its corresponding type declarations.
index.ts(2,30): error TS2307: Cannot find module 'pkg/index' or its corresponding type declarations.
index.ts(3,32): error TS2307: Cannot find module 'pkg/index.js' or its corresponding type declarations.
index.ts(4,32): error TS2307: Cannot find module 'pkg/internal' or its corresponding type declarations.
index.ts(5,34): error TS2307: Cannot find module 'pkg/internal.js' or its corresponding type declarations.
index.ts(6,32): error TS2307: Cannot find module 'pkg/external' or its corresponding type declarations.
index.ts(7,34): error TS2307: Cannot find module 'pkg/external.js' or its corresponding type declarations.
index.ts(8,31): error TS2307: Cannot find module 'pkg/exports' or its corresponding type declarations.

== with typeRoots ==
index.ts(8,31): error TS2307: Cannot find module 'pkg/exports' or its corresponding type declarations.

{ module: 'Preserve', moduleResolution: 'Node10' }
=== no typeRoots ===
index.ts(1,22): error TS2307: Cannot find module 'pkg' or its corresponding type declarations.
index.ts(2,30): error TS2307: Cannot find module 'pkg/index' or its corresponding type declarations.
index.ts(3,32): error TS2307: Cannot find module 'pkg/index.js' or its corresponding type declarations.
index.ts(4,32): error TS2307: Cannot find module 'pkg/internal' or its corresponding type declarations.
index.ts(5,34): error TS2307: Cannot find module 'pkg/internal.js' or its corresponding type declarations.
index.ts(6,32): error TS2307: Cannot find module 'pkg/external' or its corresponding type declarations.
index.ts(7,34): error TS2307: Cannot find module 'pkg/external.js' or its corresponding type declarations.
index.ts(8,31): error TS2307: Cannot find module 'pkg/exports' or its corresponding type declarations.

== with typeRoots ==
index.ts(8,31): error TS2307: Cannot find module 'pkg/exports' or its corresponding type declarations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions