question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tsc not respecting types/typeRoots config for imported modules

See original GitHub issue

Bug Report

Codebase is pulling in type declarations from the parent ../node_modules/@types/ folder, despite attempting to restrict the types and typeRoots to ignore it.

client/
  node_modules/@types/foo/ # server shouldn't ever see this!
  tsconfig.json
  
  server/
    node_modules/foo/ # server should see this as a module with no type declarations
    tsconfig.json

🔎 Search Terms

  • Stop typescript from including types from parent node_modules
  • Prevent typeRoots from using parent node_modules

Related Issues:

  • How to block typescript 2.0 from walking up all parent directories while resolving modules? #13992
  • Declarations are unexpectedly used from node_modules in parent directory #30124

The issue is very similar to the ones described above, but neither were resolved beyond saying that unwanted types could be excluded with "types": []. This only seems to work for excluding global types (e.g. @types/node) as far as I can tell, there is no way to prevent typescript pulling in types for a module that is explicitly imported. See repro for more context.

🕗 Version & Regression Information

We are seeing this problem with typescript@3.9.9, but I have created a reproduction that also confirms it on typescript@latest and typescript@next too.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about excluding types and type roots.

💻 Code

Reproduced in repo with CI demonstrating incorrect behaviour here: https://github.com/danprince/typeroots-bug-repro

🙁 Actual behavior

The server codebase ends up including ../node_modules/@types/foo/index.d.ts as part of the compilation (through an import foo from "foo") even with "typeRoots": ["./node_modules/@types"] and "types": [].

Our current workaround is to remove the parent node_modules as part of our CI step.

We could also work around this by shadowing the declarations in the local node_modules/@types dir, or using paths hack to point typescript towards a non-existent version of the module, but it shouldn’t be that hard.

All I want is to tell tsc is to never ever look in ../node_modules/@types.

🙂 Expected behavior

Never look in ../node_modules/@types for anything. I know that conflicts with the "moduleResolution": "node" to some degree, but it should be possible to configure that as far as type declarations are concerned.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kylecordescommented, Jun 9, 2022

It appears to me (I don’t have a convenient isolated test case) that the problem from multiple past issues still exists also: TypeScript looks up to ../node_modules/@types/ for global types, no matter what combination of typeRoots, types, etc. is configured. It’s a significant problem with manually or automatically hoisted dependencies in a monorepo.

1reaction
danprincecommented, Mar 7, 2021

Understood. Thanks!

Our use case here is probably too specific to ever be worth changing the module resolution behaviour, but I could definitely see it being worth an additional note in the docs.

I had been thinking that the general concept of the node_modules/@types directory was fully configurable with typeRoots, rather than being a distinct part of module resolution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
Common TypeScript module problems and how to solve them
In the following section, we will review how to resolve this issue. Solution 2: Locate the module and resolve imports. Using the configuration ......
Read more >
tsconfig.js exclude property not working when import modules ...
I am going nuts currently why my typescript compilation (tsc) is always trying to compile node_modules files even when I ...
Read more >
How To Use Modules in TypeScript | DigitalOcean
ts . Now that you've imported the vectors, you can re-export them in a single module with the following highlighted syntax: vector_project/src ...
Read more >
Understanding TypeScript's “Compilation Process” & the ...
However, the tsc compiler (short for TypeScript compiler) needs a JSON ... Such imports would not get excluded despite the exclude option ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found