Skip to content

Relating large unions of literal types performs poorlyΒ #53191

Closed
@ahejlsberg

Description

@ahejlsberg

πŸ•— Version & Regression Information

All versions.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Digits = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type T9XXXX = `9${Digits}${Digits}${Digits}${Digits}`;
type TXXXXX = `${Exclude<Digits, '0'>}${Digits}${Digits}${Digits}${Digits}`;

let x: Exclude<TXXXXX, T9XXXX> = '88888';

πŸ™ Actual behavior

The example above takes ~45s to compile on a reasonably fast machine. The underlying issue is that as we relate large unions of primitive types, we have a fast path for matches, but not for mismatches. Because the majority of the constituents of TXXXXX are not present in T9XXXX, we spend a lot of time in the slow path that attempts to find matches by iterating through all constituents.

πŸ™‚ Expected behavior

We can do much better by implementing a fast path for mismatches.

Metadata

Metadata

Assignees

Labels

Domain: PerformanceReports of unusually slow behaviorFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions