microsoft / microsoft/TypeScript

Iterator Helpers: `return` method typing is weaker than ideal

Open
#60,176 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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

Playground

Documentation Link

https://tc39.es/proposal-iterator-helpers/

Spec states that below objects always have the return method:

  • %WrapForValidIteratorPrototype%
  • %IteratorHelperPrototype%

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.