Skip to content

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
microsoft:mainfrom
maschwenk:fix-return-mapper-constraint-filter
Open

Max Schwenk (maschwenk) wants to merge 1 commit into
microsoft:mainfrom
maschwenk:fix-return-mapper-constraint-filter

Conversation

@maschwenk

@maschwenk Max Schwenk (maschwenk) commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

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:

get a subagent working on that #65617 regression

Analysis

The nested call's contextual return type includes null, so return mapper inference for E fails the object constraint. Before #65617, it fell back to object. With #65617's InferencePriorityReturnType, getInferredType keeps every union member satisfying object. Each argument's contextual type is instantiated over that union.

Fix

Setting InferenceFlagsReturnMapper disables constraint filtering only for the return mapper. No existing baselines change, and #64690 stays fixed. Reverting the priority instead reopens #64690.

before #65617 main with fix
mikro-orm tests/ instantiations 16.9M 37.1M 16.8M
mikro-orm wall time, median of 6 16.5 s 18.5 s 16.4 s
mikro-orm TS2589 no yes no
new test contextualReturnTypeInferenceConstraintFallback (the issue's repro with 6 types) TS2590 no errors

@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Dec 5, 2026
@maschwenk
Max Schwenk (maschwenk) marked this pull request as ready for review December 5, 2026 05:50
Copilot AI balanced review requested due to automatic review settings December 5, 2026 05:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Domain: Performance Performance related For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

#65617 causes excessive instantiations and TS2589/TS2590 in nested generic calls

3 participants