-
Notifications
You must be signed in to change notification settings - Fork 13k
Add nested call and new expressions as potential intra expression inference sites #54183
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
base: main
Are you sure you want to change the base?
Add nested call and new expressions as potential intra expression inference sites #54183
Conversation
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
@typescript-bot test this |
Heya @weswigham, I've started to run the perf test suite on this PR at bcb0da5. You can monitor the build here. Update: The results are in! |
Heya @weswigham, I've started to run the diff-based top-repos suite on this PR at bcb0da5. You can monitor the build here. Update: The results are in! |
Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at bcb0da5. You can monitor the build here. Update: The results are in! |
Heya @weswigham, I've started to run the extended test suite on this PR at bcb0da5. You can monitor the build here. |
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 seems fine - iirc, needing to do a deeper traversal for context-sensitivity checking was something we've known for awhile, we've just been avoiding doing it in most of our other fixes, since we've been worried about the perf cost of a deeper tree traversal. Assuming perf comes back fine, only change I think I'd want is a deprecated isContextSensitive
in the public API (which can just call the new function), so this isn't a hard API break.
Heya @weswigham, I've run the RWC suite on this PR - assuming you're on the TS core team, you can view the resulting diff here. |
@weswigham Here are the results of running the top-repos suite comparing Everything looks good! |
Hey @weswigham, the results of running the DT tests are ready. |
…inference-with-resolved-calls # Conflicts: # src/compiler/checker.ts # tests/baselines/reference/intraExpressionInferences.errors.txt # tests/baselines/reference/intraExpressionInferences.js # tests/baselines/reference/intraExpressionInferences.symbols # tests/baselines/reference/intraExpressionInferences.types # tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts
is this relevant for a function that is annotated with |
@weswigham Here they are:
CompilerComparison Report - main..54183
System
Hosts
Scenarios
TSServerComparison Report - main..54183
System
Hosts
Scenarios
StartupComparison Report - main..54183
System
Hosts
Scenarios
Developer Information: |
…inference-with-resolved-calls # Conflicts: # src/compiler/checker.ts
…inference-with-resolved-calls # Conflicts: # src/compiler/checker.ts # tests/baselines/reference/intraExpressionInferences.errors.txt # tests/baselines/reference/intraExpressionInferences.js # tests/baselines/reference/intraExpressionInferences.symbols # tests/baselines/reference/intraExpressionInferences.types # tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts
…inference-with-resolved-calls
68e3c49
to
bfdaa86
Compare
@typescript-bot perf test public |
Heya @weswigham, I've started to run the public perf test suite on this PR at bfdaa86. You can monitor the build here. Update: The results are in! |
@weswigham Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@typescript-bot run dt |
Heya @weswigham, I've started to run the diff-based user code test suite on this PR at bfdaa86. You can monitor the build here. Update: The results are in! |
…inference-with-resolved-calls
It would definitely impact some inferences negatively. Context-sensitive functions are replaced with |
@Andarist maybe there's been a miscommunication - I was asking about all call/construct invocation expressions like
In #54183 (comment), it seems you're talking about all function expressions like
|
Ah, sorry - I misread it. But isn't this what this PR is effectively doing? function containsContextSensitiveOrCallOrNewExpression(node: Expression | MethodDeclaration | ObjectLiteralElementLike | JsxAttributeLike | JsxChild): boolean {
return containsContextRelatedNode(node, n => isContextSensitiveFunctionOrObjectLiteralMethod(n) || isCallOrNewExpression(n));
} This check doesn't have any extra checks combined with |
…inference-with-resolved-calls
#57909 was merged so the issue above is resolved now. @jakebailey could you re-run top800 and other important tests here? @DanielRosenwasser @weswigham could I ask for another review? :) |
@typescript-bot perf test this |
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
tsserverComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
startupComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@jakebailey Here are the results of running the top 800 repos with tsc comparing Everything looks good! |
@jakebailey would it be possible to get a PR preview build with the changes from here? We think there might be an issue in react-query that’s related to this and I’d like to check if this PR fixes it: 🙏 |
…inference-with-resolved-calls
a9c578f
to
32740a4
Compare
@typescript-bot pack this |
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
Thank you @jakebailey ❤️ . @Andarist just confirmed that this PR fixes the issue, which is great news 🎉 |
…inference-with-resolved-calls
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.
Pull Request Overview
This PR improves TypeScript's type inference for nested generic function calls by expanding the conditions under which expressions are considered as potential intra-expression inference sites. The change addresses a bug where nested generic functions were being skipped during the first pass of type inference, leading to incomplete type resolution.
Key changes:
- Renames
isContextSensitive
tocontainsContextSensitive
for better clarity of purpose - Introduces
containsContextSensitiveOrCallOrNewExpression
to include call and new expressions as inference sites - Updates inference site detection to consider nested calls and constructors alongside context-sensitive functions
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
src/compiler/checker.ts |
Core logic changes to expand inference site detection and rename context sensitivity checking function |
src/compiler/types.ts |
Updates type interface to reflect function rename |
src/services/refactors/extractSymbol.ts |
Updates function call to use renamed containsContextSensitive |
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferencesNestedGenericCall1.ts |
Comprehensive test case for nested generic call inference with complex Fastify-style type provider pattern |
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferencesJsx.tsx |
Additional JSX test case demonstrating improved inference |
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts |
Extended test cases for both object and tuple parameter patterns |
Various baseline files | Expected test outputs showing improved type inference results |
Comments suppressed due to low confidence (2)
…inference-with-resolved-calls
Sometimes nested generic functions are skipped during inference in the first pass:
https://github.dev/microsoft/TypeScript/blob/04d4580f4eedc036b014ef4329cffe9979da3af9/src/compiler/checker.ts#L33619-L33634
However, those calls are resolved from within
checkExpressionWithContextualType
and by the time we have a chance to calladdIntraExpressionInferenceSite
they are already resolved. So it, imho, makes sense to actually use them as intra expression inference sites.fixes #54184
fixes #52114
fixes #53776