首页 > 解决方案 > TypeScript:重复标识符“IteratorResult”

问题描述

我正在尝试通过tsc- 我已在全球范围内安装 - 进行编译,但出现错误:

~/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.

@types/node安装了 10.1.0 版本。(@latest有自己的问题...)

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"
  ]
}

如果我全局卸载typescript并运行npx tsc它可以工作,但是全局安装和运行应该没有错typescript。毕竟,这就是在全球范围内安装东西的全部意义所在。

与此同时,我有一个解决方法,就是别名 tsc (我在 Windows 中使用 git bash )。

alias tsc="path/to/project/node_modules/.bin/tsc.cmd"

标签: typescripttypesimport

解决方案


在 GitHub 上发现了一个相关的问题 - https://github.com/microsoft/TypeScript/issues/32333。@rbuckton 建议升级@types/node。它对我有用。


推荐阅读