Skip to content

Commit 298282d

Browse files
authored
Enum.A & "a" should be Enum.A and not never (#65658)
1 parent 3713de0 commit 298282d

5 files changed

Lines changed: 258 additions & 3 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26379,6 +26379,19 @@ func (c *Checker) getEnumLiteralType(value any, enumSymbol *ast.Symbol, symbol *
2637926379
return t
2638026380
}
2638126381

26382+
// Look up the plain literal that contains this enum member without creating a new type.
26383+
func (c *Checker) getMatchingNonEnumLiteral(t *Type) *Type {
26384+
if t.flags&TypeFlagsEnumLiteral != 0 {
26385+
switch {
26386+
case t.flags&TypeFlagsStringLiteral != 0:
26387+
return c.stringLiteralTypes[getStringLiteralValue(t)]
26388+
case t.flags&TypeFlagsNumberLiteral != 0:
26389+
return c.numberLiteralTypes[getNumberLiteralValue(t)]
26390+
}
26391+
}
26392+
return nil
26393+
}
26394+
2638226395
func isLiteralType(t *Type) bool {
2638326396
if t.flags&TypeFlagsBoolean != 0 {
2638426397
return true
@@ -27282,6 +27295,18 @@ func (c *Checker) addTypeToIntersection(typeSet *orderedSet[*Type], includes Typ
2728227295
}
2728327296
if !typeSet.contains(t) {
2728427297
if t.flags&TypeFlagsUnit != 0 && includes&TypeFlagsUnit != 0 {
27298+
// An enum member is a subtype of the plain literal with the same value.
27299+
if flags&TypeFlagsEnumLiteral != 0 {
27300+
if literal := c.getMatchingNonEnumLiteral(t); literal != nil && typeSet.replace(literal, t) {
27301+
return includes | flags&TypeFlagsIncludesMask
27302+
}
27303+
} else if flags&TypeFlagsStringOrNumberLiteral != 0 && includes&TypeFlagsEnumLiteral != 0 {
27304+
for _, existing := range typeSet.values {
27305+
if c.getMatchingNonEnumLiteral(existing) == t {
27306+
return includes
27307+
}
27308+
}
27309+
}
2728527310
// We have seen two distinct unit types which means we should reduce to an
2728627311
// empty intersection. Adding TypeFlags.NonPrimitive causes that to happen.
2728727312
includes |= TypeFlagsNonPrimitive
@@ -27393,11 +27418,12 @@ func (c *Checker) intersectUnionsOfPrimitiveTypes(types []*Type) ([]*Type, bool)
2739327418
}
2739427419

2739527420
// Check that the given type has a match in every union. A given type is matched by
27396-
// an identical type, and a literal type is additionally matched by its corresponding
27397-
// primitive type, and missingType is matched by undefinedType (and vice versa).
27421+
// an identical type, a literal type by its corresponding primitive type, an enum member
27422+
// by its plain literal, and missingType by undefinedType (and vice versa).
2739827423
func (c *Checker) eachUnionContains(unionTypes []*Type, t *Type) bool {
27424+
literal := c.getMatchingNonEnumLiteral(t)
2739927425
for _, u := range unionTypes {
27400-
if !c.unionContainsType(u, t, true /*matchSymbol*/) {
27426+
if !c.unionContainsType(u, t, true /*matchSymbol*/) && (literal == nil || !containsType(u.Types(), literal)) {
2740127427
return false
2740227428
}
2740327429
}

‎tsc/internal/checker/utilities.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,19 @@ func (s *orderedSet[T]) add(value T) {
10241024
s.valuesByKey[value] = struct{}{}
10251025
}
10261026

1027+
// replace preserves order. The caller must ensure newValue is not already in the set.
1028+
func (s *orderedSet[T]) replace(oldValue T, newValue T) bool {
1029+
if !s.contains(oldValue) {
1030+
return false
1031+
}
1032+
s.values[slices.Index(s.values, oldValue)] = newValue
1033+
if s.valuesByKey != nil {
1034+
delete(s.valuesByKey, oldValue)
1035+
s.valuesByKey[newValue] = struct{}{}
1036+
}
1037+
return true
1038+
}
1039+
10271040
func getContainingFunctionOrClassStaticBlock(node *ast.Node) *ast.Node {
10281041
return ast.FindAncestor(node.Parent, ast.IsFunctionLikeOrClassStaticBlockDeclaration)
10291042
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//// [tests/cases/compiler/enumLiteralIntersections.ts] ////
2+
3+
=== enumLiteralIntersections.ts ===
4+
enum A {
5+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
6+
7+
One = 1,
8+
>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
9+
10+
Two = "2",
11+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
12+
}
13+
14+
const a: "2" = A.Two;
15+
>a : Symbol(a, Decl(enumLiteralIntersections.ts, 5, 5))
16+
>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
17+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
18+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
19+
20+
const b: 1 = A.One;
21+
>b : Symbol(b, Decl(enumLiteralIntersections.ts, 6, 5))
22+
>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
23+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
24+
>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
25+
26+
type Extant1 = A.One & 1;
27+
>Extant1 : Symbol(Extant1, Decl(enumLiteralIntersections.ts, 6, 19))
28+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
29+
>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
30+
31+
type Extant2 = A.Two & "2";
32+
>Extant2 : Symbol(Extant2, Decl(enumLiteralIntersections.ts, 8, 25))
33+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
34+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
35+
36+
type ExtantAny1 = A & 1;
37+
>ExtantAny1 : Symbol(ExtantAny1, Decl(enumLiteralIntersections.ts, 9, 27))
38+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
39+
40+
type ExtantAny2 = A & "2";
41+
>ExtantAny2 : Symbol(ExtantAny2, Decl(enumLiteralIntersections.ts, 10, 24))
42+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
43+
44+
const numericMember: Extant1 = A.One;
45+
>numericMember : Symbol(numericMember, Decl(enumLiteralIntersections.ts, 13, 5))
46+
>Extant1 : Symbol(Extant1, Decl(enumLiteralIntersections.ts, 6, 19))
47+
>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
48+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
49+
>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
50+
51+
const stringMember: Extant2 = A.Two;
52+
>stringMember : Symbol(stringMember, Decl(enumLiteralIntersections.ts, 14, 5))
53+
>Extant2 : Symbol(Extant2, Decl(enumLiteralIntersections.ts, 8, 25))
54+
>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
55+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
56+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
57+
58+
const numericEnum: ExtantAny1 = A.One;
59+
>numericEnum : Symbol(numericEnum, Decl(enumLiteralIntersections.ts, 15, 5))
60+
>ExtantAny1 : Symbol(ExtantAny1, Decl(enumLiteralIntersections.ts, 9, 27))
61+
>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
62+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
63+
>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
64+
65+
const stringEnum: ExtantAny2 = A.Two;
66+
>stringEnum : Symbol(stringEnum, Decl(enumLiteralIntersections.ts, 16, 5))
67+
>ExtantAny2 : Symbol(ExtantAny2, Decl(enumLiteralIntersections.ts, 10, 24))
68+
>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
69+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
70+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
71+
72+
const literalFirst: "2" & A.Two = A.Two;
73+
>literalFirst : Symbol(literalFirst, Decl(enumLiteralIntersections.ts, 18, 5))
74+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
75+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
76+
>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
77+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
78+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
79+
80+
const union: A & (0 | "2") = A.Two;
81+
>union : Symbol(union, Decl(enumLiteralIntersections.ts, 19, 5))
82+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
83+
>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
84+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
85+
>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12))
86+
87+
// @ts-expect-error
88+
const mismatch: A & (0 | "2") = A.One;
89+
>mismatch : Symbol(mismatch, Decl(enumLiteralIntersections.ts, 21, 5))
90+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
91+
>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
92+
>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0))
93+
>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8))
94+
95+
// @ts-expect-error
96+
const plainString: ExtantAny2 = "2";
97+
>plainString : Symbol(plainString, Decl(enumLiteralIntersections.ts, 23, 5))
98+
>ExtantAny2 : Symbol(ExtantAny2, Decl(enumLiteralIntersections.ts, 10, 24))
99+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//// [tests/cases/compiler/enumLiteralIntersections.ts] ////
2+
3+
=== enumLiteralIntersections.ts ===
4+
enum A {
5+
>A : A
6+
7+
One = 1,
8+
>One : A.One
9+
>1 : 1
10+
11+
Two = "2",
12+
>Two : A.Two
13+
>"2" : "2"
14+
}
15+
16+
const a: "2" = A.Two;
17+
>a : "2"
18+
>A.Two : A.Two
19+
>A : typeof A
20+
>Two : A.Two
21+
22+
const b: 1 = A.One;
23+
>b : 1
24+
>A.One : A.One
25+
>A : typeof A
26+
>One : A.One
27+
28+
type Extant1 = A.One & 1;
29+
>Extant1 : A.One
30+
>A : any
31+
32+
type Extant2 = A.Two & "2";
33+
>Extant2 : A.Two
34+
>A : any
35+
36+
type ExtantAny1 = A & 1;
37+
>ExtantAny1 : A.One
38+
39+
type ExtantAny2 = A & "2";
40+
>ExtantAny2 : A.Two
41+
42+
const numericMember: Extant1 = A.One;
43+
>numericMember : A.One
44+
>A.One : A.One
45+
>A : typeof A
46+
>One : A.One
47+
48+
const stringMember: Extant2 = A.Two;
49+
>stringMember : A.Two
50+
>A.Two : A.Two
51+
>A : typeof A
52+
>Two : A.Two
53+
54+
const numericEnum: ExtantAny1 = A.One;
55+
>numericEnum : A.One
56+
>A.One : A.One
57+
>A : typeof A
58+
>One : A.One
59+
60+
const stringEnum: ExtantAny2 = A.Two;
61+
>stringEnum : A.Two
62+
>A.Two : A.Two
63+
>A : typeof A
64+
>Two : A.Two
65+
66+
const literalFirst: "2" & A.Two = A.Two;
67+
>literalFirst : A.Two
68+
>A : any
69+
>A.Two : A.Two
70+
>A : typeof A
71+
>Two : A.Two
72+
73+
const union: A & (0 | "2") = A.Two;
74+
>union : A.Two
75+
>A.Two : A.Two
76+
>A : typeof A
77+
>Two : A.Two
78+
79+
// @ts-expect-error
80+
const mismatch: A & (0 | "2") = A.One;
81+
>mismatch : A.Two
82+
>A.One : A.One
83+
>A : typeof A
84+
>One : A.One
85+
86+
// @ts-expect-error
87+
const plainString: ExtantAny2 = "2";
88+
>plainString : A.Two
89+
>"2" : "2"
90+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
enum A {
5+
One = 1,
6+
Two = "2",
7+
}
8+
9+
const a: "2" = A.Two;
10+
const b: 1 = A.One;
11+
12+
type Extant1 = A.One & 1;
13+
type Extant2 = A.Two & "2";
14+
type ExtantAny1 = A & 1;
15+
type ExtantAny2 = A & "2";
16+
17+
const numericMember: Extant1 = A.One;
18+
const stringMember: Extant2 = A.Two;
19+
const numericEnum: ExtantAny1 = A.One;
20+
const stringEnum: ExtantAny2 = A.Two;
21+
22+
const literalFirst: "2" & A.Two = A.Two;
23+
const union: A & (0 | "2") = A.Two;
24+
// @ts-expect-error
25+
const mismatch: A & (0 | "2") = A.One;
26+
// @ts-expect-error
27+
const plainString: ExtantAny2 = "2";

0 commit comments

Comments
 (0)