Skip to content

Iterator Helpers: return method typing is weaker than ideal #60176

Open

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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions