-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Incorrect typing for iterables in lib #29982
Comments
I hit some runtime errors after following Typescript's iterable types. At runtime, a call to The return type of Here's a demonstration of the issue: https://github.com/justjake/ts-3.4.-async-iterable-issue async function* generate() {
yield 1
yield 2
yield 3
}
// AsyncIterableIterator<1 | 2 | 3>
type Out = ReturnType<typeof generate>
async function consume() {
const iterator = generate()
const yield1 = await iterator.next()
const yield2 = await iterator.next()
const yield3 = await iterator.next()
const yield4 = await iterator.next()
// IteratorResult<1 | 2 | 3>
type ExpectedYield4Type = typeof yield4
// 1 | 2 | 3
type ExpectedYield4ValueType = typeof yield4.value
console.log('yield4', yield4) // { value: undefined, done: true }
console.log('yiled4.value', yield4.value) // undefined
if (yield4.value === undefined) {
throw new Error('yield4.value type is 1 | 2 | 3, so value cannot be undefined')
}
}
consume() Here's a playground link, although this is less helpful because playground does not have the AsyncIterableIterator type (see also https://github.com/Microsoft/TypeScript/issues/23030#issuecomment-482692170) |
Search Terms:
iterable return type
asynciterable return type
Related Issues:
#2983 - describes how the iterables types are not inferred correctly for generators. Fixing this current issue would be a step towards making that issue resolvable as well.
The Problem
The type definitions for iterable and asynciterable do not match the specification.
According to the specification, when
IteratorResult
hasdone: true
, thevalue
is the return value of the iterator. This is not the same type as an element of the iterator. When implementing an iterator or async iterator "by hand", you get spurious type errors. For example:Gives errors:
Thus, the types for iterables and async iterables should look more like this:
and
This should allow typescript to correctly infer that
value
is not required whendone: true
. For generators that do have a distinct return type from their yield type, they will be able to typecheck without spurious errors.If the above definitions are acceptable to the maintainers I can prepare a PR to update the type definitions.
References
The text was updated successfully, but these errors were encountered: