-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Add TReturn/TNext to Iterable et al #58243
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
@jakebailey, @weswigham: Am I missing something? The bot says there were interesting changes but it links to a clean pipeline result. |
@rbuckton Here are the results of running the top 400 repos comparing Something interesting changed - please have a look. Details
|
Based on the last two runs, the following is a summary of the differences between
I think the assignability issues in (1.iv) are far more problematic than those in (2.iii), as those in (2.iii) can be addressed with postfix- |
Would it be possible to get a fresh packaged build? |
@typescript-bot: pack this |
Hey @rbuckton, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
For what it's worth:
I wonder if it's worth considering a combination of the two approaches: keeping the generator body parsing behaviour as it is in 5.4 (with fallback return type |
I'm really not certain they should differ.
One of the reasons for this change is to support the Iterator helpers proposal. One of the capabilities in that proposal is allowing users to subclass class MyIterator extends Iterator {
next() {
...
}
} Since custom iterators can have a meaningful |
It is not just |
@typescript-bot run dt |
@rbuckton Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
@rbuckton Here are the results of running the user tests with tsserver comparing Everything looks good! |
e6df161
to
fe2ff63
Compare
@rbuckton Here are the results of running the top 200 repos with tsserver comparing Everything looks good! |
Hey @rbuckton, the results of running the DT tests are ready. There were interesting changes: Branch only errors:Package: node/v16
Package: node/v18
Package: node
Package: get-intrinsic
Package: regenerator-runtime
Package: es-get-iterator
Package: bresenham
Package: win-ca
Package: es-abstract
|
@rbuckton Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
It looks like top400 generated an empty repo list on the last run, so it didn't actually test anything. (I guess the pipeline should probably check for this!) |
Hm, that's concerning. @typescript-bot test top400 |
@jakebailey Here are the results of running the top 400 repos with tsc comparing Something interesting changed - please have a look. Details
|
|
We avoided making this change in the past as it was very breaky, but at some point we need to address this discrepancy. Having incorrect types here is also causing problems with properly typing the Iterator Helpers proposal.
This also adds a new
BuiltinIteratorReturn
intrinsic type whose actual type is determined by the state of a newstrictBuiltinIteratorReturn
compiler option:"strictBuiltinIteratorReturn": false
-any
(emulates current behavior forIterableIterator
)"strictBuiltinIteratorReturn": true
-undefined
The
strictBuiltinIteratorReturn
is astrict
option flag, meaning that it is enabled automatically when you set"strict": true
in yourtsconfig.json
.The new
BuiltinIteratorReturn
type is passed as theTReturn
type argument for built-ins usingIterableIterator
orAsyncIterableIterator
to enable stricter checks against the result of callingnext()
on the iterator.Enabling
strictBuiltinIteratorReturn
results in a more accurate and type-safe return type for thenext()
methods of iterators produced by built-ins likeArray
,Set
,Map
, etc.:vs
Since this is a
strict
flag, there is a fair amount of existing code that will likely produce new compilation errors as a result of this change:This now fails as there is no correlation between
set.size
and the iterator produced bykeys()
, so the compiler is unaware that this constraint has been validated. If you are certain iterator will always yield at least one value, you can use a non-null assertion operator to strip off the| undefined
:A follow-on PR to TypeScript-DOM-lib-generator can be found here: microsoft/TypeScript-DOM-lib-generator#1713
DefinitelyTyped breaks will be addressed by DefinitelyTyped/DefinitelyTyped#69632
Fixes #33353
Fixes #52998
Fixes #43750
Supersedes #56517
Related #58222