-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Improve contextual typing by generic rest parameters #30114
Conversation
@@ -20018,7 +20018,7 @@ namespace ts { | |||
// We clone the contextual mapper to avoid disturbing a resolution in progress for an | |||
// outer call expression. Effectively we just want a snapshot of whatever has been | |||
// inferred for any outer call expression so far. | |||
const instantiatedType = instantiateType(contextualType, cloneTypeMapper(getContextualMapper(node))); | |||
const instantiatedType = instantiateType(contextualType, cloneTypeMapper(getContextualMapper(node), InferenceFlags.NoDefault)); |
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 cloneTypeMapper
call used to create the returnType
mapper inside call resolution doesn't pass InferenceFlags.NoDefault
with this change. Intentional?
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, it's intentional. We only want the InferenceFlags.NoDefault
behavior here (because we're transposing inferences from the outer call mapper and don't want an absence of inferences in the outer mapper to turn into what appears to be a concrete inference of the default).
This PR improves contextual typing by generic rest parameters such that we can infer a tuple type from the function expression or arrow function being contextually typed while simultaneously assigning contextual parameter types to the function expression or arrow function. Consider:
In the examples above, the type inferred for
A
is[]
,[any]
,[any, any]
, and[number]
respectively. Previously, in all but the last example we'd inferany[]
forA
.