← Back to TypeScript Course | Chapter 4: Interfaces | Lesson 7 of 7

Index Signatures की जानकारी

एक index signature एक interface को उन objects describe करने देता है जिनके property names पहले से known नहीं हैं। यह specify करता है कि keys किस type की हैं और वे keys किस type की values contain कर सकती हैं।
Syntax
typescript
interface InterfaceName {
  [key: string]: valueType;
}

Basic Index Signatures

एक index signature, जो square brackets के साथ [key: string]: number जैसी लिखी जाती है, उस object के लिए property names और values का type define करती है जिसकी exact keys पहले से known नहीं हैं।

यह TypeScript को एक object type करने देता है भले ही उसकी properties dynamically add होती हों।

उदाहरण: Basic Index Signatures

typescript
interface Scores {
  [key: string]: number;
}
const scores: Scores = { math: 90, science: 85 };
console.log(scores);

String Index Signatures

String index signatures dictionary-like objects और lookup tables के लिए खासकर उपयोगी हैं, जैसे country codes को country names से map करना। उस object पर access की गई हर property, उसका नाम चाहे जो हो, declared value type की गारंटी देती है।

उदाहरण: String Index Signatures

typescript
interface CountryCodes {
  [code: string]: string;
}
const codes: CountryCodes = { IN: "India", US: "United States" };
console.log(codes["IN"]);

Number Index Signatures

एक number index signature एक object को numeric keys के साथ access होने देता है, कुछ उस तरह जैसे arrays under the hood काम करते हैं। यह string index signatures से कम common है पर तब दिखता है जब array-like या sparse numeric collections model किए जा रहे हों।

उदाहरण: Number Index Signatures

typescript
interface StringArray {
  [index: number]: string;
}
const items: StringArray = { 0: "first", 1: "second" };
console.log(items[0]);

Index Signatures with Named Properties

एक interface एक index signature को specific named properties के साथ combine कर सकता है, जब तक वे named properties index signature द्वारा उनकी values के लिए declared type के साथ compatible हैं।

यह आपको कुछ known properties के exist होने की गारंटी देते हुए अभी भी arbitrary additional properties allow करने देता है।

उदाहरण: Index Signatures with Named Properties

typescript
interface Config {
  name: string;
  [key: string]: string;
}
const config: Config = { name: "App", version: "1.0" };
console.log(config);

Practical Dictionary Example

Index signatures एक dictionary object को type करने का standard तरीका हैं जहां keys runtime पर dynamically add होती हैं, जैसे generated IDs से keyed एक cache। एक के बिना, TypeScript किसी ऐसी key उपयोग करने वाले किसी भी property access को reject कर देगा जिसे यह compile time पर verify नहीं कर सकता।

उदाहरण: Practical Dictionary Example

typescript
interface Cache {
  [id: string]: number;
}
const cache: Cache = {};
cache["user_1"] = 100;
cache["user_2"] = 200;
console.log(cache);
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. एक अलग value type वाली property जोड़ना, जैसे [key: string]: number में एक string, जो एक error है।
  2. यह मान लेना कि हर key scores["art"] पढ़ते समय exist करती है, जो number typed है पर undefined हो सकती है।
  3. एक number index signature उपयोग करना और string keys के covered होने की उम्मीद करना।
चैप्टर सारांश
  • Interfaces एक object की shape define करते हैं, flexibility और safety के लिए optional (?) और readonly properties के साथ।
  • Interfaces दूसरे interfaces को extend कर सकते हैं, classes द्वारा implement किए जा सकते हैं, और dynamic keys के लिए index signatures उपयोग कर सकते हैं।
  • Interfaces और type aliases overlap करते हैं, तो एक चुनने से पहले यह जानना मदद करता है कि वे कैसे अलग हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.