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. |
@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 |
src/lib/dom.iterable.d.ts
Outdated
@@ -1,30 +1,30 @@ | |||
/// <reference lib="dom" /> | |||
|
|||
interface DOMTokenList { | |||
[Symbol.iterator](): IterableIterator<string>; | |||
[Symbol.iterator](): IterableIterator<string, void>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably this change is going to be reflected in the dom lib generator, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file isn't one of the generated ones, actually, so I don't think the generator has to change unless one of the *.generated.*.d.ts
files are edited.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I plan to do that as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generator will need to change so that we have the correct types on any DOM collections produced by the generator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That ends up being a bit tricky if we're also shipping DOM types in a standalone package since older TS wouldn't support TReturn
and TNext
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably this change is going to be reflected in the dom lib generator, too?
@@ -2,11 +2,12 @@ | |||
|
|||
=== YieldStarExpression4_es6.ts === | |||
function *g() { | |||
>g : () => Generator<any, void, undefined> | |||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |||
>g : () => Generator<any, void, unknown> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it weird to think that it's almost better for the type for an untyped yield
result to be void
instead of unknown
(meaning what we infer, not the default)? I guess it'd be funky seeing the void
in the parameter list of the .next
call, but it feels like if you wrote function foo*() { return yield val }
, having foo
's TReturn
be void
instead of unknown
would be kinda nice.
I dunno, probably not terribly important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having TNext
default to either void
ended up being an even larger breaking change.
export const Nothing1: Strategy<State> = strategy("Nothing", function*(state: State) { | ||
~~~~~~~~ | ||
!!! error TS2345: Argument of type '(state: State) => Generator<never, State, unknown>' is not assignable to parameter of type '(a: State) => IterableIterator<State, void>'. | ||
!!! error TS2345: Call signature return types 'Generator<never, State, unknown>' and 'IterableIterator<State, void>' are incompatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: We should special-case the error messages for comparing a generator/iterable/iterator - this is a really big stack just to say one of the return, next, or yield types don't match.
It only does that check for syntax. Most consumers of iterables are APIs, like In an ideal world the type for |
@@ -9,17 +9,17 @@ interface SymbolConstructor { | |||
readonly asyncIterator: unique symbol; | |||
} | |||
|
|||
interface AsyncIterator<T, TReturn = any, TNext = undefined> { | |||
interface AsyncIterator<T, TReturn = any, TNext = unknown> { | |||
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places. | |||
next(...args: [] | [TNext]): Promise<IteratorResult<T, TReturn>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth taking the opportunity to label the tuple element here (and in AsyncIterator/Generator) to avoid args_0
appearing in the computed function signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@typescript-bot perf test |
@rbuckton Here are the results of running the user tests comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
Hey @rbuckton, the results of running the DT tests are ready. There were interesting changes: Branch only errors:Package: string.prototype.matchall
Package: async-iterable-stream
Package: regenerator-runtime
Package: es-get-iterator
Package: consumable-stream/v1
Package: consumable-stream/v2
Package: consumable-stream
|
@rbuckton Here are the results of running the user tests comparing Something interesting changed - please have a look. Details
|
@rbuckton Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
tsserverComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
startupComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@rbuckton Here are the results of running the top 400 repos comparing Something interesting changed - please have a look. Details
|
@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 |
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 thenoUncheckedIndexedAccess
compiler option:"noUncheckedIndexedAccess": false
-any
(emulates current behavior forIterableIterator
)"noUncheckedIndexedAccess": true
-undefined
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.A follow-on PR to TypeScript-DOM-lib-generator can be found here: microsoft/TypeScript-DOM-lib-generator#1713
Fixes #33353
Fixes #52998
Fixes #43750
Supersedes #56517
Related #58222