Discriminated union
A TypeScript type made of variants that share one literal field, so the compiler knows which variant you have.
For example, a request state is either { status: "loading" }, { status: "ok", data } or { status: "error", error }. Checking status narrows the type, so you cannot read data in the error branch. With an exhaustive switch, adding a new variant makes the compiler point at every place that does not handle it yet.
Related terms
- Runtime validation (Zod): Checking at run time that data from outside (requests, APIs, files, models) matches the type your code assumes.
- Structured outputs: The model’s answer is constrained to match a JSON schema you provide.
