Pick your bowler
Three styles. Only three. The captain picks one.
Cricket bowlers come in styles, and only a few of them. Fast rips through with pace. Spin loops the ball into the surface and asks it to turn. Medium is the workhorse — neither express nor tweaker, somewhere in between. There is no fourth thing called ‘mostly-fast’ or ‘sort-of-spin’.
TypeScript has a type for that. A union of literal types lists the only allowed values: 'fast' | 'spin' | 'medium'. Any string outside that set won't compile. The captain — and the type system — pick from a closed list.
Tap a style below. Then try the red button to pick a style that doesn't exist — the umpire will explain why it isn't allowed.
Three options, in one type.
The pipe symbol (|) means “or”. The whole type means “one of these three strings, and no others.”
type BowlerStyle = 'fast' | 'spin' | 'medium'; const style: BowlerStyle = 'fast'; // captain's pick — the union enforces it // TypeScript would catch this: // const wrong: BowlerStyle = 'mostly-fast'; // // Type '"mostly-fast"' is not assignable to // // type 'BowlerStyle'.
A union is a closed list.
BowlerStyle can hold only 'fast', 'spin', or 'medium'. Try assigning any other string and TypeScript stops you at compile time — no ‘mostly-fast’, no typos, no surprises in production.
Use a union of literal types whenever the answer comes from a fixed set of options — bowler styles, payment statuses, traffic-light colours, anything where ‘something else’ would be a bug.