Skip to content

Narrow generic conditional and indexed access return types when checking return statements - #64230

Draft
Gabriela Araujo Britto (gabritto) wants to merge 3 commits into
mainfrom
gabritto/retcond
Draft

Narrow generic conditional and indexed access return types when checking return statements#64230
Gabriela Araujo Britto (gabritto) wants to merge 3 commits into
mainfrom
gabritto/retcond

Conversation

@gabritto

@gabritto Gabriela Araujo Britto (gabritto) commented Sep 10, 2026

Copy link
Copy Markdown
Member

Port of #56941 + #61359.

Pending: inferring conditional return types.

Fixes #33912.
Fixes #33014.

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

Type-reference wrappers and one indexed-access branch can currently produce unsound accepted returns.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Ports conditional and indexed-access return-type narrowing into the native TypeScript checker.

Changes:

  • Adds control-flow-aware return checking and narrowing constraints.
  • Records branch flow nodes for conditional expressions.
  • Adds comprehensive compiler and Fourslash regression coverage.
File summaries
File Description
tools/scripts/tsc/ast.json Adds conditional branch flow fields.
tsc/internal/ast/ast_generated.go Regenerates conditional-expression AST.
tsc/internal/binder/binder.go Captures return-position branch flow.
tsc/internal/checker/checker.go Implements return-type narrowing.
tsc/internal/checker/types.go Adds narrowing metadata and links.
tsc/internal/fourslash/tests/returnTypeNarrowingAfterCachingTypes_test.go Tests cached-type behavior.
tsc/testdata/tests/cases/compiler/dependentReturnType1.ts Core narrowing scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType2.ts JavaScript/JSDoc scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType3.ts Real-world TypeScript scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType4.ts Optional-parameter scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType5.ts Indexed-access scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType6.ts Narrowing eligibility scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType8.ts Expression-bodied arrow scenario.
tsc/testdata/tests/cases/compiler/dependentReturnType9.ts Switch exhaustiveness scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType10.ts Primitive/object union scenario.
tsc/testdata/tests/cases/compiler/dependentReturnType11.ts Embedded type-parameter scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType12.ts Non-strict optionality scenarios.
tsc/testdata/tests/cases/compiler/dependentReturnType13.ts Non-primitive restrictions.
tsc/testdata/baselines/reference/compiler/dependentReturnType1.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType1.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType1.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType2.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType2.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType2.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType3.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType3.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType3.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType4.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType4.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType4.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType5.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType5.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType5.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType6.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType6.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType6.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType8.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType8.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType9.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType9.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType9.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType10.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType10.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType11.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType11.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType11.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType12.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType12.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType12.types Expected types.
tsc/testdata/baselines/reference/compiler/dependentReturnType13.errors.txt Expected diagnostics.
tsc/testdata/baselines/reference/compiler/dependentReturnType13.symbols Expected symbols.
tsc/testdata/baselines/reference/compiler/dependentReturnType13.types Expected types.
Review details

Files not reviewed (1)

  • tsc/internal/ast/ast_generated.go: Generated file
  • Files reviewed: 51/52 changed files
  • Comments generated: 2
  • Review effort level: Balanced

Comment thread tsc/internal/checker/checker.go
Comment thread tsc/testdata/tests/cases/compiler/dependentReturnType5.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

2 participants