TypeScript 和 Iterator:类型 'IterableIterator<T>' 不是数组类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49218765/
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 and Iterator: Type 'IterableIterator<T>' is not an array type
提问by kechol
When I use the yield*
expressionon TypeScript, it always gets an error.
当我在 TypeScript 上使用yield*
表达式时,它总是出错。
Type 'IterableIterator' is not an array type.
类型 'IterableIterator' 不是数组类型。
How can I set the types correctly without using any
to avoid the errors?
如何在不使用的情况下正确设置类型any
以避免错误?
function* g1(): IterableIterator<number> {
yield 2;
yield 3;
yield 4;
}
function* g2(): IterableIterator<number> {
yield 1;
// ERROR: Type 'IterableIterator<number>' is not an array type.
yield* g1();
yield 5;
}
const iterator = g2();
回答by Titian Cernicova-Dragomir
If possible update target to es2015
(or above) in the tsconfig to resolve the error without enabling down level iteration`:
如果可能es2015
,将 tsconfig 中的目标更新为(或更高)以解决错误,而无需启用下级迭代`:
{
"compilerOptions": {
"target": "es2015"
}
}
If you target es5, enable downLevelIteration
explicitly in the tsconfig:
如果您的目标是 es5,请downLevelIteration
在 tsconfig 中显式启用:
{
"compilerOptions": {
"target": "es5",
"downlevelIteration": true
}
}
回答by Deepak
tsc demo.ts --lib "es6","dom" --downLevelIteration
Use this command for compilation. It will solve the issue. Adding these values in tsconfig.json will not solve the problem, if tsconfig.json is created with target:es5. updating tsconfig.json manually will not work. Use this command, just change the name of your .ts file.
使用此命令进行编译。它将解决问题。如果 tsconfig.json 是用 target:es5 创建的,在 tsconfig.json 中添加这些值不会解决问题。手动更新 tsconfig.json 将不起作用。使用此命令,只需更改 .ts 文件的名称。