make illegal states unrepresentable
A design rule: shape your types so that invalid combinations, such as loading and error at once, cannot even be written.
In more detail
A type with separate optional loading, error and data fields allows nonsense combinations. A discriminated union allows only the real states, so the compiler rejects the impossible ones.
Related terms
- discriminated union — A type made of several object shapes that share one literal field, such as status or type, whose value tells them apart.
- state machine — A model in which something is always in exactly one named state, such as idle, loading, success or error, and only defined events move it to another.
Taught in
- Discriminated Unions: Tagged Variants And Pattern Matching — Production TypeScript
- Tuples: Fixed Length, Labeled Elements And Variadic Tuples — Production TypeScript