CodeZettaInterview Hub

TypeScript · Type Narrowing · Junior · Best Practice

Discriminated unions — pattern?

Short Interview Answer

Shared literal tag field to distinguish variants for exhaustive switches.

Detailed Explanation

Preferred over boolean flags soup. Enables never checks for exhaustiveness.

Example

type Shape =
  | { kind: 'circle'; r: number }
  | { kind: 'square'; size: number };

Did you know this answer?