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.
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.