Avatar ·

خطأ "Element implicitly has an 'any' type" عند الوصول إلى كائن بواسطة مفتاح من نوع string

مرحباً بالجميع! أنا مبتدئ في TypeScript وقد واجهت خطأً لا أستطيع فهمه. لدي كائن بمفاتيح محددة مسبقاً، وأريد الحصول على قيمة بناءً على مفتاح يأتي من متغير من نوع string. إليك مثال مبسط: ```typescript const userRoles = { admin: 'full access', editor: 'editing', viewer: 'view only' }; function getRole(roleName: string) { return userRoles[roleName]; } ``` يظهر TypeScript الخطأ التالي: ``` Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ admin: string; editor: string; viewer: string; }'. No index signature with a parameter of type 'string' was found on type '{ admin: string; editor: string; viewer: string; }'. ``` أفهم أن المشكلة تكمن في أنني أحاول استخدام `string` للوصول إلى كائن مفاتيحه عبارة عن قيم حرفية محددة (`'admin' | 'editor' | 'viewer'`). لكن ماذا أفعل إذا كان المفتاح يأتي من مصدر خارجي (على