discriminated union

A type made of several object shapes that share one literal field, such as status or type, whose value tells them apart.

In more detail

When code checks that field in an if or switch, languages such as TypeScript narrow the value to the matching shape, so they know data exists only in the success case. It lets the compiler reject impossible states and unhandled cases.

Example

type State = { status: "idle" } | { status: "success"; data: string };

Related terms

Taught in

References