4. Architecture, Testing & Tooling
Practice Lab 2: Typed State Reducer, Narrowing & Unit Tests
Combine Modules 3 and 4 in a hands-on lab: implement a pure state reducer handling discriminated union actions, narrow untrusted payloads, and verify every transition with automated checks.
2 min readUpdated 2026-12-01
In This Lesson
- Implement a pure immutable reducer handling discriminated action objects
- Validate and narrow untrusted payload inputs before updating state
- Verify every action branch and edge case with automated assertions
In React (useReducer), Redux, and TypeScript frontends, pure reducers ((state, action) => nextState) keep complex UI logic predictable and 100% unit-testable without needing a browser!
Your Mission
Implement boardReducer(state, action) in the playground below:
"ADD_TASK": validatestypeof action.title === "string"and non-empty trimmed title; appends{ id: action.id, title: action.title.trim(), status: "todo" }immutably."MOVE_TASK": updates only the matching task'sstatus("todo" | "doing" | "done") immutably."REMOVE_TASK": filters out the task with matchingaction.id.- Unknown
action.type: throwsnew Error("Unknown action").
JavaScriptLab 2: Pure State Reducer & Action Narrowing
Output
Click “Run Code” to execute this snippet right in your browser.