← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 13 of 20

Utility Types में Pick

Pick<T, K> दूसरे type से सिर्फ selected properties रखते हुए एक नया type बनाता है। यह उपयोगी है जब एक function या component को एक बड़े object का सिर्फ एक हिस्सा चाहिए।
Syntax
typescript
type Result = Pick<TypeName, "key1" | "key2">;

Basic Pick

Pick<T, K>, property-name literals K के एक union के साथ, एक existing type T से एक या ज़्यादा properties select करता है, और सिर्फ वे properties रखते हुए एक छोटा type produce करता है।

उदाहरण: Basic Pick

typescript
interface User {
  id: number;
  name: string;
  email: string;
}
type UserPreview = Pick<User, "id" | "name">;
const preview: UserPreview = { id: 1, name: "Ravi" };
console.log(preview);

Pick for Function Arguments

Pick उन functions के लिए एक छोटा parameter object define कर सकता है जिन्हें एक बड़े model से सिर्फ कुछ fields चाहिए, बजाय पूरा original type pass करने की ज़रूरत के।

उदाहरण: Pick for Function Arguments

typescript
interface User {
  id: number;
  name: string;
  email: string;
}
function greet(user: Pick<User, "name">) {
  console.log("Hello", user.name);
}
greet({ name: "Ravi" });

Pick with Interfaces

Pick interfaces के साथ exactly वैसे ही काम करता है जैसे यह type aliases के साथ करता है, क्योंकि दोनों आखिर में named, typed properties का एक set describe करते हैं जिनसे Pick select कर सकता है।

इसका मतलब है कि आप Pick को किसी भी special-casing के बिना type definition के दोनों styles के साथ freely mix कर सकते हैं।

उदाहरण: Pick with Interfaces

typescript
interface Product {
  id: number;
  title: string;
  price: number;
}
type ProductSummary = Pick<Product, "title" | "price">;
const s: ProductSummary = { title: "Book", price: 20 };
console.log(s);

Pick and Type Safety

Pick को selected property names का source type पर असल में मौजूद होना चाहिए, जो उन keys की list में typos के खिलाफ strong compile-time checking देता है जिन्हें आप pick कर रहे हैं।

उदाहरण: Pick and Type Safety

typescript
interface User {
  id: number;
  name: string;
}
type IdOnly = Pick<User, "id">;
const u: IdOnly = { id: 5 };
console.log(u.id);

When to Use Pick

Pick उपयोग करें जब आपको एक existing type का सिर्फ कुछ properties के आधार पर एक छोटा, focused view चाहिए, बजाय fields के एक subset को एक बिल्कुल नए type definition में duplicate करने के।

उदाहरण: When to Use Pick

typescript
interface Article {
  id: number;
  title: string;
  body: string;
  author: string;
}
type ArticleHeader = Pick<Article, "title" | "author">;
const header: ArticleHeader = { title: "News", author: "Ravi" };
console.log(header);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक ऐसी key का नाम देना जो मौजूद नहीं है, जैसे Pick<User, "username">, जो एक compile error है।
  2. Keys को एक array ["id"] के रूप में pass करना बजाय literals के एक union "id" | "name" के।
  3. यह भूल जाना कि Pick original properties के optional और readonly modifiers रखता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.