typescript 打字稿:重复标识符“IteratorResult”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57331779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript: Duplicate identifier 'IteratorResult'
提问by dx_over_dt
I'm trying to compile via tsc
--which I've installed globally--and I'm getting an error:
我正在尝试通过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.
I have @types/node
version 10.1.0 installed. (@latest
has its own issues...)
我@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"
]
}
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.
如果我typescript
全局卸载并运行npx tsc
它可以工作,但是typescript
全局安装和运行应该没有问题。毕竟,这就是在全球范围内安装东西的全部意义所在。
In the meantime I have a workaround which is to just alias tsc (I'm using git bash in Windows).
与此同时,我有一个解决方法,就是给 tsc 取别名(我在 Windows 中使用 git bash)。
alias tsc="path/to/project/node_modules/.bin/tsc.cmd"
回答by OldMan
Found an issue on GitHub - https://github.com/microsoft/TypeScript/issues/32333which was related. @rbuckton suggested upgrading @types/node
. It worked for me.
在 GitHub 上发现了一个相关的问题 - https://github.com/microsoft/TypeScript/issues/32333。@rbuckton 建议升级@types/node
。它对我有用。
回答by danielv
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).
您通常不需要明确包含 *.d.ts 文件。并且可能永远不会声明来自其他库(或节点类型)的文件。
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.
tsconfig
的“排除”部分"node_modules"
默认排除所有内容(除其他外)。当您添加时,"node_modules/@types/**/*.d.ts"
您会覆盖该 exclude 和 tsc 尝试包含它们,但这些类型已经声明。
Check Typescript docs on tsconfig.json, it explains the "typeRoots", "files"and "include"/"exclude"config options in detail.
检查打字稿文档tsconfig.json,它解释了“typeRoots”,“文件”和“包括”/ “排除”在详细的配置选项。
回答by omostan
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
我在我的 angular 8 应用程序中遇到错误,并且在尝试了这里提出的所有建议(包括接受的答案)后无法解决问题。我不得不查看以前编译没有错误的 angular 6 应用程序,并意识到我可以通过包含跳过库检查
"skipLibCheck": true
“skipLibCheck”:真
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
到 tsconfig.json 文件。鉴于我的应用程序运行良好,没有问题,我决定采用这种方法。这是我的 tsconfig.json 文件的完整配置
{ "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.
此配置后不再有错误。 注意:这并不意味着问题已经解决,但至少它允许我跳过导致错误的错误。由于我的应用程序正在按预期运行这一事实,我认为此时此错误无关紧要。
回答by bersling
For me it turned out I had a node_modules
folder in a parent directory project, something similar to this:
对我来说,原来我node_modules
在父目录项目中有一个文件夹,类似于:
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.
由于安装node_modules
了旧版本@types/node
,问题发生了。然而,在我的情况下,解决方案不是更新@types/node
而是删除它们,node_modules
因为我一开始没有使用它们。
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:
如果您确实需要node_modules
在具有不同类型的父目录中使用 a 并且这就是您想要的方式,那么您可以具体指定typeRoots
:
{
"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
这样,node_modules
就不会扫描父对象的类型。否则,请在此处阅读: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.
默认情况下,所有可见的“@types”包都包含在您的编译中。任何封闭文件夹的 node_modules/@types 中的包都被认为是可见的;具体来说,这意味着 ./node_modules/@types/、../node_modules/@types/、../../node_modules/@types/ 等中的包。
回答by Spikolynn
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';