Pick<T, K> / Omit<T, K>
| Since: | Pick<T,K> | TypeScript 2.1(2016) |
|---|---|---|
| Omit<T,K> | TypeScript 3.5(2019) |
Utility types that create a new type by selecting or excluding properties from an existing type. Pick<T, K> keeps only the specified properties, while Omit<T, K> removes the specified properties.
Syntax
type SelectedType = Pick<BaseType, "propertyName" | "propertyName">; // Omit<T, K>: Creates a type with the properties specified by key K removed from type T. type ExcludedType = Omit<BaseType, "propertyName" | "propertyName">;
Syntax Overview
| Type | Description |
|---|---|
| Pick<T, K> | Returns a type that contains only the properties specified by key K from type T. Use this when you need only a few properties. |
| Omit<T, K> | Returns a type with the properties specified by key K removed from type T. Use this when you want to exclude only a few properties. |
Sample Code
interface User {
id: number;
name: string;
email: string;
password: string;
createdAt: Date;
updatedAt: Date;
}
// Pick: Select only the properties you need.
// Extract just id and name for displaying a user profile.
type UserProfile = Pick<User, "id" | "name">;
// Equivalent to: { id: number; name: string; }
const profile: UserProfile = { id: 1, name: "Kiryu Kazuma" };
console.log(profile.name); // Outputs "Kiryu Kazuma".
// console.log(profile.email); // Error: email does not exist on UserProfile.
// Omit: Remove properties you don't need.
type PublicUser = Omit<User, "password">;
// Equivalent to: { id: number; name: string; email: string; createdAt: Date; updatedAt: Date; }
const publicUser: PublicUser = {
id: 1,
name: "Kiryu Kazuma",
email: "kiryu_kazuma@wp-p.info",
createdAt: new Date("2024-01-01"),
updatedAt: new Date("2024-06-01"),
};
console.log(publicUser.email); // Outputs "kiryu_kazuma@wp-p.info".
// Omitting multiple properties.
type UserForCreation = Omit<User, "id" | "createdAt" | "updatedAt">;
// Exclude id and timestamp fields when creating a new user.
// Equivalent to: { name: string; email: string; password: string; }
function createUser(data: UserForCreation): User {
return {
...data,
id: Math.floor(Math.random() * 10000),
createdAt: new Date(),
updatedAt: new Date(),
};
}
const newUser = createUser({ name: "Majima Goro", email: "majima_goro@wp-p.info", password: "hashed_pw" });
console.log(newUser.id); // Outputs a random ID (differs on each run)
// Choosing between Pick and Omit: use Pick when keeping fewer properties, use Omit when excluding fewer.
// For example, to display only name and email:
type DisplayFields = Pick<User, "name" | "email">; // Specifies 2 properties → Pick is concise.
// type DisplayFields = Omit<User, "id" | "password" | "createdAt" | "updatedAt">; // Specifies 4 properties → more verbose.
function displayUser(user: DisplayFields): void {
console.log(`${user.name} (${user.email})`);
}
displayUser({ name: "Kiryu Kazuma", email: "kiryu_kazuma@wp-p.info" });
// Outputs "Kiryu Kazuma (kiryu_kazuma@wp-p.info)".
Running the above produces the following output:
npx ts-node ts_pick_omit.ts Kiryu Kazuma kiryu_kazuma@wp-p.info Kiryu Kazuma (kiryu_kazuma@wp-p.info)
Details
Pick<T, K> generates a new type containing only the properties specified by key K from type T. Internally, it is implemented as the mapped type { [P in K]: T[P] }. It is useful for extracting only the information you need from partial form fields or API responses.
Omit<T, K> generates a new type with the properties specified by key K removed from type T. It is handy for creating types that exclude sensitive fields such as passwords, or for representing an "ID-less" state before saving to a database.
To decide which to use, compare the number of properties you want to keep against the number you want to exclude, and choose whichever requires less code. You can also combine Pick or Omit with Partial/Required (for example, Partial<Pick<User, "name" | "email">>) to express more granular type variations.
If you find any errors or copyright issues, please contact us.