In my simplified interface, I have:
interface Author {
id?: number;
firstName?: string;
lastname?: string;
age?: number;
coAuthors?:
[
{
id: number;
firstName: string;
lastname: string;
age: number;
}
]
}
I am attempting to retrieve an object value based on a key:
const getByKeyName = <T extends {coAuthors: ['coAuthors']}, K extends keyof T>(item: T, key: K) =>
{
return item[key] ?? (item.coAuthors?.length ? item.coAuthors![0][key] : 0);
}; ^^^^^^^^^
cannot use [key] to index item.coAuthors![0]
-----------------------------
getByKeyName(author, 'id');
This leads to the TS error mentioned above, as child is a different object. What would be the best way to address this issue?