Improve generic function inference#30193
Merged
ahejlsberg merged 7 commits intomasterfrom Mar 8, 2019
Merged
Conversation
weswigham
reviewed
Mar 3, 2019
| const expr = returnStatement.expression; | ||
| if (expr) { | ||
| let type = checkExpressionCached(expr, checkMode); | ||
| let type = checkExpressionCached(expr, checkMode && checkMode && checkMode & ~CheckMode.SkipGenericFunctions); |
Member
Author
There was a problem hiding this comment.
I just wanted to be extra sure!
Member
There was a problem hiding this comment.
I heard this is how you make really really thread-safe code in Node dot jay ess.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
numbertoxandy.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
f2because we'd process theboxargument ahead of thex => [x]argument and miss the opportunity to have inferences flow from the return type annotation intoAand from the result ofx => [x]intoBbefore using inferences forBto contextually typebox. Likewise, we'd error onf3because we'd process the innerpipecall 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.