I want to create a function addEventListener
This function has two arguments, the event and the listener.
I have some specific events: ex. onScroll, onClick, onDoubleClick.
Depending on the event I want to have a separate signature / type for the listener argument.
So if I start typing:
addEventListener('onClick', (param: OnClickType) => // here I want to resolve a certain listener type )
// or
addEventListener('onScroll', (param: OnScrollType) => // here I want to resolve a certain listener type )
Is this possible in typescript ?
[EDIT]
Something like this:
[EDIT2]
export type OnClick = (event: 'onclick', listener: (x: number) => void) => void
export type OnScroll = (event: 'onscroll', listener: (x: string) => void) => void
export type EventListener = OnClick | OnScroll;
const addEventListener: EventListener = (ev, ls) => {
console.log(ev, ls);
return;
}
addEventListener('onclick', )
What I want is to infer the type of the second argument of the function (listener) depending on the value of the first argument (event).
