Index Signatures की जानकारी
In this page:
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
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
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
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
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
interface Cache {
[id: string]: number;
}
const cache: Cache = {};
cache["user_1"] = 100;
cache["user_2"] = 200;
console.log(cache);
- एक अलग value type वाली property जोड़ना, जैसे
[key: string]: numberमें एक string, जो एक error है। - यह मान लेना कि हर key
scores["art"]पढ़ते समय exist करती है, जोnumbertyped है परundefinedहो सकती है। - एक number index signature उपयोग करना और string keys के covered होने की उम्मीद करना।
- Interfaces एक object की shape define करते हैं, flexibility और safety के लिए optional (
?) औरreadonlyproperties के साथ। - 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: