TypeScriptDevelopmentType

TypeScript: Omit & Pick

Omit and Pick are two utility types in TypeScript. Omit is used to exclude properties from an object type, and Pick is used to select properties from an object type. In this content, we will explain these two utility types with examples.

Omit is a utility type that is used to exclude properties from an object type. It takes two parameters: the object type and the properties to exclude. The syntax of the Omit type is as follows:

interface Article {
  id: number;
  name: string;
  description: string;
  link: string;
  tags: string[];
}


type NewArticleType = Omit<Article, "link" | "tags">;
Select the keys you want to exclude:
The new type is like this:
type NewArticleType = {
 id: number;
 name: string;
 description: string;
}

Pick is a utility type that is used to select properties from an object type. It takes two parameters: the object type and the properties to select. The syntax of the Pick type is as follows:

interface Article {
  id: number;
  name: string;
  description: string;
  link: string;
  tags: string[];
}


type NewArticleType = Pick<
  Article,
  "id" | "name" | "description"
>;
Select the keys you want to include:
The new type is like this:
type NewArticleType = {
 id: number;
 name: string;
 description: string;
}

The Omit and Pick utility types are very useful when working with TypeScript. They allow you to create new types by excluding or selecting properties from existing types. This can help you write more maintainable and readable code.


This content was supported by



If this content helped you, you can buy me a coffee.
Related: The Anatomy of a URL

You can join the newsletter to be notified of awesome interactive articles and courses about software, design and AI. You will receive at most a few emails per month.


+99

Join to our Supporters or Sponsors