Command Palette

Search for a command to run...

Hectal
Phase 7: Generics
TypeScript · Exercise 5 of 6 PreviousNext
Intermediate

Write groupBy

groupBy should sort a list into piles, one per key. The current version works only for unknown items and string keys, so the callback (w) => w[0] doesn't even compile.

Make it generic: T for the item, K extends PropertyKey for the key that the callback returns. The result is a Partial<Record<K, T[]>>: not every key has a pile.

What Check tests

  1. Your code has no type errors
  2. The console prints the three piles
  3. Numbers can be grouped by a text key
  4. The callback sees the real item type
  5. A boolean key is rejected
  6. A pile may be missing

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Console

Nothing printed yet.