Skip to content

FlashcardNot attempted

Typed reducers

Recall it before you reveal

How do you make useReducer actions type-safe?

Space to reveal
Show answer & explanation

Model actions as a discriminated union on type:

type Action = { type: 'ADD'; payload: Todo } | { type: 'DELETE'; payload: number }

Inside switch (action.type) TS narrows payload per case, and a default: assertNever(action) makes a missing case a compile error. dispatch({ type: 'ADD', payload: 5 }) won't compile.

Deep dive (read after you answer)

A reducer receives every action your component can dispatch, so its type has to describe all of them at once. The tool for that is a discriminated union: several object types that share one literal field (usually type), so TypeScript can tell them apart.

type Action =
| { type: 'add'; todo: Todo }
| { type: 'toggle'; id: number }
| { type: 'clear' };
function reducer(state: Todo[], action: Action): Todo[] {
switch (action.type) {
case 'add':
return [...state, action.todo]; // action.todo exists here
case 'toggle':
return state.map((t) => (t.id === action.id ? { ...t, done: !t.done } : t));
case 'clear':
return [];
}
}

Inside each case, TypeScript narrows action to the one member with that type, so reading action.id in the add case is an error. useReducer(reducer, []) infers the state and the dispatch type from the reducer, so a call like dispatch({ type: 'toggle' }) without an id won't compile.

Common mistakes:

  • Typing actions as { type: string; payload?: any }: narrowing is impossible and every payload is any.
  • Forgetting an exhaustiveness check. A default branch that passes action to a function taking never turns a newly added, unhandled action into a compile error.
  • Building action objects in variables without as const, which widens 'add' to string.