Consider this basic AsyncIterator Example from MDN:
var asyncIterable = {
[Symbol.asyncIterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return Promise.resolve({ value: this.i++, done: false });
}
return Promise.resolve({ done: true });
}
};
}
};
(async function() {
for await (let num of asyncIterable) {
console.log(num);
}
})();
Running this on node 10.16.0 works fine. However, I cannot seem to make it run via Typescript. Using this tsconfig:
{
"compilerOptions": {
"lib": ["es2016", "esnext.asynciterable"],
"target": "es2016"
}
}
results in error The type returned by the 'next()' method of an async iterator must be a promise for a type with a 'value' property.
Same error for target esnext
.
If I remove the target
option entirely, I get a Type '{ [Symbol.asyncIterator](): { i: number; next(): Promise<{ done: boolean; }>; }; }' is not an array type or a string type.
The TS manual mentions several caveats, none of which could solve my problem. Oddly enough, iterating async generators works fine.
Which tsconfig options are required to make this example compile?