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

Utility Types में Record

Record<K, T> एक object type बनाता है जिसकी keys K से आती हैं और जिसकी values सबकी type T होती है। यह dictionaries और lookup tables के लिए उपयोगी है।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
type PlanPrices = Record<"free" | "pro" | "enterprise", number>;
const prices: PlanPrices = { free: 0, pro: 20, enterprise: 100 };
console.log(prices);
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. Record<string, number> उपयोग करना और यह मानना कि एक missing key एक error है, जब stock.pears type-check में number है लेकिन असल में undefined हो सकता है।
  2. Record<"small" | "large", number> लिखना और एक key छोड़ देना, जो एक error है क्योंकि हर key required है।
  3. Record<K, T> के argument order को confuse करना और value type पहले लिखना।

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.