TypeScript Pick<Type, Keys> Utility Type
Last Updated :
03 Sep, 2024
TypeScript’s Pick<Type, Keys> utility type allows you to create a new type by selecting specific properties (`Keys`) from an existing type (`Type`). This is useful for narrowing down types to only the relevant properties, enhancing type safety, and reducing redundancy in complex type definitions.
Syntax
type NewType = Pick<Type, Keys>;
- Type: It is the type from which you want to pick the properties.
- Keys: It is a union type of key that you want to pick.
Partials Vs Pick<Type, Keys>: Pick and Partial are two different utility types in TypeScript, although they have some similarities, while Pick is used to create a new type by selecting specific properties from an existing type, Partial is used to create a new type by making all properties of an existing type optional.
Approach: We can see how we can use the Pick utility type through this step-by-step approach for easy understanding.
Step 1: First of all we will need to define the original type that we want to pick properties from.
interface Person {
name: string;
age: number;
gender: string;
address: string;
}
Step 2: Now we will need to define the new type that you want to create using the Pick utility type, in this case, the new type is PersonDetails and the original type is Person and we want to pick the name and age properties from it.
type PersonDetails = Pick<Person, 'name' | 'age'>;
Step 3: And now we can use the new type PersonDetails in your code.
const person: PersonDetails = {
name: 'John',
age: 30,
};
Step 4: And finally we can use it as per our requirements.
Example 1: In this example, we define a Person interface and use the Pick utility type to create NameAndAge, selecting only name and age properties, resulting in { name: ‘John’, age: 30 }.
JavaScript
// Define a "Person" interface with name, age, and address properties
interface Person {
name: string;
age: number;
address: string;
}
// Create "NameAndAge" type using Pick to select name and age from Person
type NameAndAge = Pick<Person, 'name' | 'age'>;
// Declare "person" of type "NameAndAge" with name and age values
const person: NameAndAge = { name: 'John', age: 30 };
// Log the "person" constant
console.log(person);
Output:
{ name: 'John', age: 30 }
Example 2: In this example, we create a MyUser interface and use the Pick utility with a getUserDetails function, which accepts a MyUser object with only username and email, returning a formatted string.
JavaScript
// Define "MyUser" interface
interface MyUser {
id: number;
username: string;
email: string;
password: string;
}
// Function to get user details
function getUserDetails(user: Pick<MyUser, 'username' | 'email'>): string {
return `Username: ${user.username}, Email: ${user.email}`;
}
// Create a "user" object
const user: MyUser = {
id: 123,
username: 'johndoe',
email: '[email protected]',
password: 'password123',
};
// Get and log user details
const userDetails = getUserDetails(user);
console.log(userDetails);
Output:
Username: johndoe, Email: [email protected]
Conclusion
the Pick utility type in TypeScript allows developers to create new types with specific properties from existing types, enhancing type safety and reducing redundancy. It’s especially useful in large codebases with complex types, offering precision and flexibility in type management.
Similar Reads
TypeScript Omit<Type, Keys> Utility Type
TypeScript's Omit<Type, Keys> utility type creates a new type by excluding specific properties (Keys) from an existing type (Type). It is useful for refining types by removing unnecessary properties, enhancing type safety, and simplifying complex type definitions in TypeScript applications. Sy
4 min read
Typescript Record<Keys, Type> Utility Type
In this article, we are going to learn about the Record in Typescript. TypeScript is a programming language that is a strict syntactical superset of JavaScript. It adds optional static typing and class-based object-oriented programming to JavaScript, one of the features is Record which is a built-in
4 min read
Typescript Partial<Type> Utility Type
The Partial<Type> utility in TypeScript creates a new type by making all properties of an existing type optional. This allows you to create flexible object types where only some properties are required, streamlining code and reducing redundancy when working with complex data structures. What i
4 min read
Typescript Required<Type> Utility Type
TypeScript's Required<Type> utility type creates a new type by making all properties of an existing type mandatory. It removes optionality from each property, ensuring that all fields must be provided, which enhances type safety and reduces potential errors in type definitions. Syntax type Req
3 min read
TypeScript ThisType<Type> Utility Type
The TypeScript ThisType utility type allows you to define the type of this within a specific object or function context, providing precise type checking for methods and properties accessed through this. The ThisType utility type is primarily used to provide type annotations for this context when def
3 min read
TypeScript InstanceType<Type> Utility Type
In this article, we are going to learn about InstanceType Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the InstanceType utility type is used to extract the instance type of a constructor function or class
3 min read
TypeScript ReturnType <Type> Utility Type
The ReturnType<Type> utility type in TypeScript extracts and infers the return type of a given function type. It enhances type safety and reusability by allowing developers to dynamically determine the type of values returned by functions without manually specifying them. Syntaxtype ResultType
3 min read
TypeScript Readonly <Type> Utility Type
In this article, we are going to learn about Readonly Utility Type in Typescript. Typescript is a popular programming language used for building scalable and robust applications. One of the features of Typescript is Readonly Utility Type which is used to create a new type where all properties are re
2 min read
TypeScript NonNullable<Type> Utility Type
In this article, we are going to learn about NonNullable Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the NonNullable utility is used to create a new type by removing null and undefined from the given type
2 min read
TypeScript ThisParameterType<Type> Utility Type
In this article, we are going to learn about ThisParameterType Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the ThisParameterType utility type is used to extract the type of this parameter in a function ty
3 min read