Skip to content

Commit 0a229d2

Browse files
Copilotjakebailey
andauthored
Use comparability for discriminant properties when narrowing types for 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>
1 parent ffb99e2 commit 0a229d2

File tree

5 files changed

+5
-145
lines changed

5 files changed

+5
-145
lines changed

internal/checker/flow.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,9 @@ func (c *Checker) narrowTypeBySwitchOnDiscriminant(t *Type, data *ast.FlowSwitch
11121112
if t.flags&TypeFlagsUndefined == 0 {
11131113
u = c.getRegularTypeOfLiteralType(c.extractUnitType(t))
11141114
}
1115-
return !slices.Contains(switchTypes, u)
1115+
return !slices.ContainsFunc(switchTypes, func(st *Type) bool {
1116+
return isUnitType(st) && c.areTypesComparable(st, u)
1117+
})
11161118
})
11171119
if caseType.flags&TypeFlagsNever != 0 {
11181120
return defaultType

testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.errors.txt

Lines changed: 0 additions & 65 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.errors.txt.diff

Lines changed: 0 additions & 69 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function action3(animal: Animal) {
186186
check(animal);
187187
>check(animal) : void
188188
>check : (p: never) => void
189-
>animal : Animal
189+
>animal : never
190190
}
191191
}
192192

testdata/baselines/reference/submodule/conformance/discriminatedUnionTypes4.types.diff

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,4 @@
6060
+>animal : { type: "dog"; bark: string; }
6161
>bark : string
6262

63-
break;
64-
@@= skipped -8, +8 lines =@@
65-
check(animal);
66-
>check(animal) : void
67-
>check : (p: never) => void
68-
->animal : never
69-
+>animal : Animal
70-
}
71-
}
63+
break;

0 commit comments

Comments
 (0)