Course contentsShow
← TypeScript
Lesson 790 of 1,79618. Conditional TypesFree lesson

Practical Distribution Patterns

Apply distributive conditionals to real scenarios like event handler typing, action creators, and discriminated union helpers.

Practical Distribution Patterns

You'll learn: How to apply distributive conditional types to solve real-world typing challenges in event handlers, action creators, and discriminated union helpers.


The Core Idea

Now that you understand how conditional types distribute over unions, it's time to see where this truly shines in production code. Distributive conditionals excel at transforming each member of a union independently — perfect for type-safe event systems, Redux-style actions, and union manipulation utilities.


Event Handler Typing

Imagine you have multiple event types and need handlers that accept the correct payload for each:

type AppEvent =
  | { type: 'login'; userId: string }
  | { type: 'logout' }
  | { type: 'pageView'; url: string };

// Extract just the payload (without 'type')
type EventPayload<T> = T extends { type: string }
  ? Omit<T, 'type'>
  : never;

// Maps each event type to its handler signature
type EventHandler<T extends AppEvent> = (
  payload: EventPayload<T>
) => void;

// Distribution means this works for each union member:
const handleLogin: EventHandler<Extract<AppEvent, { type: 'login' }>> = 
  (payload) => {
    console.log(payload.userId); // ✓ knows userId exists
  };

The conditional distributes over AppEvent, extracting each event's unique payload structure.


Action Creator Typing

For Redux-like patterns, you can auto-generate action creators from action unions:

type Action =
  | { type: 'ADD_TODO'; text: string }
  | { type: 'REMOVE_TODO'; id: number }
  | { type: 'CLEAR_TODOS' };

type ActionCreator<A extends Action> = 
  A extends { type: string }
    ? (
        ...args: keyof Omit<A, 'type'> extends never 
          ? [] 
          : [Omit<A, 'type'>]
      ) => A
    : never;

// Each action type gets its own creator signature
type AddTodoCreator = ActionCreator<Extract<Action, { type: 'ADD_TODO' }>>;
// (args_0: { text: string }) => { type: 'ADD_TODO'; text: string }

Distribution lets you handle actions with and without payloads uniformly.


Discriminated Union Helpers

Create utilities that work across all variants:

type Result<T> =
  | { status: 'success'; value: T }
  | { status: 'error'; error: string };

// Extract all 'success' cases from any Result union
type SuccessCase<R> = Extract<R, { status: 'success' }>;

// Get the value type from success cases
type UnwrapSuccess<R> = SuccessCase<R> extends { value: infer V }
  ? V
  : never;

type MyResult = Result<number> | Result<string>;
type Values = UnwrapSuccess<MyResult>; // number | string

This pattern distributes over each Result<T>, extracting values type-safely.


Key Takeaway: Distributive conditionals power real-world patterns like event handler typing, action creator generation, and discriminated union utilities by transforming each union member independently while preserving type safety.