Skip to content

Narrow destructured discriminated unions when the discriminant has a default#63563

Open
DukeDeSouth wants to merge 1 commit into
microsoft:mainfrom
DukeDeSouth:fix/50139-discriminated-union-destructuring-defaults
Open

Narrow destructured discriminated unions when the discriminant has a default#63563
DukeDeSouth wants to merge 1 commit into
microsoft:mainfrom
DukeDeSouth:fix/50139-discriminated-union-destructuring-defaults

Conversation

@DukeDeSouth

@DukeDeSouth DukeDeSouth commented Jun 17, 2026

Copy link
Copy Markdown

A default on the discriminant of a destructured union disables narrowing for the sibling bindings - { isText = false, children }: Props leaves children as string | number in both branches. It now still narrows when the default can never fire (the property is required and non-undefined across the union). Optional or possibly-undefined discriminants, and siblings with their own default, stay unchanged. Added a conformance test covering both.

Fixes #50139

A default on a required discriminant binding no longer disables discriminant
narrowing of the sibling bindings. The default is ignored for narrowing only
when it can never apply (the property is present and non-undefined in every
union constituent), so optional or possibly-undefined discriminants keep their
previous sound behavior.

Fixes microsoft#50139
Copilot AI review requested due to automatic review settings June 17, 2026 04:18
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Jun 17, 2026
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jun 17, 2026
@typescript-automation

Copy link
Copy Markdown

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

1 similar comment
@typescript-automation

Copy link
Copy Markdown

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a regression test and compiler fix to ensure discriminant narrowing still works when a required discriminant is destructured with a default initializer, while preserving existing soundness boundaries for optional/possibly-undefined discriminants.

Changes:

  • Added a new conformance test covering narrowing behavior with required discriminants that have destructuring defaults (plus negative/soundness cases).
  • Updated checker.ts to allow dependent narrowing through binding elements with defaults only when the default can never apply (property always present and non-undefined across the declared union).
  • Added corresponding reference baselines (.types, .symbols, .errors.txt) for the new test.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/cases/conformance/controlFlow/dependentDestructuredVariablesWithDefaults.ts New regression test validating narrowing with required discriminants + defaults and guarding soundness boundaries.
tests/baselines/reference/dependentDestructuredVariablesWithDefaults.types Baseline updated to reflect the new test’s expected type/narrowing results.
tests/baselines/reference/dependentDestructuredVariablesWithDefaults.symbols Baseline updated to reflect expected symbol binding results for the new test.
tests/baselines/reference/dependentDestructuredVariablesWithDefaults.errors.txt Baseline updated to capture expected errors for unsound narrowing cases.
src/compiler/checker.ts Compiler change enabling dependent narrowing through destructured defaults when the default is provably never used.

@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Discriminated union parameter destructuring doesn't work if the fields have defaults

2 participants