-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Contextually typed binding element initializers #35855
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
Conversation
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the parallelized community code test suite on this PR at 529afa9. You can monitor the build here. It should now contribute to this PR's status checks. |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 529afa9. You can monitor the build here. It should now contribute to this PR's status checks. |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at 529afa9. You can monitor the build here. It should now contribute to this PR's status checks. |
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
@typescript-bot run dt |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at f986e16. You can monitor the build here. It should now contribute to this PR's status checks. |
Heya @ahejlsberg, I've started to run the parallelized community code test suite on this PR at f986e16. You can monitor the build here. It should now contribute to this PR's status checks. |
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
RWC tests are clean. DT and Community test failures all appear to be preexisting conditions. |
@RyanCavanaugh @weswigham This one is ready to merge. Want to take a look? |
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.
Just to confirm: we no longer need to specially handle the widening of binding patterns whose initializer is an assertion expression because... our assumption is assertions can't occur in JS files? (/* @type */
casts should probably have counted though, even though they didn't) Or for another reason?
@weswigham The special case for a type assertion predated our support for fresh literal types. It was there just so you could force a binding element declaration not to widen. We neither need nor want that odd behavior anymore. Widening now follows naturally from the combined freshness of the destructured element type and the initializer type, i.e. a unit type stays fresh only of every occurrence of it in a union type is fresh. |
This PR revises our contextual typing logic for initializers in destructuring declarations. Previously, the construct
had no contextual type for
xxx
. Now,xxx
is contextually typed byb.a
and the type inferred fora
is consistent with the logically equivalentwith the exception that
??
substitutesxxx
for bothundefined
andnull
whereas destructuring has no special behavior fornull
.Some examples:
Before this PR, the inferred type for
a2
wasnumber
. Now, because the type of the initializer is a subtype of the contextual type0 | 1 | undefined
, no widening occurs. However, an initial value of2
causes widening because it isn't a subtype. If this widening is not desired, aconst
assertion can be used to suppress it.Fixes #35693.