This example shows a state machine in TypeScript using discriminated unions. The machine has three states: 'idle', 'loading', and 'success'. Each state is an object with a 'type' property. The function nextState takes the current state and uses a switch on state.type to decide the next state. It moves from 'idle' to 'loading', then to 'success' with data, then back to 'idle'. The execution table traces each step, showing the current state, the matched type, the action taken, and the next state. The variable tracker shows how the 'state' variable changes after each step. Key moments explain why the 'type' property is important, what happens if a state is not handled, and why some states carry extra data. The visual quiz tests understanding of state types at steps, presence of extra data, and TypeScript's role in catching missing cases. The snapshot summarizes the pattern as a safe, clear way to manage state transitions using discriminated unions.