-
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 generic function inference #30193
Conversation
@@ -22060,7 +22089,7 @@ namespace ts { | |||
forEachReturnStatement(<Block>func.body, returnStatement => { | |||
const expr = returnStatement.expression; | |||
if (expr) { | |||
let type = checkExpressionCached(expr, checkMode); | |||
let type = checkExpressionCached(expr, checkMode && checkMode && checkMode & ~CheckMode.SkipGenericFunctions); |
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.
This line seems like a typo.
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.
I just wanted to be extra sure!
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.
I heard this is how you make really really thread-safe code in Node dot jay ess.
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.