TypeScript - mapping between event name and event type

less than 1 minute read

Type script for mapping between event name and event type

1
2
3
4
5
6
7
8
9
10
11
12
13
function htmlEventMap<K extends keyof HTMLElementEventMap>(
  eventName: K,
  callback: (event: HTMLElementEventMap[k]) => void
) {
  console.log(eventName)
}

function elementEventMap<K extends keyof ElementEventMap>(
  eventName: K,
  callback: (event: ElementEventMap[k]) => void
) {
  console.log(eventName)
}