Union and intersection complexity
Concept and boundary
Union and intersection representations can affect checker work. A template literal cross product explicitly creates combinations, whereas a structured pair stores the components separately. This bounded fixture compares representable contracts, not a universal speed ranking.
Accepted project
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. Package stubs under node_modules are local teaching fixtures, not installed dependencies.
type A="a"|"b"|"c"; type B="1"|"2"|"3"; type Flat=`${A}${B}`; type Pair={a:A;b:B}; const f:Flat="a2"; const p:Pair={a:"a",b:"2"}; console.log(f===p.a+p.b);tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [], "extendedDiagnostics": true }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node main.js. Expected stdout:
trueCompiler output contains "Instantiations:".
Rejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
type A="a"|"b"|"c"; type B="1"|"2"|"3"; type Flat=`${A}${B}`; const f:Flat="d2";From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
The cross product does not contain d2. Restore the accepted configuration/source to compare the result.
Independent comparison: structured-pair
The structured representation avoids materializing a template-literal cross product. Compare the counters with the accepted mixed example, not a claim of universal speed.
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. Package stubs under node_modules are local teaching fixtures, not installed dependencies.
type A="a"|"b"|"c"; type B="1"|"2"|"3"; type Pair={a:A;b:B}; const p:Pair={a:"a",b:"2"}; console.log(p.a+p.b);tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [], "extendedDiagnostics": true }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node main.js. Expected stdout:
a2Compiler output contains "Types:".
Compiler output contains "Instantiations:".
Compiler output contains "Check time:".
Practice and recap
Measure both representations in separate equally configured projects.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Union and intersection complexity — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Union and intersection complexity?
Using the complete accepted project and the exact command in the lesson, which result is expected?
type A="a"|"b"|"c"; type B="1"|"2"|"3"; type Flat=`${A}${B}`; type Pair={a:A;b:B}; const f:Flat="a2"; const p:Pair={a:"a",b:"2"}; console.log(f===p.a+p.b);Why does the rejected project fail under its stated configuration?
type A="a"|"b"|"c"; type B="1"|"2"|"3"; type Flat=`${A}${B}`; const f:Flat="d2";What is the result of the independent structured-pair comparison?
type A="a"|"b"|"c"; type B="1"|"2"|"3"; type Pair={a:A;b:B}; const p:Pair={a:"a",b:"2"}; console.log(p.a+p.b);