function groupIntoMap(items, keyFn) {
  /** @type {Map<TKey, Array<TValue>>} */
  const groups = new Map();

  for (const item of items) {
    const key = keyFn(item);
    const group = groups.get(key) || [];
    group.push(item);
    groups.set(key, group);
  }

  return groups;
}
@template TKey
@template TValue
@param {Array<TValue>} items
@param {(item: TValue) => TKey} keyFn
@return {Map<TKey, Array<TValue>>}