DEV Community

Discussion on: Best ways to use "Extract" utility type in Typescript

Collapse
 
brense profile image
Rense Bakker

How does Extract differ from Pick?

Collapse
 
uttam_py profile image
Uttam Sharma

I had same quesiton :)

Collapse
 
arafat4693 profile image
Arafat

Extract creates a new type by selecting some of the original types, while Pick is used to create a new type by choosing some of the original properties.

Here's an example to illustrate the difference:

interface Person {
  name: string;
  age: number;
  email: string;
  phone: string;
}

// Extract a subset of types from Person
type PersonKeys = Extract<keyof Person, 'name' | 'email'>;
// Result: type PersonKeys = "name" | "email"

// Pick some properties from Person
type PersonInfo = Pick<Person, 'name' | 'age'>;
// Result: type PersonInfo = { name: string; age: number; }
Enter fullscreen mode Exit fullscreen mode

Some comments have been hidden by the post's author - find out more