# Discriminated Unions

> The most powerful pattern in TypeScript for state management.

Subject: TypeScript | Lesson 17 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/discriminated-unions
Previous: [Assertion Functions (asserts)](https://codewithanimation.in/typescript/assertion-functions.md) | Next: [Typing Functions](https://codewithanimation.in/typescript/typing-functions.md)

## The Tagged Union Pattern

A Discriminated Union (also known as a Tagged Union) is a union type made up of multiple object types that all share a single, common property with literal types (like a `type`, `kind`, or `status` string).

Because every object in the union has this shared property, TypeScript can use it to definitively identify which object shape it is currently looking at.

## Perfect State Management

This is the cornerstone of robust TypeScript applications. It's the pattern used by Redux actions, `useReducer` in React, and finite state machines.

Instead of having one giant object where everything is optional (`{ status: string, data?: Data, error?: Error }`), you create distinct, mutually exclusive states. If status is 'loading', `data` doesn't even exist on the type. This makes invalid states mathematically impossible.

## The Discriminant Property

```typescript
type State = 
  | { status: "loading" }
  | { status: "success"; data: string[] }
  | { status: "error"; message: string };

function renderUI(state: State) {
  if (state.status === "success") {
    // TS knows this MUST be the success object.
    // 'data' is safely available. 'message' does not exist.
    console.log(state.data.length);
  }
}
```

## Common mistake: The missing tag error

A discriminated union only works if *every single member* of the union shares the exact same property name.

If you have `type Action = { type: 'ADD' } | { action: 'REMOVE' }`, TypeScript cannot discriminate them safely because there is no single shared property to check.

## Tip: Switch Statements

Discriminated unions pair beautifully with `switch` statements. TypeScript will correctly narrow the type inside every `case` block, providing perfect autocompletion for that specific object.

## Try it: Try it

Select a state type and watch how TypeScript narrows the union, unlocking only the properties that belong to that specific state.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/discriminated-unions

## Check yourself

### Question 1

Why is a Discriminated Union better than a single object with optional properties?

- A. It compiles to faster JavaScript.
- B. It makes invalid states impossible by ensuring certain properties only exist when a specific status/tag is present.
- C. It is required for React components to render properly.

Correct answer: B

By defining distinct, mutually exclusive object shapes based on a tag, you eliminate the risk of accessing data when an error occurred, or checking an error message when the state is loading.

- Why not A: Types are erased at compile time; the resulting JS is the same.
- Why not C: It is useful in React, but not strictly required.

## Remember this

- A Discriminated Union requires a shared literal property across all objects (e.g., `status`).
- Checking that shared property narrows the union to one specific object shape.
- It prevents 'impossible states' by removing optional properties in favor of explicit state objects.
- It is the standard pattern for Redux actions and complex state management.
