microsoft / microsoft/TypeScript

tsc not respecting types/typeRoots config for imported modules

Offen
#43,094 9 Kommentare 5 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten typeroots-bug-repro und seinen server- und client-tsconfig.json-Dateien und führe dann die gemeldete tsc-Kompilierung aus, um die Auflösung von parent node_modules/@types zu beobachten. Verfolge, wie moduleResolution mit typeRoots, types und einem expliziten Import von foo interagiert. Das Ziel ist erreicht, wenn eine unterstützte Konfiguration verhindert, dass die übergeordneten Deklarationen ausgewählt werden, und gleichzeitig das beabsichtigte Verhalten der Modulauflösung erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.