While trying to map the keys of data I'm getting several Typescript errors and I don't understand why. I'm getting back as a response from my API an object with generic keys as followed:
{
1: {
name: 'test1',
country: 'test1',
},
2: {
name: 'test2',
country: 'test2',
}
....generic amount of keys
}
I have defined the following typescript type for this:
export type Users<T> = {
[K in keyof T]: {
name: string;
country: string;
};
};
When I'm trying to map on the keys of this typed object in my component I'm gtting an error from typescript:
Object.keys(data).map((key) => {
<p>{data[key].name}</p>
})
Error on Object.keys(data) line:
const data: Users | undefined No overload matches this call.
Overload 1 of 2, '(o: {}): string[]', gave the following error. Argument of type 'Users | undefined' is not assignable to parameter of type '{}'. Type 'undefined' is not assignable to type '{}'. Overload 2 of 2, '(o: object): string[]', gave the following error. Argument of type 'Users | undefined' is not assignable to parameter of type 'object'. Type 'undefined' is not assignable to type 'object'.ts(2769) No quick fixes available
Error on data[key].name:
Object is possibly 'undefined'.