Skip to content
Open
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
99 changes: 98 additions & 1 deletion tsc/internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/microsoft/TypeScript/tsc/internal/core"
"github.com/microsoft/TypeScript/tsc/internal/diagnostics"
"github.com/microsoft/TypeScript/tsc/internal/evaluator"
"github.com/microsoft/TypeScript/tsc/internal/jsnum"
"github.com/microsoft/TypeScript/tsc/internal/scanner"
"github.com/microsoft/TypeScript/tsc/internal/tracing"
"github.com/zeebo/xxh3"
Expand Down Expand Up @@ -886,6 +887,7 @@ func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue boo
if t.flags&TypeFlagsUnion != 0 {
keyPropertyName = c.getKeyPropertyName(t)
}
var literalIndex *literalKeyIndex
narrowedType := c.mapType(candidate, func(n *Type) *Type {
// If a discriminant property is available, use that to reduce the type.
matching := t
Expand All @@ -901,6 +903,7 @@ func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue boo
// because that is the asserted type, but t for `instanceof` because generics aren't reflected in
// prototype object types.
var mapType func(*Type) *Type
var directlyRelated *Type
if checkDerived {
mapType = func(t *Type) *Type {
switch {
Expand All @@ -912,7 +915,15 @@ func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue boo
return c.neverType
}
} else {
nKey := c.getLiteralKeyOfType(n)
mapType = func(t *Type) *Type {
// Literal-keyed types with different values are unrelated in both directions, so we skip
// the (potentially expensive) relationship checks.
if nKey != nil {
if tKey := c.getLiteralKeyOfType(t); tKey != nil && tKey.AsLiteralType().value != nKey.AsLiteralType().value {
return c.neverType
}
}
switch {
case c.isTypeStrictSubtypeOf(t, n):
return t
Expand All @@ -925,8 +936,21 @@ func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue boo
}
return c.neverType
}
// When the current type is a union with literal-keyed constituents, only the constituents without
// a literal key or with the same literal key value as the candidate constituent can be related. Use
// an index to find them, avoiding quadratic work when narrowing large unions of literals.
if nKey != nil && matching == t && t.flags&TypeFlagsUnion != 0 {
if literalIndex == nil {
literalIndex = c.newLiteralKeyIndex(t)
}
if related, ok := literalIndex.mapRelated(c, nKey, mapType); ok {
directlyRelated = related
}
}
}
if directlyRelated == nil {
directlyRelated = c.mapType(matching, mapType)
}
directlyRelated := c.mapType(matching, mapType)
if directlyRelated.flags&TypeFlagsNever == 0 {
return directlyRelated
}
Expand Down Expand Up @@ -963,6 +987,79 @@ func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue boo
return c.getIntersectionType([]*Type{t, candidate})
}

type literalKeyIndex struct {
byValue map[any][]*Type
unkeyed []*Type
}

func (c *Checker) newLiteralKeyIndex(t *Type) *literalKeyIndex {
index := &literalKeyIndex{byValue: make(map[any][]*Type)}
for _, constituent := range t.Types() {
if key := c.getLiteralKeyOfType(constituent); key != nil {
value := key.AsLiteralType().value
index.byValue[value] = append(index.byValue[value], constituent)
} else {
index.unkeyed = append(index.unkeyed, constituent)
}
}
return index
}

// mapRelated applies f to the constituents of the indexed union that may be related to a type with the given
// literal key; f is assumed to map all other constituents to never. When the constituents map to at most one
// distinct non-never type, mapRelated returns that type (or never) and true, which is what mapping f over the
// entire union produces. Otherwise, it returns false and the caller should map over the entire union.
func (index *literalKeyIndex) mapRelated(c *Checker, key *Type, f func(*Type) *Type) (*Type, bool) {
var result *Type
for _, constituents := range [2][]*Type{index.byValue[key.AsLiteralType().value], index.unkeyed} {
for _, constituent := range constituents {
if mapped := f(constituent); mapped.flags&TypeFlagsNever == 0 && mapped != result {
if result != nil {
return nil, false
}
result = mapped
}
}
}
if result == nil {
return c.neverType, true
}
return result, true
}

// getLiteralKeyOfType returns the string, number, or bigint literal type of t if t is such a literal type or an
// irreducible intersection of exactly one such literal type and object types (e.g. a branded literal type). In the
// subtype and strict subtype relations, types with literal keys of different values are never related. NaN is equal
// to no value, itself included, so a NaN literal type has no literal key.
func (c *Checker) getLiteralKeyOfType(t *Type) *Type {
if isLiteralWithKey(t) {
return t
}
if t.flags&TypeFlagsIntersection == 0 || c.getReducedType(t) != t {
return nil
}
var key *Type
for _, constituent := range t.Types() {
switch {
case isLiteralWithKey(constituent) && key == nil:
key = constituent
case constituent.flags&TypeFlagsObject == 0:
return nil
}
}
return key
}

func isLiteralWithKey(t *Type) bool {
switch {
case t.flags&(TypeFlagsStringLiteral|TypeFlagsBigIntLiteral) != 0:
return true
case t.flags&TypeFlagsNumberLiteral != 0:
return !t.AsLiteralType().value.(jsnum.Number).IsNaN()
}
return false
}

func (c *Checker) getInstanceType(constructorType *Type) *Type {
prototypePropertyType := c.getTypeOfPropertyOfType(constructorType, "prototype")
if prototypePropertyType != nil && !IsTypeAny(prototypePropertyType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//// [tests/cases/compiler/narrowingByLiteralKeyedTypes.ts] ////

=== narrowingByLiteralKeyedTypes.ts ===
declare function isBranded<T>(t: T): t is T & { __brand: true };
>isBranded : Symbol(isBranded, Decl(narrowingByLiteralKeyedTypes.ts, 0, 0))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 0, 27))
>t : Symbol(t, Decl(narrowingByLiteralKeyedTypes.ts, 0, 30))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 0, 27))
>t : Symbol(t, Decl(narrowingByLiteralKeyedTypes.ts, 0, 30))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 0, 27))
>__brand : Symbol(__brand, Decl(narrowingByLiteralKeyedTypes.ts, 0, 47))

