Published lesson · 30 minutes
Model safe states with unions
Objective: Represent a small workflow as a discriminated union and narrow each state before reading its fields.
Understand the idea
Model safe states with unions works best when the learner can explain the reason behind each step, not just copy a result. A discriminated union gives each state a literal tag and the fields that belong to it; a `switch` can then make impossible combinations visible.
A support request can be open with a message, resolved with a date, or waiting for a reply with the latest response. Treat the names and details as invented practice information; the example is for learning a method and does not describe a live service.
A dependable sequence is: Define a union with a `status` tag, handle each case in a function and let TypeScript check that every state is covered. Write down the important decision before you begin so another person can follow the reasoning.
Avoid a single object with many unrelated optional fields. A missing field should be impossible in a state that does not use it. Compare the result with the objective, then change one thing at a time if the first attempt does not work.
Add one new state and update the handler until the type checker reports no missing case. Explain one assumption that would need checking before anyone used a similar result outside the classroom.
Keep a short record of the choices you made and the evidence you used to check them. A useful practice result is understandable by another learner, stays within the stated scope and makes its limitations visible before anyone relies on it.
Worked examples
Model safe states with unions: practice example
Explore how model safe states with unions supports the lesson objective with a small synthetic example.
main.ts
type Request = { status: "open"; message: string } | { status: "resolved"; resolvedAt: string };
function label(request: Request): string { return request.status === "open" ? request.message : `Resolved ${request.resolvedAt}`; }
console.log(label({ status: "open", message: "Practice request" }));Practice tasks
Explore model safe states with unions
Use the worked example for “Model safe states with unions”. Change one small detail to suit a different synthetic community situation, then explain what you expected and how you checked whether the change still met the lesson objective.
Build a TypeScript practice example
Create a small practice result that demonstrates this objective: Represent a small workflow as a discriminated union and narrow each state before reading its fields. Use only synthetic information, keep the first version focused, and write down one limitation that a later version should address.
Review and explain the result
Review your “Model safe states with unions” result as if another learner will use it without your explanation. Identify one strength, one exact point that may be unclear, and a revision that would make the result easier to understand or use.
Model safe states with unions becomes easier when you define a union with a `status` tag, check the result and explain the choice to another learner.
Course overview