Q26: How do `Pick<T, K>` and `Omit<T, K>` utility types work?
Pick<T, Keys>: Constructs a type by picking a specific set of property keys fromT.Omit<T, Keys>: Constructs a type by removing a specific set of property keys fromT.
Pick vs Omit
interface Article {
id: number;
title: string;
content: string;
author: string;
}
type ArticlePreview = Pick<Article, 'id' | 'title'>; // Contains ONLY id & title
type ArticleDraft = Omit<Article, 'id'>; // Contains title, content, author💡 Pick creates a subset with selected keys; Omit creates a subset excluding specified keys.
Key Takeaways
- Pick selects specific keys; Omit excludes specific keys.