Q08. What is a discriminated union?

A discriminated union is a union of object types sharing a property whose literal value identifies the variant. Switching on that property narrows the object and supports exhaustive handling. It is useful for async states, commands, events, and API results.

A status field with loading, success, and error variants lets each case require the fields appropriate to that state.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Discriminated union
type Result = { ok: true; data: string } | { ok: false; error: string };
💡 The literal ok property identifies the variant.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

Which field most commonly distinguishes variants in a discriminated union?