【发布时间】:2018-07-13 09:39:04
【问题描述】:
我正在尝试创建一个自定义迭代。
这是我的代码的简化示例:
class SortedArray {
*[Symbol.iterator]() {
yield 1;
yield 2;
yield 3;
return 4;
}
}
const testingIterables = new SortedArray();
for(let item of testingIterables as any) { // i have to cast it as any or it won't compile
console.log(item);
}
此代码将在 ES6 上正确运行,但使用 TypeScript 它将编译并且不打印可迭代值。
这是 TypeScript 中的错误还是我遗漏了什么?
谢谢
【问题讨论】:
-
除了马达拉的回答之外,还有一个使用--downlevelIteration flag的选项。
标签: typescript ecmascript-6 iterator iterable