Open
Description
⚙ Compilation target
ESNext
⚙ Library
esnext.iterator.d.ts
Missing / Incorrect Definition
-
Iterator.from(value: Iterator)
- PR Improve generic type signature of Iterator.from() #59927-
this is a bad idea; my mistakevalue
iterator argument withTNext
other thanundefined
should be accepted; - Since
IteratorObject
instances (objects whose prototype isIterator.prototype
) are not wrapped byIterator.from
, we could add anIterator.from
overload to represent those cases and pass through all generic type parameters:from<T, TReturn = any, TNext = any>(value: IteratorObject<T, TReturn, TNext>): IteratorObject<T, TReturn, TNext>;
-
TReturn
type should be passed through from the argument to the return type
-
- Iterator helper intermediate operators (
filter
,map
etc) do not propagate the return value of their source iterator and should haveTReturn
set toundefined
- This is already the case in the current declarations
-
Iterator objects returned fromthanks to @bakkot for the clarificationIterator.from
and the other built-in iterators returned fromArray.values
etc always have areturn
method (built-in iterators returned fromArray.values
etc also always have athrow
method)- There is no way that I am aware of to express this behavior in TypeScript with the current definition of
IteratorObject
without also forcing user-defined classes extending from the javascriptIterator
class to also implement thereturn
method, which would be wrong. The impact on client code is minimal as the Iterator object'sreturn
method can always be called safely using thereturn!()
notation.
- There is no way that I am aware of to express this behavior in TypeScript with the current definition of
Sample Code
https://github.com/nikolaybotev/iteratorhelpersdemo
Documentation Link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator
and
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols