Write a typed groupBy(items, getKey) that groups items into an object by the key the callback returns.
groupBy(users, (user) => user.role) returns { admin: [...], member: [...] }. Type it so the keys and the item type come from the call, and so the result doesn't claim a group exists when no item had that key.
function groupBy(items: any[], getKey: (item: any) => string): Record<string, any[]> { const groups: Record<string, any[]> = {}; for (const item of items) { (groups[getKey(item)] ??= []).push(item); } return groups;} // type User = { name: string; role: "admin" | "member" };// groupBy(users, (user) => user.role);// // typed Partial<Record<"admin" | "member", User[]>>The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…