Repository navigation
Skip unrelated literal-keyed constituents when narrowing - #65639
Jake Bailey (jakebailey) wants to merge 3 commits into
Conversation
Narrowing a union by a type guard relates each constituent of the
narrowed type to each constituent of the candidate, with up to four
subtype checks per pair. With large unions of literals, such as the
4,000 template literal keys in 65620, that is millions of checks. When
one side is a branded literal like `"k.0001" & { value: T }`, each check
goes through a full structural relation and is cached, so the relation
cache overflows and every later check reports excessive complexity at
the same node.
Two literal types with different values, alone or intersected with
object types, are never subtypes of each other. Narrowing now skips
such pairs, and when a candidate constituent has a literal key, it maps
only the constituents with the same value and those without a literal
key, instead of mapping over the whole union.
Fixes 65620
|
TypeScript Bot (@typescript-bot) test it |
There was a problem hiding this comment.
🟡 Changes recommended
The narrowing optimization incorrectly drops NaN-valued enum members, including those in branded intersections, so it needs a NaN exclusion and regression coverage.
1 open issue
What changed in this PR
This PR optimizes type-guard narrowing in the compiler to avoid excessive subtype checks on large literal unions, addressing the performance issue reported in #65620.
Changes:
- Skips comparisons between literal-keyed types with different values.
- Indexes union constituents to limit checks to matching literal values and unkeyed types.
- Adds regression tests for branded-literal narrowing and a 4,000-member union.
| File | Description |
|---|---|
tsc/internal/checker/flow.go |
Adds literal-key filtering and indexed narrowing. |
tsc/testdata/tests/cases/compiler/narrowingLargeLiteralUnionByBrandedLiterals.ts |
Reproduces the large-union performance issue. |
tsc/testdata/tests/cases/compiler/narrowingByLiteralKeyedTypes.ts |
Covers narrowing across literals, enums, generics, and objects. |
tsc/testdata/baselines/reference/compiler/narrowingLargeLiteralUnionByBrandedLiterals.symbols |
Records symbols for the large-union regression. |
tsc/testdata/baselines/reference/compiler/narrowingByLiteralKeyedTypes.types |
Records expected narrowed types. |
tsc/testdata/baselines/reference/compiler/narrowingByLiteralKeyedTypes.symbols |
Records symbols for the narrowing cases. |
Review effort: Balanced
|
Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready. Everything looks the same! |
|
Jake Bailey (@jakebailey) Here are the results of running the user tests with tsc comparing Everything looks good! |
|
Jake Bailey (@jakebailey) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Narrowing `N` with an `x is N.A | N.B` guard, where `N.A = NaN`, drops `N.A`: NaN is equal to no value, itself included, so the NaN literal type never matches its own literal key.
NaN is equal to no value, itself included, so a NaN-valued enum member never matched its own key. A NaN literal type now goes through the relation checks as other types without a literal key do.
0fce222 to
c611fc6
Compare
|
Disappointingly not a perf win, but, oh well |
|
Jake Bailey (@jakebailey) Here are the results of running the top 400 repos with tsc comparing Everything looks good! |

This was found as part of #65064, surprisingly.
Narrowing a union by a type guard relates each constituent of the narrowed type to each constituent of the candidate, with up to four subtype checks per pair. With large unions of literals, such as the 4,000 template literal keys in #65620, that is millions of checks. When one side is a branded literal like
"k.0001" & { value: T }, each check goes through a full structural relation and is cached, so the relation cache overflows and every later check reports excessive complexity at the same node.Two literal types with different values, alone or intersected with object types, are never subtypes of each other. Narrowing now skips such pairs, and when a candidate constituent has a literal key, it maps only the constituents with the same value and those without a literal key, instead of mapping over the whole union.
Fixes #65620