type Apple = {
type: "fruit";
color: "green";
};
type Banana = {
type: "fruit";
color: "yellow";
};
type Tomato = {
type: "vegetable";
color: "red";
};
type GroupedByType = GroupBy<Apple | Banana | Tomato, "type">;
// ^?
// {
// fruit: Apple | Banana;
// vegetable: Tomato;
// }
type GroupedByColor = GroupBy<Apple | Banana | Tomato, "color">;
// ^?
// {
// green: Apple;
// yellow: Banana;
// red: Tomato;
// }
Groups a type
Tby the values of a keyK.