sindresorhus / ts-extras

Essential utilities for TypeScript projects
MIT License
587 stars 15 forks source link

Object.groupBy is missing #60

Closed sarimarton closed 5 months ago

sarimarton commented 5 months ago

I'm just passing by to leave an issue as a reminder, as I notice there isn't one yet.

Sorry that I don't have time to contribute.

It would be great to have a better typing in cases like

Object.groupBy([{ a: 5, b: 6 }, { a: 7, b: 8 }], x => x.a)
kovacsaronlaszlo commented 5 months ago

My solution is the following lines:

type ObjectGroupBy = <T, K extends PropertyKey | undefined>(
  items: Iterable<T>,
  keySelector: (item: T, index: number) => K
) => Record<Exclude<K, undefined>, T[]>

const objectGroupBy = Object.groupBy as ObjectGroupBy
sarimarton commented 5 months ago

sorry, invalid issue