Repository navigation
Produce union types when instantiating generic contextual types - #65617
Anders Hejlsberg (ahejlsberg) wants to merge 5 commits into
Conversation
|
TypeScript Bot (@typescript-bot) test it |
There was a problem hiding this comment.
🔵 Needs a closer look
The inference changes affect contravariant candidates, literal widening, and unions, requiring compiler-suite validation of recursive constraints, binding patterns, and boolean contexts that cannot be run in this review environment.
No open issues 🎉
What changed in this PR
This PR updates the compiler’s return-derived contextual inference to preserve candidates as union types, addressing #64690’s regression where ok([]) inferred a readonly tuple incompatible with the declared union return type.
Changes:
- Use return-type inference priority when constructing contextual return mappings.
- Apply constraint filtering to combined return-priority flags and exclude
neverfrom the preferred contextual-type path. - Add a regression test and baselines showing mutable
[]inference.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/contextualReturnTypeInferenceUnions.ts |
Adds the regression case for #64690. |
tsc/testdata/baselines/reference/compiler/contextualReturnTypeInferenceUnions.types |
Records the expected Ok<[]> inference. |
tsc/testdata/baselines/reference/compiler/contextualReturnTypeInferenceUnions.symbols |
Records symbols for the regression test. |
tsc/internal/checker/inference.go |
Recognizes return-type priority within combined flags during constraint filtering. |
tsc/internal/checker/checker.go |
Updates contextual return inference priority and handling of never. |
Review effort: Balanced
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
|
Hey Anders Hejlsberg (@ahejlsberg), the results of running the DT tests are ready. Everything looks the same! |
|
Anders Hejlsberg (@ahejlsberg) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Looks like some new errors in those benchmarks. |
|
Jake Bailey (@jakebailey) It needed a merge from main. |
|
Anders Hejlsberg (@ahejlsberg) Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
With this PR we produce union types (instead of picking particular inference candidates) when instantiating generic contextual types that were inferred from return types. This is consistent with preserving as much information as possible in contextual types.
Fixes #64690.