declare function isType<T>(x: unknown): x is T;
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 1, 24))
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 1, 27))
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 1, 27))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 1, 24))

declare const s1: "a" | "b" | "c" | undefined;
>s1 : Symbol(s1, Decl(narrowingByLiteralKeyedTypes.ts, 3, 13))

if (isBranded(s1)) {
>isBranded : Symbol(isBranded, Decl(narrowingByLiteralKeyedTypes.ts, 0, 0))
>s1 : Symbol(s1, Decl(narrowingByLiteralKeyedTypes.ts, 3, 13))

s1;
>s1 : Symbol(s1, Decl(narrowingByLiteralKeyedTypes.ts, 3, 13))
}

declare const s2: "a" | "b" | string;
>s2 : Symbol(s2, Decl(narrowingByLiteralKeyedTypes.ts, 8, 13))

if (isType<"a" & { tag: 1 }>(s2)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>tag : Symbol(tag, Decl(narrowingByLiteralKeyedTypes.ts, 9, 18))
>s2 : Symbol(s2, Decl(narrowingByLiteralKeyedTypes.ts, 8, 13))

s2;
>s2 : Symbol(s2, Decl(narrowingByLiteralKeyedTypes.ts, 8, 13))
}

declare const n1: 1 | 2 | 3 | -0 | 1n | 2n;
>n1 : Symbol(n1, Decl(narrowingByLiteralKeyedTypes.ts, 13, 13))

if (isType<(1 & { tag: 1 }) | (0 & { tag: 0 }) | (2n & { tag: 2 })>(n1)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>tag : Symbol(tag, Decl(narrowingByLiteralKeyedTypes.ts, 14, 17))
>tag : Symbol(tag, Decl(narrowingByLiteralKeyedTypes.ts, 14, 36))
>tag : Symbol(tag, Decl(narrowingByLiteralKeyedTypes.ts, 14, 56))
>n1 : Symbol(n1, Decl(narrowingByLiteralKeyedTypes.ts, 13, 13))

n1;
>n1 : Symbol(n1, Decl(narrowingByLiteralKeyedTypes.ts, 13, 13))
}

enum E { A = "a", B = "b" }
>E : Symbol(E, Decl(narrowingByLiteralKeyedTypes.ts, 16, 1))
>A : Symbol(E.A, Decl(narrowingByLiteralKeyedTypes.ts, 18, 8))
>B : Symbol(E.B, Decl(narrowingByLiteralKeyedTypes.ts, 18, 17))

declare const e1: E | "a" | "c";
>e1 : Symbol(e1, Decl(narrowingByLiteralKeyedTypes.ts, 19, 13))
>E : Symbol(E, Decl(narrowingByLiteralKeyedTypes.ts, 16, 1))

