打字稿和迭代器:输入“IterableIterator<T>"不是数组类型 [英] TypeScript and Iterator: Type 'IterableIterator<T>' is not an array type
本文介绍了打字稿和迭代器:输入“IterableIterator<T>"不是数组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用 yield*
表达式 在 TypeScript 上,总是报错.
When I use the yield*
expression on TypeScript, it always gets an error.
类型 'IterableIterator' 不是数组类型.
Type 'IterableIterator' is not an array type.
如何在不使用 any
的情况下正确设置类型以避免错误?
How can I set the types correctly without using any
to avoid the errors?
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();
推荐答案
如果可能,将 tsconfig 中的目标更新为 es2015
(或更高版本)以解决错误,而无需启用下级迭代`:>
If possible update target to es2015
(or above) in the tsconfig to resolve the error without enabling down level iteration`:
{
"compilerOptions": {
"target": "es2015"
}
}
如果你的目标是 es5,请在 tsconfig 中显式启用 downLevelIteration
:
If you target es5, enable downLevelIteration
explicitly in the tsconfig:
{
"compilerOptions": {
"target": "es5",
"downlevelIteration": true
}
}
这篇关于打字稿和迭代器:输入“IterableIterator<T>"不是数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文