object-transforms
Extract<T, U>
Keeps only those members of union T that are assignable to U. The dual of Exclude, giving the set-intersection T ∩ U.
type Extract<T, U> = T extends U ? T : never Usage patterns
| Pattern | Purpose |
|---|---|
| Extract<T, string> | Pull only string members from a mixed union |
| Extract<Event, { type: 'click' }> | Filter discriminated union by discriminant |
| Extract<keyof T, string> | Restrict to string keys, dropping symbol/number keys |
| Extract<T, never> | Always never |
Examples
type All = 'a' | 'b' | 1 | 2;
type Numbers = Extract<All, number>; Result: 1 | 2
type Event = { type: 'click'; x: number } | { type: 'key'; k: string };
type ClickEvent = Extract<Event, { type: 'click' }>; Isolates one variant of a discriminated union
type StrKeys<T> = Extract<keyof T, string>; Handy when iterating keys with Object.keys, which returns strings only
type X = Extract<'a' | 'b', 'c'>; Result: never — no members match
Gotcha
Like Exclude, Extract distributes over union members; wrap in a tuple to suppress. Passing a non-union yields either T or never with no in-between.
Related utility types
Exclude<T, U>
Filters a union T by removing every member that is assignable to U. Distributes over union members via the conditional type, producing the set-difference T \ U.
NonNullable<T>
Removes null and undefined from the type T. In TS 4.9+ it is implemented as `T & {}`, which drops nullish members without altering the rest.
Pick<T, K>
Builds a new type containing only the properties from T whose keys are in the union K. Ideal for narrowing large interfaces to the subset a function or component actually needs.
infer R
A keyword usable only inside the extends clause of a conditional type that introduces a fresh type variable inferred from the matched shape. It is the primitive that powers ReturnType, Parameters, InstanceType, Awaited, and countless custom utilities.
Partial<T>
Constructs a type where every property of T is set to optional. Useful for representing update payloads, patch objects, and configuration overrides where all fields may be omitted.
← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never