Use comparability for discriminant properties when narrowing types for a default switch clause#2734
Merged
jakebailey merged 2 commits intomainfrom Feb 10, 2026
Conversation
…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
Contributor
There was a problem hiding this comment.
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.ContainsFuncwithareTypesComparablefor 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. |
sandersn
approved these changes
Feb 10, 2026
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>
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.
Port of TypeScript#61211.
Enum member types used as
caseexpressions weren't properly narrowing discriminated unions indefaultclauses 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.slices.Contains(switchTypes, u)withslices.ContainsFuncusingisUnitType(st) && areTypesComparable(st, u)innarrowTypeBySwitchOnDiscriminantProperty💡 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.