TypeScript: خطأ "Element implicitly has an 'any' type" عند الوصول إلى كائن باستخدام مفتاح نصي
```html
مرحباً بالجميع! أنا مبتدئ في TypeScript وواجهت مشكلة لا أستطيع حلها. أكتب دالة تأخذ كائنًا يحتوي على بيانات المستخدمين وسلسلة نصية — الحالة. يجب إرجاع مصفوفة المستخدمين بهذه الحالة.
هذا هو الكود الخاص بي:
interface User {
name: string;
status: 'active' | 'inactive' | 'pending';
}
const usersByStatus: Record<string, User[]> = {
active: [{ name: 'Иван', status: 'active' }],
inactive: [{ name: 'Мария', status: 'inactive' }],
pending: [{ name: 'Петр', status: 'pending' }],
};
function getUsersByStatus(status: string): User[] {
return usersByStatus[status];
}
TypeScript يعطي خطأ:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<string, User[]>'. No index signature with a parameter of type 'string' was found on type 'Record<string, User[]>'.
لا أفهم لماذا يظهر الخطأ. ففي النهاية، Record<string, User[]> يجب أن يقبل أي