-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trouble implementing AsyncIterator interface in 3.6.2 #33239
Comments
The error message has a lot of text, but it's clear:
|
@ilogico Woops, I forgot to change the return type when I was testing that this error does not happen for sync iterators. class ConstantIterator<T> implements AsyncIterator<T, undefined, T | undefined> {
constructor(private constant: T) {
}
async next(value?: T): Promise<IteratorResult<T>> {
if (value != null) {
throw new Error("ConstantIterator.prototype.next may not take any values");
}
return {value: this.constant, done: false};
}
} try running tsc version 3.6.2 on the above code.
Is this an actual requirement in the async iterator spec? Can you point to where this is stated? As far as I can tell I have never defined an async iterator which expected a promise as the result of a yield. |
Based on a quick test in Node.js, there is no special handling for passing promises to > async function* g() { console.log(yield); }
undefined
> let iter = g();
undefined
> iter.next(Promise.resolve(812))
Promise { <pending> }
> iter.next(Promise.resolve(812))
Promise { 812 }
Promise { { value: undefined, done: true } } The promise is not unwrapped by |
Yeah I initially thought it was an assignability error related to the weird tuple args, but I think it’s probably the fact that |
@sandersn Can you explain the addition of the “Working as Intended” label? Here’s the current interface AsyncIterator<T, TReturn = any, TNext = undefined> {
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
next(...args: [] | [TNext | PromiseLike<TNext>]): Promise<IteratorResult<T, TReturn>>;
return?(value?: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T, TReturn>>;
throw?(e?: any): Promise<IteratorResult<T, TReturn>>;
} Requiring the
At no point in any of these steps is the The correct definition of interface AsyncIterator<T, TReturn = any, TNext = undefined> {
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
next(...args: [] | [TNext]): Promise<IteratorResult<T, TReturn>>;
return?(value?: TReturn): Promise<IteratorResult<T, TReturn>>;
throw?(e?: any): Promise<IteratorResult<T, TReturn>>;
} In other words, the same as You could make an argument that the value passed to In any case, the value passed to |
I agree that requiring the next method to accept |
To be 100% clear: if a |
@brainkim I understood your comment
To refer to your misunderstanding. But it sounds like it's a misunderstanding by the author of lib.d.ts? |
@sandersn Misunderstandings all around! Yes, I think lib.d.ts was written with a slight misunderstanding of async iterators. |
Would it be possible to fix this in a 3.6 (3.6.4?) release? Currently assigning/implementing async iterators is really broken on 3.6 and I don’t like idea of telling people you have to skip a version of typescript just to use them. I would also be willing to volunteer time to get this and other (async) iterator issues fixed! Let me know if you’re willing to accept non-microsoft contributions for these issues. |
You are correct that |
TypeScript Version: 3.6.2
Search Terms:
async iterator iterable next
Code:
filename: constant.ts
Expected behavior:
Code compiles.
Actual behavior:
Note that the synchronous version of
ConstantIterator
does not throw the same error. It’s the type union ofT | PromiseLike<T>
which seems to cause the error.Playground Link:
Playground has yet to be updated for 3.6.
Related Issues:
#30790 Strongly typed iterator PR.
#32682 Other (async) iterator usability issues.
#33131 Possible change to type inference in 3.6.
The text was updated successfully, but these errors were encountered: