Typed event and state system
Concept and boundary
Use discriminated unions to correlate event kind and payload, and a transition function to encode valid state changes. Decode external messages before dispatch. Invalid transitions are rejected and the old state is preserved.
Accepted project
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. For the AST explorer only, install the pinned typescript package locally (npm install --save-dev typescript@5.8.3).
import {decode,step} from "./state"; const start={kind:"idle"} as const; const event=decode({kind:"start",id:7}); if(event){const next=step(start,event);console.log(next?.kind,start.kind);console.log(step(start,{kind:"finish"})===undefined)} console.log(decode({kind:"start",id:"7"})===undefined);export type State={kind:"idle"}|{kind:"running";id:number}|{kind:"done"}; export type Event={kind:"start";id:number}|{kind:"finish"}; export function decode(x:unknown):Event|undefined{if(typeof x!=="object"||x===null||!("kind" in x))return;if(x.kind==="finish")return {kind:"finish"};if(x.kind==="start"&&"id" in x&&typeof x.id==="number")return {kind:"start",id:x.id}} export function step(s:State,e:Event):State|undefined{if(s.kind==="idle"&&e.kind==="start")return {kind:"running",id:e.id};if(s.kind==="running"&&e.kind==="finish")return {kind:"done"}}tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [] }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node main.js. Expected stdout:
running idletruetrueRejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
import {step} from "./state"; step({kind:"idle"},{kind:"start",id:"7"});From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
The start event requires a numeric id. Restore the accepted configuration/source to compare the result.
Practice and recap
Add cancel with explicit allowed source states and a malformed-message test.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Typed event and state system — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Typed event and state system?
Using the complete accepted project and the exact command in the lesson, which result is expected?
import {decode,step} from "./state"; const start={kind:"idle"} as const; const event=decode({kind:"start",id:7}); if(event){const next=step(start,event);console.log(next?.kind,start.kind);console.log(step(start,{kind:"finish"})===undefined)} console.log(decode({kind:"start",id:"7"})===undefined);Why does the rejected project fail under its stated configuration?
import {step} from "./state"; step({kind:"idle"},{kind:"start",id:"7"});