我正在尝试新的"异步发生器/用于舞蹈:
error TS2504: Type must have a '[Symbol.asyncIterator]()' method that returns an async iterator.
我收到以下错误:
foreach()
我该如何工作?
什么是正确的"类型安全"返回类型为"它"功能
答案 0 :(得分:1)
您的代码实际上很好,但异步迭代器当前是ES.next功能,因此您必须指定此ES版本,例如将以下属性放在tsconfig.json中:
"target": "esnext"
<强> Transpiling 强>
如果要转换为以前的ES版本,TypeScript将不会自动为您处理此问题。相反,您可以使用core-js
:
首先,安装core-js:npm install core-js
然后,编辑tsconfig.json:
{
compilerOptions: {
"lib": [
"es2015",
"esnext.asynciterable"
],
"target": "es2015"
[...]
},
[...]
}
然后,将core-js导入源文件:
import 'core-js/shim';
答案 1 :(得分:0)
您需要填充asyncIterator
符号。
(Symbol as any).asyncIterator = Symbol.asyncIterator || Symbol.for('Symbol.asyncIterator');