DEV Community

Nguyen Dinh Khai
Nguyen Dinh Khai

Posted on • Edited on

Pick, Omit and Partial in Typescript

A. Pick
Use pick to create a new type from a previous type with only a few necessary keys.

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

type PickUser = Omit<User, 'name'>;

// Resut
type PickUser = {
    name: string;
};
Enter fullscreen mode Exit fullscreen mode

B. Omit
While Pick is handy, there are occasions when the inverse operation is required. We may need to construct a type that includes everything except a few fields from a preceding type

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

type OmitUser = Omit<User, 'name'>;

// Resut
type OmitUser = {
    age: number;
};

Enter fullscreen mode Exit fullscreen mode

C. Partial
This utility type facilitates swift creation of a type with optional or undefined properties based on an existing type

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

type PartialUser = Partial<User>;

// Resut
type PartialUser = {
    name?: string | undefined;
    age?: number | undefined;
};
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay