Warm-up1 / 1
Can you write this from memory?
Declare `id` that can be string or number
TypeScript unions (|) model values that could be one of several types. Intersections (&) combine types into a single shape that has all properties. Together they're how you build exact TypeScript type definitions.
This page covers the basics. For the full reference including discriminated unions and exhaustive checking patterns, see the union & intersection cheat sheet.
What You'll Practice: TypeScript Union & Intersection Types
Define union types with the | operatorCombine types with intersection (&)Build discriminated unions with literal discriminant propertiesHandle nullable types with union and narrowingApply exhaustive switch patterns with never
TypeScript Union & Intersection Types Sample Exercises
Example 1Difficulty: 1/5
Declare `values` as an array that can contain strings or numbers
let values: (string | number)[];Example 2Difficulty: 2/5
Create type `Direction` for movement in a 2D grid (4 cardinal directions)
type Direction = 'up' | 'down' | 'left' | 'right';Example 3Difficulty: 2/5
Create type Employee combining Person and { employeeId: string }
type Employee = Person & { employeeId: string };+ 3 more exercises
Quick Reference
TypeScript Union & Intersection Types Cheat Sheet →Copy-ready syntax examples for quick lookup