TypeScript Utility Types - Extract

| 2 min read

What is Extract?

The Extract utility type in TypeScript is used to construct a new type by extracting members from a union type that matches a certain criterion. In short, it is only getting a couple of specific values that you need.

type Extract<T, U> = T extends U ? T : never;

How Does Extract Work?

  • Example of extracting specific keys from a union:
type Role = "ADMIN" | "MANAGER" | "USER";
type RoleExtracted = Extract<Role, "ADMIN" | "MANAGER">;
  • Example of extracting function types from a union:
type Args = string | number | (() => string) | (() => number);
type FunctionArgs = Extract<Args, Function>;

Recap

The Extract utility type is a tool in TypeScript that allows you to create a new type by extracting specific members from a union type. It can be useful when you want to work with a subset of a union type. This makes your TypeScript code more flexible and maintainable by allowing you to work with specific subsets of a union type. Whether you’re pulling out specific keys from a union of literal types or extracting function types from a union of various types, Extract provides a way to refine and constrain your types as needed.

Resources

TS Handbook - Extract

Thank you!

Thank you for your time and for reading this!