Open
Description
TypeScript Version: 3.7.2
Search terms: generator iterator asynciterator type union never
Code
example.ts
type NumberIterator = Iterator<number> | AsyncIterator<number>;
type NumberGenerator = Generator<number> | AsyncGenerator<number>;
function *numbers(): NumberIterator {
yield 1;
}
A type alias which is a union of Iterator
and AsyncIterator
is used as the return type of a synchronous generator.
Expected behavior:
Code compiles.
Actual behavior:
example.ts(5,11): error TS2322: Type '1' is not assignable to type 'never'.
If you replace NumberIterator
with NumberGenerator
as the return type the function above, the code correctly type-checks. Something is wrong here.
Playground Link:
The playground link won’t work because the playground doesn’t support async iterators yet (microsoft/TypeScript-Website#15).
Related Issues:
#30790 Strongly-typed iterator PR