Open dobesv opened 5 years ago
I hit some runtime errors after following Typescript's iterable types.
At runtime, a call to await iterable.next()
will return { done: true, value: undefined }
for a complete async * function
generator. However, the return type for async * function
generator,
AsyncIterableIterator<T>
, does not allow for value: undefined
.
The return type of await iterable.next()
should include | { done: true, value: undefined }
.
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)
@RyanCavanaugh @rbuckton
Search Terms:
iterable return type asynciterable return type
Related Issues:
https://github.com/Microsoft/TypeScript/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