You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, at the moment it seems like there is no way to override the type for this parameter for the lifecycle methods (created , started and stopped). In consequence, it's impossible to create those methods if they implement a custom this (similarly to how methods and actions do it).
This can be temporarily ignored as suggested in #67, but that's a workaround and not a fix.
I'm realtively new to typescript so I might be missing some obvious solution / best practices related to this, but I think the following could work:
The above code is meant to provide a way to supply additional type for this parameter at the service declaration level (e.g. in greeter.service.ts), which would change it from:
exportinterfaceGreeterSettingsextendsServiceSettingSchema{defaultName: string;}exportinterfaceGreeterMethods{uppercase(str: string): string;}exporttypeGreeterThis=Service<GreeterSettings>&GreeterMethods;constGreeterService: ServiceSchema<GreeterSettings>={name: "greeter",settings: {defaultName: "Moleculer",},dependencies: [],actions: {},events: {},methods: {uppercase(str: string){returnstr.toUpperCase()();},},created(this: GreeterThis){// <-- This does not workconsole.log(this.broker.services);},asyncstarted(this: GreeterThis){// <-- This does not workthis.uppercase('hello world');},asyncstarted(this: GreeterThis){// <-- This does not workthis.uppercase('goodbye world');},};
to
exportinterfaceGreeterSettingsextendsServiceSettingSchema{defaultName: string;}exportinterfaceGreeterMethods{uppercase(str: string): string;}exporttypeGreeterThis=Service<GreeterSettings>&GreeterMethods;constGreeterService: ServiceSchema<GreeterSettings,GreeterThis>={// <-- This is the actual change// in service declarationname: "greeter",settings: {defaultName: "Moleculer",},dependencies: [],actions: {},events: {},methods: {uppercase(str: string){returnstr.toUpperCase()();},},created(this: GreeterThis){console.log(this.broker.services);},asyncstarted(this: GreeterThis){this.uppercase('hello world');},asyncstarted(this: GreeterThis){this.uppercase('goodbye world');},};
Please let me know if you'd like me to create a PR with this change.
The text was updated successfully, but these errors were encountered:
marceliwac
changed the title
Support for types for lifecycle methods (created, started, stopped)
Support for typed "this" in lifecycle methods (created, started, stopped)
Jan 30, 2024
Hi, at the moment it seems like there is no way to override the type for
this
parameter for the lifecycle methods (created
,started
andstopped
). In consequence, it's impossible to create those methods if they implement a customthis
(similarly to how methods and actions do it).This can be temporarily ignored as suggested in #67, but that's a workaround and not a fix.
I'm realtively new to typescript so I might be missing some obvious solution / best practices related to this, but I think the following could work:
index.d.ts
should be changed to:
index.d.ts
The above code is meant to provide a way to supply additional type for
this
parameter at the service declaration level (e.g. in greeter.service.ts), which would change it from:to
Please let me know if you'd like me to create a PR with this change.
The text was updated successfully, but these errors were encountered: