Typed action results
What you need to know
A discriminated union models a Server Action result as one of several explicit outcomes. It makes impossible combinations harder to construct and lets the interface handle each valid outcome clearly.
By the end, you can: model success and error variants; handle each branch.
Represent the result and render every branch
type Result = {kind:'idle'} | {kind:'invalid';message:string} | {kind:'saved';id:string};function describe(result:Result):string { switch(result.kind) { case 'idle': return 'Ready'; case 'invalid': return result.message; case 'saved': return `Saved ${result.id}`; default: { const impossible:never = result; return impossible; } }}console.log(describe({kind:'saved',id:'b7'}));Expected behavior: Saved b7. Adding another Result variant without handling it makes the exhaustive branch fail type checking.
How it works
Use a shared serializable state type such as {kind:'idle'}, {kind:'invalid',message:string} or {kind:'saved',id:string}. Narrow on the discriminant before reading variant fields; exhaustive switches can use never to expose unhandled variants. FormData entries may be strings, files or null, so a TypeScript annotation alone does not establish a valid input. Validate the runtime value before converting it. When wiring React useActionState, the action receives previous state before FormData; a plain form action does not receive that extra state argument. Keep the state union separate from the input parser. Define business validation independently of transport: a form and Route Handler may call the same pure parser. Do not return stack traces, database errors or secrets to the client. A conflict is an expected business result, not a successful save. If redirect is used after a successful write, handle it outside catch blocks that would swallow its control flow.
Validate an unknown title before building data
type Parsed = {ok:true;title:string} | {ok:false;message:string};function parseTitle(value:unknown):Parsed { if(typeof value !== 'string') return {ok:false,message:'Title required'}; const title=value.trim(); if(title.length<3 || title.length>60) return {ok:false,message:'Use 3–60 characters'}; return {ok:true,title};}console.log(parseTitle(' Fern '));Expected behavior: The result contains ok:true and title Fern. Null or a non-string returns the error variant.
Common mistake
Do not write {success:boolean,id?:string,error?:string} and assume id is always present on success. A discriminated union expresses the relationship; still parse external data at runtime.
Practice and verification
Extend Result with a conflict variant that includes the current revision. First observe the missing branch with tsc, then add a message. Test invalid and whitespace-only titles as well as a valid title.
Scope: App Router, proposed Next.js 16.x, React 19 family, Cache Components disabled. Framework snippets are static or explicitly partial; actual browser, auth and generated-route checks are pending. SQLite exercises run separately in the authoring environment, not the ByteMind evaluator.