While trying to compile my typescript source code, I see that the compiler is also trying to compile the types under my node_modules folder . I am using typescript 2.6.1 and my tsconfig file is as below
{
"compilerOptions": {
"allowSyntheticDefaultImports":true,
"outDir": "./dist",
"mapRoot": "./dist",
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"sourceRoot": "./source",
"removeComments": false
},
"exclude": [
"node_modules",
"test"
],
"include": [
"source/*.ts"
]
}
When I run the following comand "tsc -w -p tsconfig.json" I get the following error
node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.
After reading this document, I got the answer https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
under section @types, typeRoots and types
They have mentioned
Specify "types": [] to disable automatic inclusion of @types packages.
The updated tsconfig.json is as follows
{
"compilerOptions": {
"allowSyntheticDefaultImports":true,
"outDir": "./dist",
"mapRoot": "./dist",
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"sourceRoot": "./source",
"removeComments": false,
"types": []
},
"exclude": [
"node_modules",
"test"
],
"include": [
"source/*.ts"
]
}
Why?
This can also occur when TypeScript versions don't correspond.
E.g. You are running 2.9.x in your global npm cache, and you have TypeScript 3.5.x installed locally in your project node_modules.
Test
You can test this by running "npx tsc", but this will only work if you have TypeScript saved as a dependency, you've run "npm install", and you have npm 5.2.x or greater.
Otherwise you can check your local TypeScript version with "npm list typescript", and your global TypeScript version with "npm list typscript -g"
Solution
If in the case of the "npx tsc" approach, it passes, or, in the second case of the versions being mismatched, you should only need to ensure that you align your local TypeScript version with your global TypeScript version, or vice versa.
Other Notes:
"npx" will run the command using the Node package from your project's local "node_modules" folder.
Check out Node Version Manager if you don't know about it, it's great for switching between Node versions on the fly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With