TypeScript: स्ट्रिंग कुंजी द्वारा ऑब्जेक्ट तक पहुँचने पर "Element implicitly has an 'any' type" त्रुटि
सभी को नमस्कार! मैं TypeScript में नया हूँ और एक समस्या का सामना कर रहा हूँ जिसे हल नहीं कर पा रहा हूँ। मैं एक function लिख रहा हूँ जो उपयोगकर्ताओं के डेटा वाला एक ऑब्जेक्ट और एक स्ट्रिंग — status लेता है। मुझे इस status वाले उपयोगकर्ताओं की array लौटानी है।
यहाँ मेरा कोड है:
```typescript
interface User {
name: string;
status: 'active' | 'inactive' | 'pending';
}
const usersByStatus: Record = {
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'. No index signature with a parameter of type 'string' was found on type 'Record'.
मुझे समझ नहीं आ रहा कि यह त्रुटि क्यों हो रही है। आखिरकार, `Record` को किसी भी स्ट्रिंग को स्वीकार करना चाहिए।