AI Hints
Home›TypeScript Tutorial›Utility Types
TypeScript · Lesson 65 of 120

Pick and Omit

Choose properties.

⏱ 2 min read▶ Video at 50:21🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Pick keeps only the properties you name. Omit keeps everything except the ones you name. Both make new types from existing ones.

Pick and Omit
From the video: Pick and Omit
  • Pick: only these
  • Omit: all but these
  • New types

Public customer data

A customer has a password hash, that must never be sent out. Public customer omits it. Customer card picks just the name. Run it.

app.tstypescript
type Customer = { id: number; name: string; email: string; passwordHash: string };

type PublicCustomer = Omit<Customer, "passwordHash">;
type CustomerCard = Pick<Customer, "name">;

function toPublic({ passwordHash, ...rest }: Customer): PublicCustomer {
  return rest;
}

const sam: Customer = { id: 7, name: "Sam", email: "sam@example.com", passwordHash: "x9f2" };
const card: CustomerCard = { name: sam.name };
console.log(toPublic(sam), card);
Output
{ id: 7, name: 'Sam', email: 'sam@example.com' } { name: 'Sam' }

Summary

Pick and Omit build new types by choosing properties. Next, Record.

Key takeaways
  • Pick<T, K>
  • Omit<T, K>
  • Safe outputs
Next up: Record — typed dictionaries