Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Duplicate identifier 'IteratorResult'

Found an issue on GitHub - https://github.com/microsoft/TypeScript/issues/32333 which was related. @rbuckton suggested upgrading @types/node. It worked for me.


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.


I suspect it is because your include section:

"include": [
    "app/**/*.ts",
    "app/**/*.tsx",
    "test/**/*.ts",
    "test/**/*.tsx",
    "node_modules/@types/**/*.d.ts",
    "./types/**/*.d.ts"
  ]

You usually don't need to explicitly include *.d.ts files. And probably never declaration files from other libraries (or node types).

tsconfig's "exclude" section excludes everything under "node_modules" by default (among other things). When you add "node_modules/@types/**/*.d.ts" you override that exclude and tsc tries to include them, but those types are already declared.

Check Typescript docs on tsconfig.json, it explains the "typeRoots", "files" and "include"/"exclude" config options in detail.


For me it turned out I had a node_modules folder in a parent directory project, something similar to this:

node_modules
my-project
- node_modules

Since the node_modules had an older version of @types/node installed, the problem happened. In my case the solution however wasn't to update @types/node but instead to remove those node_modules since I wasn't using them in the first place.

If you actually need to have a node_modules in a parent directory with different types and this is how you want it to be, then you can specify the typeRoots specifically:

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es6",
    "declaration": true,
    "outDir": "./dist",
    "typeRoots": ["./node_modules/@types/"]
  },
  "include": [
    "src/**/*"
  ]
}

That way, the parent node_modules aren't scanned for types. Otherwise they are, read here: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.


As @Muhammad bin Yusrat said in his comment, run npm i @types/node@latest (npm i @types/node doesn't work !!) if you have just updated angular to 9. That worked for me.

It also got rid of another ionic 5 console error after running ionic serve-> 'refused to load image 'http:localhost:8100/favicon.ico' because it violates the following Content Security Policy .....' (see below).

ionic 5 error after running ionic serve

Another 'IteratorResult' error was caused by "Spread Types" Error. See Typescript: Spread types may only be created from object types. Basically somewhere in your code you have used a spread operator like this return { id: doc.payload.id, ...doc.payload.data() }; and you have to change it to this return { id: doc.payload.id, ...doc.payload.data() as {} }; ie add as {}


Add "skipLibCheck": true in compilerOptions in tsconfig.json.

This solved the issue. Check here


I just removed types/node by running

sudo npm remove @types/node

and installed again by running the following command and it worked for me.

sudo npm i @types/node