Skip to content

Use comparability for discriminant properties when narrowing types for a default switch clause#2734

Merged
jakebailey merged 2 commits intomainfrom
copilot/port-comparability-for-default-switch
Feb 10, 2026
Merged

Use comparability for discriminant properties when narrowing types for a default switch clause#2734
jakebailey merged 2 commits intomainfrom
copilot/port-comparability-for-default-switch

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 10, 2026

Port of TypeScript#61211.

Enum member types used as case expressions weren't properly narrowing discriminated unions in default clauses because the check used strict equality instead of type comparability. For example, AnimalType.cat (enum type) wasn't matched against "cat" (string literal type), so the union was never fully exhausted.

  • Replace slices.Contains(switchTypes, u) with slices.ContainsFunc using isUnitType(st) && areTypesComparable(st, u) in narrowTypeBySwitchOnDiscriminantProperty
return !slices.ContainsFunc(switchTypes, func(st *Type) bool {
    return isUnitType(st) && c.areTypesComparable(st, u)
})

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…r a default switch clause

Port of TypeScript PR #61211. Changes slices.Contains to slices.ContainsFunc
with a comparability check in the default clause narrowing of switch statements,
so that enum values used as case expressions properly narrow discriminated unions.

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Port comparability for discriminant properties in switch clauses Use comparability for discriminant properties when narrowing types for a default switch clause Feb 10, 2026
Copilot AI requested a review from jakebailey February 10, 2026 22:50
@jakebailey jakebailey marked this pull request as ready for review February 10, 2026 22:52
Copilot AI review requested due to automatic review settings February 10, 2026 22:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Ports TypeScript#61211 to fix discriminated-union exhaustion in default switch clauses when case expressions are enum member types, by using type comparability rather than strict equality.

Changes:

  • Update switch-based discriminant narrowing to use slices.ContainsFunc with areTypesComparable for membership checks.
  • Refresh conformance baselines to reflect correct narrowing to never (and removal of the prior error output).
  • Update baseline diff artifacts accordingly.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.types.diff Baseline diff updated to reflect new narrowing behavior in default.
testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.types Baseline updated: animal correctly narrows to never in default.
testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.errors.txt.diff Baseline diff updated to show the error is no longer produced.
testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.errors.txt Error baseline removed since narrowing now exhausts the union.
internal/checker/flow.go Core logic change: replace strict-equality membership with comparability-based predicate.

@jakebailey jakebailey enabled auto-merge February 10, 2026 23:24
@jakebailey jakebailey added this pull request to the merge queue Feb 10, 2026
Merged via the queue into main with commit 0a229d2 Feb 10, 2026
26 checks passed
@jakebailey jakebailey deleted the copilot/port-comparability-for-default-switch branch February 10, 2026 23:50
Copilot AI added a commit that referenced this pull request Feb 25, 2026
…r a default switch clause (#2734)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants