What is the Exclude utility type?
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> removes from union type T all members that are assignable to U. Implementation: type Exclude<T, U> = T extends U ? never : T — it distributes over the union and replaces matching members with never, which then collapses. Example: type Colors = "red" | "blue" | "green"; type NotBlue = Exclude<Colors, "blue">; — result is "red" | "green". Exclude any object type: type Primitives = Exclude<string | number | boolean | object, object>; — result is string | number | boolean. Compare with Omit: Exclude works on union types (removes union members); Omit works on object types (removes properties). Related: Extract<T, U> is the opposite — keeps only the members of T that ARE assignable to U. Use case: type EventKeys = Exclude<keyof HTMLElement, "style" | "className"> to create a subset of DOM event keys.
Common Mistake
Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real TypeScript project.