Repository navigation
Fall back to the constraint in the return mapper instead of filtering the inferred union - #65818
Open
Max Schwenk (maschwenk) wants to merge 1 commit into
Open
Max Schwenk (maschwenk) wants to merge 1 commit into
Max Schwenk (maschwenk) wants to merge 1 commit into
Conversation
… the inferred union Fixes microsoft#65817
Max Schwenk (maschwenk)
marked this pull request as ready for review
December 5, 2026 05:50
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The return mapper loses mutable-array context for constrained inference, causing ok([]) to infer Ok<readonly []> and trigger TS2322; the fix needs to preserve that context and add a regression test.
1 open issue
What changed in this PR
Addresses #65817’s excessive type instantiations in nested generic calls by changing contextual return inference in the TypeScript checker.
Changes:
- Marks return-mapper inference contexts with a dedicated flag.
- Falls back to constraints instead of filtering incompatible unions in those contexts.
- Adds a six-type regression case with type and symbol baselines.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/contextualReturnTypeInferenceConstraintFallback.ts |
Adds the nested-call regression test. |
tsc/testdata/baselines/reference/compiler/contextualReturnTypeInferenceConstraintFallback.types |
Records expected inferred types. |
tsc/testdata/baselines/reference/compiler/contextualReturnTypeInferenceConstraintFallback.symbols |
Records expected symbol bindings. |
tsc/internal/checker/inference.go |
Disables union filtering for return mappers. |
tsc/internal/checker/checker.go |
Defines and applies the return-mapper flag. |
Review effort: Balanced
| // A return mapper's inferences only instantiate contextual types, so they fall back to the constraint. | ||
| // Filtering there can keep a large union whose every constituent then instantiates each argument's | ||
| // contextual type. | ||
| if inference.priority == InferencePriorityReturnType && n.flags&InferenceFlagsReturnMapper == 0 { |
This branch has not been deployed
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Fixes #65817
This is an automated PR authored by Claude Code using Claude Opus 5.5 with Extra High reasoning level.
Motivation & Guidance
Motivation: The user asked me to fix #65617's regression.
Guidance:
Analysis
The nested call's contextual return type includes
null, so return mapper inference forEfails theobjectconstraint. Before #65617, it fell back toobject. With #65617'sInferencePriorityReturnType,getInferredTypekeeps every union member satisfyingobject. Each argument's contextual type is instantiated over that union.Fix
Setting
InferenceFlagsReturnMapperdisables constraint filtering only for the return mapper. No existing baselines change, and #64690 stays fixed. Reverting the priority instead reopens #64690.tests/instantiationscontextualReturnTypeInferenceConstraintFallback(the issue's repro with 6 types)