Skip to content

    ↑↓ move · ⏎ open · esc close

    Unions

    Extract

    Keeps from a union only the members assignable to `U`. The complement of `Exclude`.

    What it is

    The same distributive conditional with the branches swapped. Its most useful application is not filtering primitives but re-narrowing a discriminated union: given a union of shapes and a discriminant, `Extract` gets you the one member you meant.

    Examples

    • Extract<'a' | 'b' | 'c', 'a' | 'c'>
      "a" | "c"
    • Extract<string | number | boolean, string | boolean>
      string | boolean
    • Extract<Shape, { kind: 'circle' }>
      { kind: "circle"; r: number; }

      This is the one that earns its keep: pulling a single member back out of a discriminated union by its tag.

    Each resolved type above was printed by TypeScript 5.9.3, not written by hand.

    What it does not do

    • It does not narrow a value. `Extract` operates on types; narrowing a variable at run time still needs a check the compiler can follow.
    • It does not error when nothing matches — it gives you `never`, quietly, and the mistake surfaces somewhere else.

    Takeaway

    `Extract<Union, { tag: "x" }>` is the type-level version of the narrowing you already write with `if`. Reach for it when a generic needs one member of a union it was handed.