Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #10432.
This PR introduces two changes:
boolean
we now consider it a literal type context. Previously we specifically excluded this case.Some background on the changes.
Since
boolean
is identical to the union typetrue | false
, when an expression has the contextual typeboolean
we really should consider it a literal type context (because the contextual type is a union of unit types). Before this PR we specifically excluded that case in the name of backwards compatibility. With the fix we no longer do. However, that causes another issue to appear:In the example above, the expression
cond ? false : undefined
is contextually typed by typeboolean
because of the default value on the left hand side. That causes us to consider the expression a literal type context, and we therefore use typefalse
for thefalse
literal. So, the type ofx
on the right hand side becomesfalse | undefined
. Now, when we destructurex
we require the default value to be of a type assignable tofalse | undefined
. Butboolean
isn't.The issue really is that when a binding element has a default value, we ought to give it a union type of the destructuring type and the default type. With this PR we do. The downside is that you might sometimes end up with a union type when an error might have been more appropriate.
@mhegazy @DanielRosenwasser You might want to take a look.