if (isType<"a" & { tag: 1 }>(e1)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>tag : Symbol(tag, Decl(narrowingByLiteralKeyedTypes.ts, 20, 18))
>e1 : Symbol(e1, Decl(narrowingByLiteralKeyedTypes.ts, 19, 13))

e1;
>e1 : Symbol(e1, Decl(narrowingByLiteralKeyedTypes.ts, 19, 13))
}
if (isType<E.B & { tag: 1 }>(e1)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>E : Symbol(E, Decl(narrowingByLiteralKeyedTypes.ts, 16, 1))
>B : Symbol(E.B, Decl(narrowingByLiteralKeyedTypes.ts, 18, 17))
>tag : Symbol(tag, Decl(narrowingByLiteralKeyedTypes.ts, 23, 18))
>e1 : Symbol(e1, Decl(narrowingByLiteralKeyedTypes.ts, 19, 13))

e1;
>e1 : Symbol(e1, Decl(narrowingByLiteralKeyedTypes.ts, 19, 13))
}

declare const b1: ("a" & { x: 1 }) | ("b" & { x: 2 }) | ("c" & { y: 1 }) | "d";
>b1 : Symbol(b1, Decl(narrowingByLiteralKeyedTypes.ts, 27, 13))
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 27, 26))
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 27, 45))
>y : Symbol(y, Decl(narrowingByLiteralKeyedTypes.ts, 27, 64))

if (isType<("a" & { y: 1 }) | ("b" & { x: 2 }) | "c" | "d">(b1)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>y : Symbol(y, Decl(narrowingByLiteralKeyedTypes.ts, 28, 19))
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 28, 38))
>b1 : Symbol(b1, Decl(narrowingByLiteralKeyedTypes.ts, 27, 13))

b1;
>b1 : Symbol(b1, Decl(narrowingByLiteralKeyedTypes.ts, 27, 13))
}

function f<T extends "a" | "b">(x: "a" | "b" | "c" | T) {
>f : Symbol(f, Decl(narrowingByLiteralKeyedTypes.ts, 30, 1))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 32, 11))
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 32, 32))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 32, 11))

if (isType<("a" & T) | ("b" & { tag: 1 })>(x)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>T : Symbol(T, Decl(narrowingByLiteralKeyedTypes.ts, 32, 11))
>tag : Symbol(tag, Decl(narrowingByLiteralKeyedTypes.ts, 33, 35))
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 32, 32))

x;
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 32, 32))
}
else {
x;
>x : Symbol(x, Decl(narrowingByLiteralKeyedTypes.ts, 32, 32))
}
}

declare const o1: "a" | "b" | { kind: "a" } | { kind: "b" };
>o1 : Symbol(o1, Decl(narrowingByLiteralKeyedTypes.ts, 41, 13))
>kind : Symbol(kind, Decl(narrowingByLiteralKeyedTypes.ts, 41, 31))
>kind : Symbol(kind, Decl(narrowingByLiteralKeyedTypes.ts, 41, 47))

if (isType<"a" & { kind: "a" }>(o1)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>kind : Symbol(kind, Decl(narrowingByLiteralKeyedTypes.ts, 42, 18))
>o1 : Symbol(o1, Decl(narrowingByLiteralKeyedTypes.ts, 41, 13))

o1;
>o1 : Symbol(o1, Decl(narrowingByLiteralKeyedTypes.ts, 41, 13))
}

enum N { A = NaN, B = 1, C = 2 }
>N : Symbol(N, Decl(narrowingByLiteralKeyedTypes.ts, 44, 1))
>A : Symbol(N.A, Decl(narrowingByLiteralKeyedTypes.ts, 46, 8))
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --))
>B : Symbol(N.B, Decl(narrowingByLiteralKeyedTypes.ts, 46, 17))
>C : Symbol(N.C, Decl(narrowingByLiteralKeyedTypes.ts, 46, 24))

declare const n2: N;
>n2 : Symbol(n2, Decl(narrowingByLiteralKeyedTypes.ts, 47, 13))
>N : Symbol(N, Decl(narrowingByLiteralKeyedTypes.ts, 44, 1))

if (isType<N.A | N.B>(n2)) {
>isType : Symbol(isType, Decl(narrowingByLiteralKeyedTypes.ts, 0, 64))
>N : Symbol(N, Decl(narrowingByLiteralKeyedTypes.ts, 44, 1))
>A : Symbol(N.A, Decl(narrowingByLiteralKeyedTypes.ts, 46, 8))
>N : Symbol(N, Decl(narrowingByLiteralKeyedTypes.ts, 44, 1))
>B : Symbol(N.B, Decl(narrowingByLiteralKeyedTypes.ts, 46, 17))
>n2 : Symbol(n2, Decl(narrowingByLiteralKeyedTypes.ts, 47, 13))

n2;
>n2 : Symbol(n2, Decl(narrowingByLiteralKeyedTypes.ts, 47, 13))
}

Loading
Loading