feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.02k stars 745 forks source link

Typesafe service-events #3165

Open psi-4ward opened 1 year ago

psi-4ward commented 1 year ago

It would be very nice to have types on the eventemitter of services. This issue should track the request.

Currently I go with an own helper method on the service which types on():

export type MyService = Static<typeof processSchema>

export type MyServiceEvents = {
  created: MyService;
  removed: MyService;
  // ...
}

export class MyServiceService<ServiceParams extends MyServiceParams = MyServiceParams>
  implements ServiceInterface<MyService, MyServiceData, ServiceParams>
{
  //...

  // Helper method for typed events
  onTyped<EventName extends keyof MyServiceEvents>(eventName: EventName, handler: (payload: MyServiceEvents[EventName]) => void) {
    this.getService().on(eventName, handler);
  }
}

But we should think about to get this into feathers-core.