TypeScript 7 reference · Utility Types
TypeScript Extract<Type, Union> Utility Type
Keep only the members of a union that are assignable to another type. The opposite of Exclude.
TypeScript 7 Since 2.8
Syntax
Extract<Type, Union>
Parameters
| Parameter | Description |
|---|---|
Type |
The union to filter. |
Union |
What members must be assignable to. |
Return value
The matching union members.
Examples
type Shape = { kind: "circle"; r: number } | { kind: "square"; size: number } | { kind: "line" };
type Circle = Extract<Shape, { kind: "circle" }>;
const c: Circle = { kind: "circle", r: 2 };
console.log(c);
Output
{ kind: 'circle', r: 2 }type Mixed = string | number | (() => void);
type Fn = Extract<Mixed, Function>;
const f: Fn = () => console.log("called a function member");
f();
Output
called a function member
Version history
| Version | Change |
|---|---|
| TypeScript 2.8 | Added |
Related
Exclude<UnionType, ExcludedMembers>· Remove members from a union type. Exclude<'a' | 'b' | 'c', 'a'> is 'b' | 'c'.- TypeScript Utility Types · tutorial