andywer / typed-emitter

🔩 Type-safe event emitter interface for TypeScript
MIT License
268 stars 24 forks source link

Type issue with listener parameters #41

Open LiberaTeMetuMortis opened 1 year ago

LiberaTeMetuMortis commented 1 year ago

I have an variable called "event" and it is type is keyof Events

// I have an variable called "event" and it's type is `keyof Events`
emitter.on(event, (...args) => {
// It says args is any[] but we know that it is parameter of any of Events
})

To fix this we can change type definitions. Current type definition is:

on<E extends keyof Events> (event: E, listener: Events[E]): this

but if we change it with

on<E extends keyof Events>(event: E, listener: (...args: Parameters<Events[E]>) => ReturnType<Events[E]>): this

the problem gets solved, now TypeScript knows args is any of the Events

LiberaTeMetuMortis commented 1 year ago

@andywer I'm seeing that this package no longer gets updated but if it is going to be merged I can open a PR that solves the problem above