-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve generic type signature of Iterator.from() #59927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Improve generic type signature of Iterator.from() #59927
Conversation
I am looking into the 4 tests errors. The first two errors relate to expected errors and types/symbols in declare const g1: Generator<string, number, boolean>;
const iter1 = Iterator.from(g1); I believe this is the correct behavior! I do not know how to update the test baseline to reflect this new behavior introduced here. I am going to look into the other two errors and report here in a subsequent comment. |
Regarding the first 2 errors, here is a script that demonstrates that the code that now compiles (and fails the tests), should indeed compile, and runs as expected in Node.js 22: const g1: Generator<string, number, boolean> = function* g1gen() {
yield "hello";
return 42;
}();
const iter1 = Iterator.from(g1);
console.log(iter1.next()); // { value: 'hello', done: false }
console.log(iter1.next()); // { value: 42, done: true } |
The other two errors are about
Those two files contain calls to Again, I do not (yet) know how to go about doing this. I would be glad to take it on give some pointers in the right direction. |
@microsoft-github-policy-service agree |
Done. I have updated the baselines. |
Now going to see if I can add the two test scenarios linked in the description of this PR as new tests. |
|
…g the return value of the wrapped iterator
Tests added. |
I just realized my mistake in expecting |
This PR is now ready for review. |
I am now realizing that the passing along of the wrapped iterator's return value by the object returned by NOT passing along the NOT passing along the return value of Iterator Helpers really operate on one-way streams and the one-way stream aspect of Iterators and Generators is really best captured by their The Can someone confirm my reading is correct? |
Fixes #59926
Background
Iterator.from
returns an iterator that passes along the return value of its wrapped iterator, but does NOT pass along the argument to itsnext
method to the wrapped iterator. This behavior is observed in Node.js 22 (and other implementations, see below) using the following scripts and I assume is the standard behavior:Conformance
core-js
node.js 22
Firefox
Opera
Problem
The current type signature for
Iterator.from
basically ignores theTReturn
type argument of the underlyingIterator
protocol, which restrict certain valid code from passing typescript compilation, such as this example:Change
TReturn
type argument toIterator.from
and pass it through from the argument to the return type so as to reflect the observed behavior of theIterator.from
implementation.Iterator
class'TResult
type argument toTReturn
.Tests
tests/cases/compiler/builtinIterator.ts
to cover the scenario of the iterator returned byIterator.from
passing along the return value of the wrapped iterator.