From 88d504927f65bf113c09d8550c5924810a3a3470 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 24 Nov 2026 07:20:11 -0800 Subject: [PATCH] Enum.A & "a" should be Enum.A and not never --- tsc/internal/checker/checker.go | 32 +++++- tsc/internal/checker/utilities.go | 13 +++ .../compiler/enumLiteralIntersections.symbols | 99 +++++++++++++++++++ .../compiler/enumLiteralIntersections.types | 90 +++++++++++++++++ .../compiler/enumLiteralIntersections.ts | 27 +++++ 5 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.types create mode 100644 tsc/testdata/tests/cases/compiler/enumLiteralIntersections.ts diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 8be5416119313..6ddf3a8d5ff96 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -26338,6 +26338,19 @@ func (c *Checker) getEnumLiteralType(value any, enumSymbol *ast.Symbol, symbol * return t } +// Look up the plain literal that contains this enum member without creating a new type. +func (c *Checker) getMatchingNonEnumLiteral(t *Type) *Type { + if t.flags&TypeFlagsEnumLiteral != 0 { + switch { + case t.flags&TypeFlagsStringLiteral != 0: + return c.stringLiteralTypes[getStringLiteralValue(t)] + case t.flags&TypeFlagsNumberLiteral != 0: + return c.numberLiteralTypes[getNumberLiteralValue(t)] + } + } + return nil +} + func isLiteralType(t *Type) bool { if t.flags&TypeFlagsBoolean != 0 { return true @@ -27241,6 +27254,18 @@ func (c *Checker) addTypeToIntersection(typeSet *orderedSet[*Type], includes Typ } if !typeSet.contains(t) { if t.flags&TypeFlagsUnit != 0 && includes&TypeFlagsUnit != 0 { + // An enum member is a subtype of the plain literal with the same value. + if flags&TypeFlagsEnumLiteral != 0 { + if literal := c.getMatchingNonEnumLiteral(t); literal != nil && typeSet.replace(literal, t) { + return includes | flags&TypeFlagsIncludesMask + } + } else if flags&TypeFlagsStringOrNumberLiteral != 0 && includes&TypeFlagsEnumLiteral != 0 { + for _, existing := range typeSet.values { + if c.getMatchingNonEnumLiteral(existing) == t { + return includes + } + } + } // We have seen two distinct unit types which means we should reduce to an // empty intersection. Adding TypeFlags.NonPrimitive causes that to happen. includes |= TypeFlagsNonPrimitive @@ -27352,11 +27377,12 @@ func (c *Checker) intersectUnionsOfPrimitiveTypes(types []*Type) ([]*Type, bool) } // Check that the given type has a match in every union. A given type is matched by -// an identical type, and a literal type is additionally matched by its corresponding -// primitive type, and missingType is matched by undefinedType (and vice versa). +// an identical type, a literal type by its corresponding primitive type, an enum member +// by its plain literal, and missingType by undefinedType (and vice versa). func (c *Checker) eachUnionContains(unionTypes []*Type, t *Type) bool { + literal := c.getMatchingNonEnumLiteral(t) for _, u := range unionTypes { - if !c.unionContainsType(u, t, true /*matchSymbol*/) { + if !c.unionContainsType(u, t, true /*matchSymbol*/) && (literal == nil || !containsType(u.Types(), literal)) { return false } } diff --git a/tsc/internal/checker/utilities.go b/tsc/internal/checker/utilities.go index 01abc2278b039..d8ef84a30e09d 100644 --- a/tsc/internal/checker/utilities.go +++ b/tsc/internal/checker/utilities.go @@ -1024,6 +1024,19 @@ func (s *orderedSet[T]) add(value T) { s.valuesByKey[value] = struct{}{} } +// replace preserves order. The caller must ensure newValue is not already in the set. +func (s *orderedSet[T]) replace(oldValue T, newValue T) bool { + if !s.contains(oldValue) { + return false + } + s.values[slices.Index(s.values, oldValue)] = newValue + if s.valuesByKey != nil { + delete(s.valuesByKey, oldValue) + s.valuesByKey[newValue] = struct{}{} + } + return true +} + func getContainingFunctionOrClassStaticBlock(node *ast.Node) *ast.Node { return ast.FindAncestor(node.Parent, ast.IsFunctionLikeOrClassStaticBlockDeclaration) } diff --git a/tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.symbols b/tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.symbols new file mode 100644 index 0000000000000..53366a0aab3cc --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.symbols @@ -0,0 +1,99 @@ +//// [tests/cases/compiler/enumLiteralIntersections.ts] //// + +=== enumLiteralIntersections.ts === +enum A { +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) + + One = 1, +>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) + + Two = "2", +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) +} + +const a: "2" = A.Two; +>a : Symbol(a, Decl(enumLiteralIntersections.ts, 5, 5)) +>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) + +const b: 1 = A.One; +>b : Symbol(b, Decl(enumLiteralIntersections.ts, 6, 5)) +>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) + +type Extant1 = A.One & 1; +>Extant1 : Symbol(Extant1, Decl(enumLiteralIntersections.ts, 6, 19)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) + +type Extant2 = A.Two & "2"; +>Extant2 : Symbol(Extant2, Decl(enumLiteralIntersections.ts, 8, 25)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) + +type ExtantAny1 = A & 1; +>ExtantAny1 : Symbol(ExtantAny1, Decl(enumLiteralIntersections.ts, 9, 27)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) + +type ExtantAny2 = A & "2"; +>ExtantAny2 : Symbol(ExtantAny2, Decl(enumLiteralIntersections.ts, 10, 24)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) + +const numericMember: Extant1 = A.One; +>numericMember : Symbol(numericMember, Decl(enumLiteralIntersections.ts, 13, 5)) +>Extant1 : Symbol(Extant1, Decl(enumLiteralIntersections.ts, 6, 19)) +>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) + +const stringMember: Extant2 = A.Two; +>stringMember : Symbol(stringMember, Decl(enumLiteralIntersections.ts, 14, 5)) +>Extant2 : Symbol(Extant2, Decl(enumLiteralIntersections.ts, 8, 25)) +>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) + +const numericEnum: ExtantAny1 = A.One; +>numericEnum : Symbol(numericEnum, Decl(enumLiteralIntersections.ts, 15, 5)) +>ExtantAny1 : Symbol(ExtantAny1, Decl(enumLiteralIntersections.ts, 9, 27)) +>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) + +const stringEnum: ExtantAny2 = A.Two; +>stringEnum : Symbol(stringEnum, Decl(enumLiteralIntersections.ts, 16, 5)) +>ExtantAny2 : Symbol(ExtantAny2, Decl(enumLiteralIntersections.ts, 10, 24)) +>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) + +const literalFirst: "2" & A.Two = A.Two; +>literalFirst : Symbol(literalFirst, Decl(enumLiteralIntersections.ts, 18, 5)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) +>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) + +const union: A & (0 | "2") = A.Two; +>union : Symbol(union, Decl(enumLiteralIntersections.ts, 19, 5)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>A.Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>Two : Symbol(A.Two, Decl(enumLiteralIntersections.ts, 1, 12)) + +// @ts-expect-error +const mismatch: A & (0 | "2") = A.One; +>mismatch : Symbol(mismatch, Decl(enumLiteralIntersections.ts, 21, 5)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>A.One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) +>A : Symbol(A, Decl(enumLiteralIntersections.ts, 0, 0)) +>One : Symbol(A.One, Decl(enumLiteralIntersections.ts, 0, 8)) + +// @ts-expect-error +const plainString: ExtantAny2 = "2"; +>plainString : Symbol(plainString, Decl(enumLiteralIntersections.ts, 23, 5)) +>ExtantAny2 : Symbol(ExtantAny2, Decl(enumLiteralIntersections.ts, 10, 24)) + diff --git a/tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.types b/tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.types new file mode 100644 index 0000000000000..76a5cd025f0d3 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/enumLiteralIntersections.types @@ -0,0 +1,90 @@ +//// [tests/cases/compiler/enumLiteralIntersections.ts] //// + +=== enumLiteralIntersections.ts === +enum A { +>A : A + + One = 1, +>One : A.One +>1 : 1 + + Two = "2", +>Two : A.Two +>"2" : "2" +} + +const a: "2" = A.Two; +>a : "2" +>A.Two : A.Two +>A : typeof A +>Two : A.Two + +const b: 1 = A.One; +>b : 1 +>A.One : A.One +>A : typeof A +>One : A.One + +type Extant1 = A.One & 1; +>Extant1 : A.One +>A : any + +type Extant2 = A.Two & "2"; +>Extant2 : A.Two +>A : any + +type ExtantAny1 = A & 1; +>ExtantAny1 : A.One + +type ExtantAny2 = A & "2"; +>ExtantAny2 : A.Two + +const numericMember: Extant1 = A.One; +>numericMember : A.One +>A.One : A.One +>A : typeof A +>One : A.One + +const stringMember: Extant2 = A.Two; +>stringMember : A.Two +>A.Two : A.Two +>A : typeof A +>Two : A.Two + +const numericEnum: ExtantAny1 = A.One; +>numericEnum : A.One +>A.One : A.One +>A : typeof A +>One : A.One + +const stringEnum: ExtantAny2 = A.Two; +>stringEnum : A.Two +>A.Two : A.Two +>A : typeof A +>Two : A.Two + +const literalFirst: "2" & A.Two = A.Two; +>literalFirst : A.Two +>A : any +>A.Two : A.Two +>A : typeof A +>Two : A.Two + +const union: A & (0 | "2") = A.Two; +>union : A.Two +>A.Two : A.Two +>A : typeof A +>Two : A.Two + +// @ts-expect-error +const mismatch: A & (0 | "2") = A.One; +>mismatch : A.Two +>A.One : A.One +>A : typeof A +>One : A.One + +// @ts-expect-error +const plainString: ExtantAny2 = "2"; +>plainString : A.Two +>"2" : "2" + diff --git a/tsc/testdata/tests/cases/compiler/enumLiteralIntersections.ts b/tsc/testdata/tests/cases/compiler/enumLiteralIntersections.ts new file mode 100644 index 0000000000000..890b6eb9471e2 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/enumLiteralIntersections.ts @@ -0,0 +1,27 @@ +// @strict: true +// @noEmit: true + +enum A { + One = 1, + Two = "2", +} + +const a: "2" = A.Two; +const b: 1 = A.One; + +type Extant1 = A.One & 1; +type Extant2 = A.Two & "2"; +type ExtantAny1 = A & 1; +type ExtantAny2 = A & "2"; + +const numericMember: Extant1 = A.One; +const stringMember: Extant2 = A.Two; +const numericEnum: ExtantAny1 = A.One; +const stringEnum: ExtantAny2 = A.Two; + +const literalFirst: "2" & A.Two = A.Two; +const union: A & (0 | "2") = A.Two; +// @ts-expect-error +const mismatch: A & (0 | "2") = A.One; +// @ts-expect-error +const plainString: ExtantAny2 = "2";