TS UNIONSChapter 2 · TS Unions & Enums
Discriminated Unions
A discriminated union is a union of object types that share one literal property, the tag, such as kind: "circle" or kind: "square". Checking the tag narrows to exactly one member, so each branch knows which properties exist. Add a never check in the default branch and TypeScript will flag any member you forget to handle.
Worked example
How it reads
kindis the tag every member shares- Inside
case "circle", onlyradiusis available - Assigning to
neverfails to compile if a case was missed

Cloud tip: Tagged unions are the TypeScript way to model states: loading, success, error. Each state carries only the data that makes sense for it.


