Only get the apparent type of a contextual type when needed #5607
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 #5598.
Background
For quite a while, it was the case that when seeking the contextual type of a value, we would always get its apparent type afterwards. This was usually the behavior desired - otherwise, you'd typically run into problems with things like object literals and index signatures.
During recent work on string literal types, this turned out to be undesirable because the widened type of a string literal type is the global
String
type. This was effectively useless because we needed to check whether the contextual type was string literal type or a union with a string literal type, so we separated out the concept of grabbing an expression's contextual type (withgetContextualType
) with grabbing the apparent type of that contextual type (getApparentTypeOfContextualType
).One thing missed in that change was that internally,
getContextualType
was callinggetApparentTypeOfContextualType
unnecessarily. This means that if you had a parenthesized string literal, it could never be contextually typed by a string literal type - it would instead get contextually typed by the global string type.The reason the apparent type was necessary in some places is that when "digging in" to a type, it's necessary to get the apparent type to recognize its available members, the shape of its type constraint, etc.; however, the apparent type is not needed in simple cases where we simply propagate the type back.