TypeScript

Omit, Pick, and Exclude in TypeScript

You have a type. You need a variation of it — same shape, minus a few fields. Or only a few fields. TypeScript gives you Omit, Pick, and Exclude for exact...

23 Apr 2024

Omit, Pick, and Exclude in TypeScript

You have a type. You need a variation of it — same shape, minus a few fields. Or only a few fields. TypeScript gives you Omit, Pick, and Exclude for exactly this.

Omit — remove properties from a type

Text
interface User {
  name: string;
  age: number;
  email: string;
}

type UserWithoutEmail = Omit<User, 'email'>;

UserWithoutEmail has name and age, but no email. I use this constantly for API responses where I need to strip sensitive fields before sending data to the client.

Pick — keep only specific properties

Text
type UserNameAndAge = Pick<User, 'name' | 'age'>;

UserNameAndAge has only name and age. The inverse of Omit. Use it when you want a subset of a larger type — say, for a summary view or a form that only touches certain fields.

Exclude — remove types from a union

Exclude works on union types, not object properties. That's the key distinction.

Text
type Primitive = string | number | boolean;
const value: Exclude<Primitive, string> = true; // number | boolean

It removes specific members from a union. If you try to use Exclude on an interface like User, it won't do what you expect — that's what Omit is for.

The trade-off

These utilities make type transformations clean and composable. But they can also make code harder to read when nested. If I see Pick<Omit<User, 'email'>, 'name'>, I'd rather see a named type alias that describes the intent. Readability always wins over cleverness.

Keep reading