Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
13 changes: 13 additions & 0 deletions tsc/internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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"

27 changes: 27 additions & 0 deletions tsc/testdata/tests/cases/compiler/enumLiteralIntersections.ts
Original file line number Diff line number Diff line change
@@ -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";
Loading