I'm currently working on processing an event payload where the event
field is a string, and the content of data
depends on the value of the event
field. While I have come up with a representation that functions correctly, I can't help but feel that there might be a better or more conventional approach:
type EventMap = {
created: { created: string },
updated: { updated: string }
}
type Payload<T extends keyof EventMap> = {
event_id: string;
event: T;
data: EventMap[T];
};
type Payloads = Payload<'created'> | Payload<'updated'>
It seems redundant to keep mentioning created
and updated
. However, using Payload<keyof EventMap>
doesn't quite fit either as it allows for any combination.