TypeScript: Duplicate identifier 'IteratorResult'
I'm trying to compile via tsc
--which I've installed globally--and I'm getting an error:
~/AppData/Roaming/nvm/v11.15.0/node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6 - error TS2300: Duplicate identifier 'IteratorResult'.
41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
~~~~~~~~~~~~~~
node_modules/@types/node/index.d.ts:170:11
170 interface IteratorResult<T> { }
~~~~~~~~~~~~~~
'IteratorResult' was also declared here.
node_modules/@types/node/index.d.ts:170:11 - error TS2300: Duplicate identifier 'IteratorResult'.
170 interface IteratorResult<T> { }
~~~~~~~~~~~~~~
~/AppData/Roaming/nvm/v11.15.0/node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6
41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
~~~~~~~~~~~~~~
'IteratorResult' was also declared here.
Found 2 errors.
I have @types/node
version 10.1.0 installed. (@latest
has its own issues...)
tsconfig.json
{
"compilerOptions": {
"target": "es2018",
"moduleResolution": "node",
"module": "commonjs",
"jsx": "react",
"lib": [
"dom",
"es2018",
"dom.iterable",
"scripthost"
],
"typeRoots": [
"./node_modules/@types",
"./types"
],
"types": [],
"alwaysStrict": true,
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"sourceMap": true,
"outDir": "dist"
},
"files": [
"app/index.tsx"
],
"include": [
"app/**/*.ts",
"app/**/*.tsx",
"test/**/*.ts",
"test/**/*.tsx",
"node_modules/@types/**/*.d.ts",
"./types/**/*.d.ts"
],
"exclude": [
"dist"
]
}
If I uninstall typescript
globally and run npx tsc
it works, but there should be nothing wrong with installing and running typescript
globally. After all, that's the whole point of installing things globally.
In the meantime I have a workaround which is to just alias tsc (I'm using git bash in Windows).
alias tsc="path/to/project/node_modules/.bin/tsc.cmd"
Solution 1:
Found an issue on GitHub - https://github.com/microsoft/TypeScript/issues/32333 which was related. @rbuckton suggested upgrading @types/node
. It worked for me.
Solution 2:
I was getting the is error in my angular 8 App and couldn't resolve the issue after trying all suggestions made here including the accepted answer. I had to look at a previous angular 6 App that compiled without errors and realised that I could just skip the library check by including
"skipLibCheck": true
to the tsconfig.json file. With the fact that my App is running well without problems, I decided to take this approach. Here is the complete configuartion of my tsconfig.json file
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
],
"skipLibCheck": true
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
There were no more errors after this configuration. Note: That doesn't mean that the problem is solved but at least it allowed me to skip the bug that was causing the error. Due to the fact that my App is running as expected I just considered this error irrelevant at this moment.