Pick

This utility type constructs a type by picking the set of properties specified by the keys K from the type T. It’s useful when we want to create a new type by selecting only a few properties from an existing type.

Syntax:

Pick<T, K>

T: Represents the type from which properties will be picked.

K: Represents the keys of T that will be picked.

Example: In this example we are using Pick utility type to picky only name and email properties from existing type.

JavaScript
interface User {
    name: string;
    age: number;
    email: string;
}

type UserSummary = Pick<User, 'name' | 'email'>;

const userSummary: UserSummary = { name: 'ram', email: 'ram@example.com' };
console.log(userSummary)

Output:

{ name: 'ram', email: 'ram@example.com' }

TypeScript Utility Types

TypeScript utility types are built-in tools that help simplify working with types in TypeScript. They offer shortcuts for common type transformations like making properties optional, extracting certain properties, or filtering out specific types.

Similar Reads

Partial

Partial utility type constructs a type with all properties of the provided type T set to optional. It’s useful when we want to create a new type where all properties can be present but aren’t required....

Required

In contrast to Partial, Required utility type constructs a type where all properties of the provided type T are set to required. This is useful when we want to ensure that all properties must be present....

Readonly

This utility type constructs a type where all properties of the provided type T are marked as readonly. It’s beneficial for ensuring immutability....

Pick

This utility type constructs a type by picking the set of properties specified by the keys K from the type T. It’s useful when we want to create a new type by selecting only a few properties from an existing type....

Parameters

Parameters utility type obtains the parameter types of a function type T as a tuple type. It’s beneficial when we need to extract the parameter types of a function for further type manipulation....

Record

Record utility type constructs an object type whose property keys are of type K and values are of type T. It’s useful when you want to define a fixed set of keys with specific value types....

Exclude

The Exclude utility type constructs a new type by excluding from T all properties that are assignable to U....