🔷 TypeScript Intermediate

What is the Extract and Exclude difference and when to use each?

Why Interviewers Ask This

Mid-level TypeScript roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

Exclude<T, U> and Extract<T, U> are complementary utility types for filtering union members. Exclude removes from union T all members assignable to U: Exclude<"a" | "b" | "c", "a">"b" | "c". Think of it as a set difference: T minus U. Extract keeps from union T only members assignable to U: Extract<"a" | "b" | "c", "a" | "b">"a" | "b". Think of it as a set intersection: T ∩ U. Both use conditional types and distribute over union members. Use Exclude when: you have a broad union and want to remove specific members (e.g., remove null/undefined — though NonNullable is more idiomatic). Use Extract when: you want to keep only a specific subset of a union (e.g., extract only function types from a union). Remember: both work on unions, not object shapes — use Omit/Pick for object properties.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.