Sub topic 4
Choosing a Discriminant Property
Assume you know basic TypeScript types, object types, optional properties, functions, if statements, and switch statements. This chapter focuses on choosing the property that lets TypeScript identify one member of a union.
A discriminant property is a shared property whose value identifies a particular union member. The property is usually called a tag. Each member gives the tag a different string literal type.
What makes a good discriminant
A good discriminant has these properties:
- Every union member has the property.
- The property is required, not optional.
- Each member uses a different literal value.
- The value describes the variant, not ordinary data.
- The property has the same name and compatible type in every member.
For example, an event can use kind as its discriminant:
type Event =
| { kind: "click"; x: number; y: number }
| { kind: "keypress"; key: string }
| { kind: "resize"; width: number; height: number };All three members have kind, but each member has a different literal value. The values "click", "keypress", and "resize" are literal types. A literal type accepts exactly that value.
When TypeScript checks event.kind, it can connect the tag value to the rest of the object. After checking for "click", it knows that x and y exist.
Choose a stable, meaningful tag
Prefer a property such as kind, type, status, or state when it clearly names the variant. The name should communicate what the alternatives represent.
function describeEvent(event: Event): string {
switch (event.kind) {
case "click":
return `Click at (${event.x}, ${event.y})`;
case "keypress":
return `Key: ${event.key}`;
case "resize":
return `${event.width}x${event.height}`;
}
}Do not use a property that can overlap between variants. For example, width is data for a resize event, not a reliable tag. A click event could also gain a width property later, which would make the union harder to narrow.
The tag should also remain stable throughout the value's lifetime. If code changes kind after construction, the other properties might no longer match the tag.
| Design | Example | Result |
|---|---|---|
| Good tag | `kind: "click"` | Clearly identifies a variant |
| Weak tag | `hasError: boolean` | Often grows into many combinations |
| Bad tag | Optional `kind?: "click"` | Missing values cannot identify a member |
Avoid optional or overlapping tags
An optional discriminant does not identify every value:
type Message =
| { kind?: "text"; body: string }
| { kind: "image"; url: string };A message with no kind could match the first member, but the tag does not provide a dependable choice. Make the property required instead.
Boolean tags can work for exactly two variants:
type SaveResult =
| { ok: true; id: string }
| { ok: false; message: string };For more than two variants, distinct string literals usually communicate intent better than several booleans. Multiple booleans can represent combinations that your application does not support, such as { loading: true, failed: true }.
Check the boundary between variants
Before choosing a tag, list the states or object shapes your program supports. Then ask:
- Can every value have exactly one variant?
- Does each variant need different fields?
- Can one required property name the variant clearly?
- Are the tag values distinct and unlikely to change meaning?
If the answers are yes, a discriminated union is usually a good fit. If the variants overlap heavily, consider whether separate interfaces, a common base type, or a different state model would express the data more accurately.
Exercise
What is wrong with this union: type Result = { success: boolean; value?: string; error?: string }?
Show answer
The boolean and optional properties allow invalid combinations, such as success with an error or failure with no error. Separate members with literal tags, such as { status: "success"; value: string } and { status: "error"; error: string }, make each valid shape explicit.