Avatar ·

TypeScript ऑब्जेक्ट्स को C# की तरह Dictionary के रूप में उपयोग करना

नमस्ते! मैं C# से TypeScript पर स्विच कर रहा हूँ और समझ नहीं पा रहा हूँ कि Dictionary का एनालॉग सही तरीके से कैसे लागू करूँ। C# में मैं लिखता हूँ:\n\n```csharp\nvar dict = new Dictionary();\ndict.Add("apple", 5);\ndict.Add("banana", 3);\nConsole.WriteLine(dict["apple"]); // 5\n```\n\nTypeScript में मैं ऐसा करने की कोशिश कर रहा हूँ:\n\n```typescript\nlet dict = {};\ndict["apple"] = 5;\ndict["banana"] = 3;\nconsole.log(dict["apple"]); // 5\n```\n\nलेकिन TypeScript `dict["apple"]` पर त्रुटि देता है — यह कहता है कि एलिमेंट का implicit type `any` है। अगर मैं स्पष्ट रूप से type define करूँ:\n\n```typescript\nlet dict: { [key: string]: number } = {};\n```\n\nतो सब काम करता है, लेकिन मैं समझ नहीं पा रहा हूँ:\n\n1. ऐसे dictionary को सही तरीके से कैसे declare करूँ?\n2. यह कैसे जाँचूँ कि key मौजूद है या नहीं?\n3. Dictionary के सभी elements को कैसे iterate करूँ?\n4. क्या TypeScript में कोई built-in Dic