A type-safe groupBy function for TypeScript
npm install better-groupbyts
import { groupBy } from "better-groupby"; // Import happiness
// Your boring data (we'll fix that)
const circus = [
{ performer: "clown", skill: "juggling" },
{ performer: "acrobat", skill: "flying" },
{ performer: "clown", skill: "pratfalls" },
{ performer: "acrobat", skill: "defying death" },
];
// Group your circus performers (no animals were harmed)
const grouped = groupBy(circus, "performer");
// Look ma, no type errors! πͺ
// For the brave souls who like to live dangerously...
const circusExtravaganza = [
{ star: "Bob", act: { type: "fire-eating", danger: "very yes" } },
{ star: "Alice", act: { type: "lion-taming", danger: "much wow" } },
{ star: "Bob", act: { type: "fire-eating", danger: "still very yes" } },
];
// Group by nested properties like a coding ninja π₯·
const byAct = groupBy(circusExtravaganza, (item) => item.act.type);
// TypeScript: "I got your back, fam" π
``