Dev Helpers
← Back

Bind an event name to its payload

Bind an event name to its payload using TypeScript.

Open this example in the interactive library

Source code

type Events = {
  start: { id: number };
  finish: { ms: number };
  failure: { reason: string };
};
console.log(
  (<K extends keyof Events>(key: K, value: Events[K]) => ({ key, value }))(
    "start",
    { id: 4 },
  ),
);

Sample output

{ key: 'start', value: { id: 4 } }