I strongly believe that the best way to showcase this concept is through a clear example:
enum EventType { A, B, C };
type MyEvent =
[EventType.A, number] |
[EventType.B, string] |
[EventType.C, number, string];
const eventsGrouped: Record<EventType, MyEvent[]> = {
[EventType.A]: [],
[EventType.B]: [],
[EventType.C]: [],
};
The objective here is to establish a type for the eventsGrouped
object
that categorizes the value type based on the key provided
In simplified terms:
Record<EventType, MyEvent where MyEvent[0] === object entry key>
To clarify further:
eventsGrouped[EventType.A]
should have the type of[EventType.A, number]
eventsGrouped[EventType.B]
should be of type[EventType.B, string]
eventsGrouped[EventType.C]
needs to match with type[EventType.C, number, string]