TypeScript 2.8.3 Type 必须有一个返回迭代器的 Symbol.iterator 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50234481/
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 2.8.3 Type must have a Symbol.iterator method that returns an iterator
提问by Tomá? Hübelbauer
I am running into an error that says Type must have a '[Symbol.iterator]()
' method that returns an iterator.. It hopes on the demarcated line:
我遇到了一个错误,说Type 必须有一个[Symbol.iterator]()
返回迭代器的 ' ' 方法。. 它希望在分界线上:
class Test {
private async do() {
const done = [...(await this.test())]; // Here's the error
}
private async *test(): AsyncIterableIterator<string> {
return;
}
}
I have found a few issues in the TypeScript GitHub repository, but none seem to have helped. They all suggest adding new entries to lib
. I am using es6
target and have added esnext
, dom
and es2018
. This has had zero effect on the error.
我在 TypeScript GitHub 存储库中发现了一些问题,但似乎都没有帮助。他们都建议将新条目添加到lib
. 我正在使用es6
目标并添加了esnext
,dom
和es2018
。这对错误的影响为零。
Do I miss more lib
entries (which I doubt as the ones I am using are the catchall ones with everything in) or is the code I am using invalid?
我是否错过了更多lib
条目(我怀疑这是因为我使用的是包含所有内容的所有条目)还是我使用的代码无效?
采纳答案by jcalz
As I commented above, the spread operator is unfortunately currently unsupported for asynchronous iterators. The relevant issue in GitHub is tc39/proposal-async-iteration#103.
正如我在上面评论的那样,不幸的是,异步迭代器目前不支持扩展运算符。GitHub 中的相关问题是tc39/proposal-async-iteration#103。
Recap from that issue (minus extraneous stuff such as "you could do it this way, oops no you can't, never mind"):
回顾那个问题(减去无关的东西,比如“你可以这样做,哎呀,你不能,没关系”):
Will array spread operator support ever be part of this proposal?
数组扩展运算符支持是否会成为该提案的一部分?
Not part of this proposal... Another proposal could certainly contemplate something new here.
不是这个提案的一部分……另一个提案当然可以在这里考虑一些新的东西。
And I don't see a proposal elsewhere (want to start one?). In any case, since it isn't part of even JavaScript ESNext, it most likely won't get added to TypeScript.
而且我在其他地方没有看到提案(想开始一个?)。无论如何,因为它甚至不是 JavaScript ESNext 的一部分,所以它很可能不会被添加到 TypeScript 中。
The most viable alternative is the for await
syntax detailed in @estus's answer. Sorry I couldn't be more helpful. Good luck!
最可行的替代方法是@estus's answer 中for await
详述的语法。抱歉,我帮不上忙了。祝你好运!
回答by Rohit Gupta
this has helped me. in tsconfig, add this :
这对我有帮助。在 tsconfig 中,添加以下内容:
{
"compilerOptions": {
"lib": [
"es5", "es6", "dom", "dom.iterable"
]
}
}
This is required for tsconfig to know which interfaces to import while transpiling your code.
这是 tsconfig 在转换代码时知道要导入哪些接口所必需的。
dom.iterable includes all the interfaces mentioned here : https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.iterable.d.ts
dom.iterable 包括这里提到的所有接口:https: //github.com/microsoft/TypeScript/blob/master/lib/lib.dom.iterable.d.ts
Also thanks to @domlas for mentioning this. I already upvoted his comment so that it is treated as default reason.
还要感谢@domlas 提到这一点。我已经对他的评论投了赞成票,因此它被视为默认原因。
回答by Estus Flask
Despite what the syntax may suggest, async generator function isn't async
function that returns a generator.
尽管语法可能暗示了什么,异步生成器函数不是async
返回生成器的函数。
As the proposalstates,
正如提案所述,
Async generator functions are similar to generator functions, with the following differences:
When called, async generator functions return an object, an async generator whose methods (next, throw, and return) return promises for { value, done }, instead of directly returning { value, done }. This automatically makes the returned async generator objects async iterators.
异步生成器函数与生成器函数类似,但有以下区别:
调用时,异步生成器函数返回一个对象,一个异步生成器,其方法(next、throw 和 return)返回对 { value, done } 的承诺,而不是直接返回 { value, done }。这会自动使返回的异步生成器对象成为异步迭代器。
It returns asynchronous generator, not a promise, so await
ing it doesn't make any good.
它返回异步生成器,而不是承诺,因此await
ing 它没有任何好处。
Since asynchronous generator has Symbol.asyncIterator
instead of Symbol.iterator
, it's non-iterable iterator, i.e. it cannot be iterated with regular ES6 methods (Array.from
, for..of
, spread syntax, etc). This is the reason why for await..of
was introduced.
由于异步生成器具有Symbol.asyncIterator
而不是Symbol.iterator
,因此它是不可迭代的迭代器,即它不能用常规的 ES6 方法(Array.from
、for..of
、扩展语法等)进行迭代。这就是for await..of
引入的原因。
The code above should be:
上面的代码应该是:
const values = [];
for await (const value of this.test()) {
values.push(v);
}
The iteration over asynchronous iterator can be desugared similarly to regular iterators, the difference is that next()
returns a promise of next value, not a value itself:
异步迭代器上的迭代可以与常规迭代器类似地去糖化,区别在于next()
返回下一个值的承诺,而不是值本身:
const iterator = this.test();
let next;
while ((next = await iterator.next()).done === false) {
values.push(next.value);
}
Since asynchronous generator spec is a proposal, the support for async iterators in ES6 iteration methods may be subject to change.
由于异步生成器规范是一个提案,ES6 迭代方法中对异步迭代器的支持可能会发生变化。