TypeScript: Duplicate identifier 'IteratorResult'

后端 未结 10 1828
无人共我
无人共我 2020-12-15 01:59

I\'m trying to compile via tsc--which I\'ve installed globally--and I\'m getting an error:

~/AppData/R         


        
相关标签:
10条回答
  • 2020-12-15 02:38

    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.

    0 讨论(0)
  • 2020-12-15 02:38

    I found this thread after googling the error. My problem was that somehow I had an unneeded import which caused this:

    import { error } from 'protractor';
    
    0 讨论(0)
  • 2020-12-15 02:38

    I resolved this issue manually by commenting one of the interface "IteratorResult" declaration in node_modules/@types/node/index.d.ts file. Hope this will help.

    0 讨论(0)
  • 2020-12-15 02:40

    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.

    0 讨论(0)
  • 2020-12-15 02:41

    This is how I solved it:

    npm uninstall --save-dev webpack
    
    npm install --save-dev @angular-devkit/build-angular@latest
    
    0 讨论(0)
  • 2020-12-15 02:42

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

    This solved the issue. Check here

    0 讨论(0)
提交回复
热议问题