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

Distribution in Mapped Type Contexts

See how distributive conditionals interact with mapped types when iterating over keys or transforming properties.

Distribution in Mapped Type Contexts

What you'll learn: How distributive conditional types behave when used inside mapped types that iterate over object keys.

The Core Interaction

You've learned that conditional types distribute over unions by default, and you've learned how mapped types iterate over keys. But what happens when you combine them—when you use a distributive conditional inside a mapped type?

The key insight: When a conditional type appears in the value position of a mapped type, distribution happens for each property independently. The mapped type iterates over keys, and for each key's value type, the conditional distributes if that value is a union.

How It Works

Imagine you have an object type where some properties have union types:

type Data = {
  id: string | number;
  name: string;
  tags: string[] | null;
};

Now you create a mapped type that applies a conditional to each property's type:

type NonNull<T> = {
  [K in keyof T]: T[K] extends null ? never : T[K]
};

type Cleaned = NonNull<Data>;
// Result: { id: string | number, name: string, tags: string[] }

Notice what happened to tags: the conditional string[] | null extends null ? never : string[] | null distributed over the union! It checked each member:

  • string[] extends null ? never : string[] → string[]
  • null extends null ? never : null → never

The union string[] | never simplified to just string[].

Filtering Properties with Distribution

This pattern lets you transform objects based on their property types:

type StringsOnly<T> = {
  [K in keyof T]: T[K] extends string ? T[K] : never
};

type Mixed = { a: string; b: number; c: string | number };
type Filtered = StringsOnly<Mixed>;
// Result: { a: string, b: never, c: string }

For property c, the conditional distributed: it kept the string branch and discarded number, leaving just string.

Key Takeaway: Distributive conditionals inside mapped types distribute over each property's value type independently, letting you filter or transform union-typed properties while iterating over an object's keys.