Open
Description
openedon Oct 9, 2024
⚙ 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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment