Utility Types में Pick
In this page:
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
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
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
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
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
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);
- एक ऐसी key का नाम देना जो मौजूद नहीं है, जैसे
Pick<User, "username">, जो एक compile error है। - Keys को एक array
["id"]के रूप में pass करना बजाय literals के एक union"id" | "name"के। - यह भूल जाना कि
Pickoriginal properties के optional और readonly modifiers रखता है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates