Loading lessons...
Selection Utility Types (Pick & Omit)
Selection Utility Types: Pick & Omit
Pick and Omit allow constructing new object types by selecting or filtering properties from an existing type.
Summary of Pick & Omit
| Utility Type | Syntax | Description |
|---|---|---|
Pick<T, K> | Pick<Type, Keys> | Constructs a type by picking specific keys K from T. |
Omit<T, K> | Omit<Type, Keys> | Constructs a type by removing specific keys K from T. |
Code Examples
interface Article {
id: string;
title: string;
content: string;
author: string;
createdAt: Date;
}
// Pick only title and author for a preview card
type ArticlePreview = Pick<Article, "title" | "author">;
/*
Resulting type:
{
title: string;
author: string;
}
*/
// Omit internal fields for API submission
type ArticleCreateInput = Omit<Article, "id" | "createdAt">;
/*
Resulting type:
{
title: string;
content: string;
author: string;
}
*/
TL;DR
- Use
Pickwhen you need a subset of properties from a larger type. - Use
Omitwhen you want everything except a few specific fields.