microsoft / microsoft/TypeScript
Iterator Helpers: `return` method typing is weaker than ideal
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
⚙ Compilation target
ESNext
⚙ Library
esnext.iterator.d.ts
Missing / Incorrect Definition
The return method always exists on iterators returned by Iterator.from (iter in below code) and iterators returned by Iterator Helpers methods (iter2 in below code).
However, it is defined as optional in the current definition. This could be improved.
Please note that you can't just add a non-optional return method definition to the IteratorObject type -- iterators returned by builtin methods other than Iterator Helpers don't have that method (iter3).
P.S. if you are interested in a motivating example, I don't have one. Sorry 🤪
Sample Code
const iter = Iterator.from({ next() { return { done: false, value: 0 }} });
iter.return(); // should not error
const iter2 = iter.map(x => x);
iter2.return(); // should not error
const iter3 = [].values();
iter3.return(); // Note: this error IS correct
Documentation Link
https://tc39.es/proposal-iterator-helpers/
Spec states that below objects always have the return method:
- %WrapForValidIteratorPrototype%
- %IteratorHelperPrototype%
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
Start in esnext.iterator.d.ts and inspect the IteratorObject, Iterator.from, and Iterator Helpers definitions against the sample code. Verify the expected behavior against the linked Iterator Helpers specification: Iterator.from and helper results should allow return(), while [].values() should continue to reject it. Done means the declarations distinguish these cases without introducing an error for iter3.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 58/100