Utility Types में Record
In this page:
type Result = Record<KeyType, ValueType>;
Basic Record
Record<K, T> एक object type बनाता है जिसकी keys K से आती हैं और जिसकी values सबकी type T की होती हैं, जो एक known key type के साथ dictionaries या lookup tables describe करने के लिए उपयोगी है।
उदाहरण: Basic Record
type Inventory = Record<string, number>;
const stock: Inventory = { apples: 10, bananas: 5 };
console.log(stock);
Record with Number Values
Record उन maps के लिए उपयोगी है जहां हर known key एक numeric value store करती है, जैसे sizes और उनकी corresponding prices describe करने के लिए Record<small | medium | large, number>।
उदाहरण: Record with Number Values
type SizePrices = Record<"small" | "medium" | "large", number>;
const prices: SizePrices = { small: 5, medium: 7, large: 9 };
console.log(prices.medium);
Record with Object Values
Record में value type T खुद एक object type हो सकता है, जो आपको user IDs से पूरे user objects के एक map जैसी ज़्यादा complex lookup structures describe करने देता है। यह Record को structured caches के लिए उतना ही उपयोगी बनाता है जितना simple dictionaries के लिए।
उदाहरण: Record with Object Values
interface User {
id: number;
name: string;
}
type UserMap = Record<number, User>;
const users: UserMap = { 1: { id: 1, name: "Ravi" } };
console.log(users[1].name);
Record with Dynamic Keys
Record string या number को एक general key type के रूप में उपयोग कर सकता है जब keys का exact set पहले से known न हो, effectively एक fixed set of named fields के बजाय एक open-ended dictionary describe करते हुए।
उदाहरण: Record with Dynamic Keys
type Scores = Record<string, number>;
const scores: Scores = {};
scores["alice"] = 90;
scores["bob"] = 85;
console.log(scores);
When to Use Record
Dictionaries, lookup tables, configuration maps, और किसी भी और fixed-key collection के लिए Record उपयोग करें जहां हर value एक ही type share करे।
यह अक्सर एक index signature का एक cleaner alternative है जब value type हर key में consistent हो।
उदाहरण: When to Use Record
type PlanPrices = Record<"free" | "pro" | "enterprise", number>;
const prices: PlanPrices = { free: 0, pro: 20, enterprise: 100 };
console.log(prices);
Record<string, number>उपयोग करना और यह मानना कि एक missing key एक error है, जबstock.pearstype-check मेंnumberहै लेकिन असल मेंundefinedहो सकता है।Record<"small" | "large", number>लिखना और एक key छोड़ देना, जो एक error है क्योंकि हर key required है।Record<K, T>के argument order को confuse करना और value type पहले लिखना।
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