🔷 TypeScript Intermediate

What is the Extract utility type?

Why Interviewers Ask This

This tests whether you can apply TypeScript knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Extract<T, U> keeps from union type T only the members that are assignable to U — the opposite of Exclude. Implementation: type Extract<T, U> = T extends U ? T : never. Example: type StringsOnly = Extract<string | number | boolean, string | number>; — result is string | number. Finding matching object types: type Shapes = { kind: "circle" } | { kind: "rect" } | { kind: "triangle" }; type Circles = Extract<Shapes, { kind: "circle" }>; — result is { kind: "circle" }. Useful for filtering union types to only include members matching a certain pattern. Combined with Record and keyof, you can filter object keys by value type: type MethodKeys<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]; — extracts only keys whose values are functions. Extract and Exclude form a complementary pair for union type manipulation.

Pro Tip

This topic has TypeScript-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.