typescript

Pick and Omit Utility Types

Create new types by picking or omitting properties from existing types

Tarun Sharma
Tarun SharmaJanuary 15, 2024 · 1 min read · Last Updated:
interface User {
  id: number;
  name: string;
  email: string;
  password: string;
  createdAt: Date;
}

// Pick only specific properties
type UserPublic = Pick<User, 'id' | 'name' | 'email'>;

// Omit sensitive properties
type UserSafe = Omit<User, 'password'>;

// Create input type (without auto-generated fields)
type CreateUserInput = Omit<User, 'id' | 'createdAt'>;

// Partial for updates
type UpdateUserInput = Partial<Omit<User, 'id'>>;

Custom utility: Required Pick

type RequiredPick<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>;

// Make only 'name' and 'email' required
type UserWithRequiredContact = RequiredPick<Partial<User>, 'name' | 'email'>;

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub


Tarun Sharma

Written byTarun Sharma
Full-stack developer and tech educator with 10+ years of experience building scalable applications. Passionate about Node.js, NestJS, React, and cloud technologies. Creator of 50+ courses on Udemy and active YouTube educator helping developers level up their skills.
Connect

Is this page helpful?

Related VideosView All

Stack Overflow Clone - APIs Integration Redux Toolkit [Closure] - App Demo #05

Become Ninja Developer - API security Best Practices with Node JS Packages #15

Nest JS Microservices using HTTP Gateway and Redis Services (DEMO) #nestjs #microservices #16