Course contentsShow
← TypeScript
Lesson 689 of 1,79616. Utility TypesFree lesson

Extract<T, U> Fundamentals

How Extract keeps only the types that match, opposite of Exclude.

Extract<T, U> Fundamentals

What you'll learn: How Extract<T, U> keeps only the types that match, making it the opposite of Exclude<T, U>.


The Core Idea

While Exclude<T, U> removes matching types from a union, Extract<T, U> does the opposite—it keeps only the types that exist in both T and U. Think of it like a filter that says "show me only what these two unions have in common."

If Exclude is like removing unwanted items from a list, Extract is like highlighting only the items that appear on both lists.


How Extract Works

Extract<T, U> takes two type arguments:

  • T: The union you're filtering
  • U: The types you want to keep

It returns only the members of T that are also assignable to U.

type Status = "pending" | "success" | "error" | "loading";
type FinalStates = "success" | "error";

// Keep only statuses that are also final states
type CompletedStatus = Extract<Status, FinalStates>;
// Result: "success" | "error"

Here, Extract found the overlap between the two unions and returned only those shared types.


Extract vs Exclude

These two utilities are mirror images:

type AllColors = "red" | "blue" | "green" | "yellow";
type Warm = "red" | "yellow";

type WarmColors = Extract<AllColors, Warm>;     // "red" | "yellow"
type CoolColors = Exclude<AllColors, Warm>;     // "blue" | "green"

Notice how they partition the original union into matching and non-matching types.


Key Takeaway: Extract<T, U> keeps only the types from T that match U, making it perfect for finding the intersection of two type unions—the opposite of Exclude.