स्ट्रिंग कुंजी द्वारा ऑब्जेक्ट तक पहुँचने पर त्रुटि "Element implicitly has an 'any' type"
सभी को नमस्कार! मैं TypeScript में नया हूँ और एक ऐसी त्रुटि का सामना किया जिसे मैं समझ नहीं पा रहा हूँ। मेरे पास पूर्वनिर्धारित कुंजियों वाला एक ऑब्जेक्ट है, और मैं string प्रकार के वेरिएबल से आने वाली कुंजी द्वारा मान प्राप्त करना चाहता हूँ।\n\nयहाँ एक सरल उदाहरण है:\n\n```typescript\nconst userRoles = {\n admin: 'पूर्ण पहुँच',\n editor: 'संपादन',\n viewer: 'केवल देखना'\n};\n\nfunction getRole(roleName: string) {\n return userRoles[roleName];\n}\n```\n\nTypeScript यह त्रुटि देता है:\n\n```\nElement implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ admin: string; editor: string; viewer: string; }'.\n No index signature with a parameter of type 'string' was found on type '{ admin: string; editor: string; viewer: string; }'.\n```\n\nमैं समझता हूँ कि समस्या यह है कि मैं ऑब्जेक्ट तक पहुँचने के लिए `string` का उपयोग करने का प्रयास कर रहा हूँ, जिसकी कुंजियाँ विशिष्ट लिटरल (`'admin' | 'editor' | 'viewer'`) हैं। लेकिन अगर कुंजी बाहरी स्रोत से आती है तो क्या करें?