Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Typescript Api
  4. / Lessons
  5. / 1

Prev

Pick

  • Pick allows us to create a new type by selecting a set of properties from an existing type.
  • It’s useful when you want a type that only contains a subset of the properties of another type.
interface User {
id: string;
name: string;
age: number;
email: string;
password: string;
}
// Create a type for displaying a user profile
type UserProfile = Pick<User, 'name' | 'email'>;
const displayUserProfile = (user: UserProfile) => {
console.log(`Name: ${user.name}, Email: ${user.email}`);
};
const user: UserProfile = { name: 'John Doe', email: 'john@example.com' };
displayUserProfile(user); // Output: Name: John Doe, Email: john@example.com

Prev