Improve generic function inference #30193
Merged
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.
This PR addresses several issues related to type inference involving combinations of contextually typed arrow functions and generic functions.
In general, type argument inference defers processing of contextually typed arrow functions and function expressions as long as possible such that inferences can be collected from other arguments before those inferences are fixed and used to type the arrow function or function expression parameters. For example:
Here, we defer processing of the arrow function until we have made inferences the two number arguments and then use those inferences to assign type
number
tox
andy
.Previously, contextually typed arrow functions and function expressions were the only types of arguments for which we'd defer inference. However, arguments with generic function types are also be subject to contextual typing. Consider:
Before this PR we would error on
f2
because we'd process thebox
argument ahead of thex => [x]
argument and miss the opportunity to have inferences flow from the return type annotation intoA
and from the result ofx => [x]
intoB
before using inferences forB
to contextually typebox
. Likewise, we'd error onf3
because we'd process the innerpipe
call before thex => [x]
arrow function.With this PR we now defer processing of arguments having generic function types along with contextually typed arrow functions and function expressions.
Fixes #25791.
Fixes #25826.