microsoft / microsoft/TypeScript
Generator functions which have a return type of a union of Iterator/AsyncIterator have a yield type of never
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
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 (https://github.com/microsoft/TypeScript-Website/issues/15).
Related Issues:
#30790 Strongly-typed iterator PR
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the TypeScript 3.7.2 diagnostic from example.ts, comparing the NumberIterator and NumberGenerator return types. Read related issue #30790 for iterator context, then verify that the synchronous generator with a union of Iterator and AsyncIterator compiles without the yield-type-never error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100