Count Items by a Computed Key

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write countBy(items, keyOf): return an object counting how many items have each key keyOf gives. Keys no item has are left out. Type it so the result's keys are exactly what keyOf returns (counting tasks by a status of "todo" | "doing" | "done" gives those three keys), each an optional number since a key may have no items, and so keyOf is given each item with its own type.

Your code must pass

  • Counts items per key
  • Returns an empty object for no items
  • Has no type errors
  • Keys are what keyOf returns
  • Each count is a number that may be missing
  • keyOf gets each item, typed