Union expansion limits
Concept and syntax
Cross products of large placeholder unions can exceed the compiler representation budget. This pinned compiler rejects a six-digit expansion of one million alternatives. Prefer a bounded smaller vocabulary or runtime validation with a broader type; limits are implementation-dependent.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Digit="0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";type Pair=`${Digit}${Digit}`; const p:Pair="42"; console.log(p);Expected output:
42Common mistake
Intentionally invalid; compile separately and do not execute.
type Digit="0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";type Huge=`${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}`;Expected diagnostic: TS2590. The cross product is too complex to represent. Use a narrower finite vocabulary or validate at runtime.
Practice and recap
Compare two and three digits; avoid increasing size indefinitely.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Union expansion limits — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Union expansion limits is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Digit="0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";type Pair=`${Digit}${Digit}`; const p:Pair="42"; console.log(p);Which diagnostic and explanation match the rejected example under the lesson settings?
type Digit="0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";type Huge=`${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}`;What is a better approach when an enormous numeric format expansion hits TS2590?