This is a similar task from the course of JS Advanced, but now it is strictly typed and uses generic types. You need to aggregate data based on a particular key, for instance, by the author of the book.
- You are given an interface
Item
, which has indexkey
of typestring
and values of typeany
.
interface Item {
[key: string]: any
}
- You are given a generic interface
ItemsGroup
, which has indexkey
of typestring
and values of type array ofT
.
interface ItemsGroup<T> {
[key: string]: T[]
}
- Your task is to implement function
groupByKey
, which takes two arguments:array
ofT
objects and stringkey
. The function should readkey
of each object in the array and group them intoItemsGroup
.
Hint: You should use a new keyword in types definition called keyof
for correct TypeScript type checks.