Sub topic 3
Pick and Omit: selecting and removing properties
This chapter assumes you know TypeScript interfaces, type aliases, unions, string literal types, and generic syntax. Pick and Omit derive new object types from an existing type. They change compile-time checking only. They do not add or remove properties at runtime.
`Pick`: keep named properties
Pick<Type, Keys> creates a type containing only the properties named by Keys. The keys must exist in the original type.
interface User {
id: number;
name: string;
email: string;
isAdmin: boolean;
}
type UserPreview = Pick<User, "id" | "name">;
const preview: UserPreview = {
id: 1,
name: "Ada",
};
console.log(preview);UserPreview contains id and name, but it does not require email or isAdmin. If you write a key that is not in User, TypeScript reports an error:
// type Invalid = Pick<User, "username">;
// Error: Type '"username"' does not satisfy the constraint 'keyof User'.A common use for Pick is defining a smaller response or view model. A view model is a type containing only the data a particular screen needs.
Pick preserves property modifiers. If a selected property was optional or readonly, it remains optional or readonly in the new type.
`Omit`: remove named properties
Omit<Type, Keys> creates a type containing every property except the named keys.
interface User {
id: number;
name: string;
email: string;
}
type NewUser = Omit<User, "id">;
const newUser: NewUser = {
name: "Ada",
email: "[email protected]",
};
console.log(newUser);Here, NewUser represents data needed to create a user. The server can assign the id, so callers must not provide it.
Omit is useful when a type has one or two properties that should not be accepted in a particular operation. It can be easier to read than rewriting a large interface.
Choosing between `Pick` and `Omit`
Use Pick when the allowed properties form a small, clearly defined list. Use Omit when most properties remain valid and only a small list must be removed.
| Goal | Utility type | Example | Column |
|---|---|---|---|
| Keep only an identifier and display name | `Pick` | `Pick<User, "id" | "name">` |
| Remove a generated identifier | `Omit` | `Omit<User, "id">` |
The following diagram shows how both utilities derive types from User:
Composing `Pick`, `Omit`, and `Partial`
Utility types can be nested. For example, an update request usually allows some editable fields but not all of them. Partial makes the remaining fields optional, while Omit prevents changing the identifier.
interface User {
id: number;
name: string;
email: string;
isAdmin: boolean;
}
type UserUpdate = Partial<Omit<User, "id">>;
function updateUser(id: number, changes: UserUpdate): void {
console.log(`Updating ${id}`, changes);
}
updateUser(1, { email: "[email protected]" });
updateUser(1, { isAdmin: false });
updateUser(1, {});
// updateUser(1, { id: 2 });
// Error: 'id' is not allowed in UserUpdate.The order matters. Omit<User, "id"> first removes id, and Partial<...> then makes name, email, and isAdmin optional. This gives the intended update shape.
A union-type pitfall
Omit can surprise you when applied directly to a union. TypeScript may calculate the keys shared by all union members before removing a key.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; side: number };
type WithoutKind = Omit<Shape, "kind">;
// This may lose the relationship between each kind and its property.For a union, distribute Omit across each member with a conditional type:
type DistributiveOmit<T, K extends keyof any> =
T extends unknown ? Omit<T, K> : never;
type ShapeData = DistributiveOmit<Shape, "kind">;
// { radius: number } | { side: number }Use this pattern when each union member has different properties that must remain distinct.
Exercise
Which type allows a caller to update a User without changing its id, while making every other property optional?
Show answer
type UserUpdate = Partial<Omit<User, "id">>;