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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Extract<Type, Union>

Parameters

ParameterDescription
Type The union to filter.
Union What members must be assignable to.

Return value

The matching union members.

Examples

Pick one variant of a unionTry it yourself
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 }
Keep function typesTry it yourself
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

VersionChange
TypeScript 2.8Added

Was this page helpful?