79084434

Date: 2024-10-14 00:35:47
Score: 0.5
Natty:
Report link

(Answering my own question...)

So it turns out that tweaking EventHandlerMap as follows resolves the TypeScript error:

type EventHandlerMap<E extends Event> = {
  // Previously: [Ev in E as Ev['type']]: (data: Ev['data']) => void
  [T in E['type']]: (data: Extract<E, { type: T }>['data']) => void
}

I suppose TypeScript can rationalise this in a way that more closely matches my intent, since it ~iterates over the Event['type']s rather than the Events themselves. This will behave differently in the case where multiple members of the Event union have the same type attribute (but different data attributes).

Playground

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: George Bradley