TypeScript doesn't infer the type of passed generic
If attributes
contains an object with the other attributes of the object it's type should be Item
, or more specifically Omit<Item, 'id'>
.
export type ResponseType<Item> = {
id: string;
attributes: Omit<Item, 'id'>
}
// parser function to run after fetching the data
function parseResponse(data: ReviewResponse[]): Review[] {
return data.map((item) => {
return {
id: item.id,
...item.attributes,
};
});
}
Playground Link
[key: string]: Item
means that attributes could have multiple Item
properties under different keys. So it could be something like { "A": { /*attributes of Review*/ }, "B": { /*attributes of Review*/ } }