feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Typesafe service-events

Open psi-4ward opened this issue 2 years ago • 0 comments

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.

psi-4ward avatar Apr 18 '23 19:04 psi-4ward