Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block typescript 2.0 from walking up all parent directories while resolving modules?

Upgraded to Typescript 2.0 (2.1.6) and it started giving "Duplicate identifier" errors. After taking a closer look it turned out that Typescript started importing @types from all upper directories (essentially other projects).

What should be the configuration to let Typescript to ignore upper node_modules?

src
 └── node_modules << *** how to ignore it? ***
     └── @types

 └── my.app << *** how to build this folder and down only? ***
         └── node_modules
             └── @types

EDIT: Here is an example of error I'm getting:

typings/globals/mocha/index.d.ts(30,13): error TS2300: Duplicate identifier 'describe'. ../../../node_modules/@types/jasmine/index.d.ts(9,18): error TS2300: Duplicate identifier 'describe'.

listFiles: true shows @types/jasmine being imported from upper folder:

C:/src/<project>/<folder>/<my.app>/typings/globals/mocha/index.d.ts
C:/src/node_modules/@types/jasmine/index.d.ts

If I rename upper node_modules folder then build succeeds.

like image 646
ZakiMa Avatar asked Feb 10 '17 03:02

ZakiMa


2 Answers

The official documentation specifies that node_modules in current directory and all parents will be traversed unless you specify typeRoots.

So in theory, the answer should be this:

{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ]
  }
}

Since you still want to include the types from the current directory.

Unfortunately this doesn't seem to work correctly for me.

like image 137
peterjwest Avatar answered Sep 17 '22 16:09

peterjwest


In this helps anyone, setting "types": [] in my tsconfig.json worked for me. See this github comment.

https://github.com/Microsoft/TypeScript/issues/13992#issuecomment-279020210

like image 44
devinm Avatar answered Sep 18 '22 16:09

devinm