Infer from context sensitive return expressions#60909
Infer from context sensitive return expressions#60909Andarist wants to merge 11 commits intomicrosoft:mainfrom
Conversation
|
@typescript-bot test it |
|
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
|
@jakebailey Here are the results of running the user tests with tsc comparing Everything looks good! |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
|
Ok, I think this one is in good shape for review. I've only made a small change to the code - I don't think it will impact any code in the wild but it might be good to rerun the extended tests. |
| SkipGenericFunctions = 1 << 3, // Skip single signature generic functions | ||
| IsForSignatureHelp = 1 << 4, // Call resolution for purposes of signature help | ||
| RestBindingElement = 1 << 5, // Checking a type that is going to be used to determine the type of a rest binding element | ||
| SkipReturnTypeFromBodyInference = 1 << 4, // Skip inferring from return types of context sensitive functions |
There was a problem hiding this comment.
It's separate from SkipGenericFunctions because getReturnTypeFromBody removes SkipGenericFunctions bit - so when the compiler gets to a function node with type parameters within a context-sensitive function's return it can't just use SkipGenericFunctions to skip over inferring its own return type from body.
The goal of SkipReturnTypeFromBodyInference is to persist through getReturnTypeFromBody.
| const inferenceContext = getInferenceContext(node); | ||
| const isReturnContextSensitive = !!node.body && (node.body.kind === SyntaxKind.Block ? forEachReturnStatement(node.body as Block, statement => !!statement.expression && isContextSensitive(statement.expression)) : isContextSensitive(node.body)); | ||
| returnType = getReturnTypeFromBody(node, checkMode | (isReturnContextSensitive ? CheckMode.SkipContextSensitive : 0)); | ||
| inferTypes(inferenceContext!.inferences, returnType, contextualReturnType); |
There was a problem hiding this comment.
alternatively, this could be handled by intra expression inference but that is order-sensitive and traditionally simple cases like this one aren't:
declare function test<T>(_: {
stuff: T,
consume: (arg: T) => void
}): void
test({
consume: (arg) => {},
stuff: 'foo' // this can come after `consume`
})So the reason I put this logic here is that it allows for the same order-independence
…ntext-sensitive-return # Conflicts: # src/compiler/checker.ts
|
@typescript-bot test top999 |
|
@RyanCavanaugh Here are the results of running the top 999 repos with tsc comparing Everything looks good! |
…ntext-sensitive-return
05b5e63 to
635b4fd
Compare
fixes #60720
fixes #57021