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
I've been looking for DI libraries for several days and this is the best I've found so far, but when integrating with typescript it is quite annoying to have to infer the types in each resolve:
interfaceService{
...
}classMyServiceimplementsService{
...
}container.register({service: asClass(MyService))// Returns any typecontainer.resolve('service');// Returns the expected type but it's quite annoying to have to put the type in every resolvecontainer.resolve<Service>('service');
Isn't there a way for the types to be auto-inferred in the register method?
The text was updated successfully, but these errors were encountered:
What you are asking for specifically is not possible since it would require chaining all registrations in order to build the type map.
The next best thing is to define a modules collection type and use that in createContainer:
interfaceModules{service: Service}constcontainer=createContainer<Modules>()container.register({// This should now be typedservice: asClass(MyService)})// Returns expected typecontainer.resolve('service');
However, .resolve (and .cradle) are usually only used for entrypoints and for library integrations, 99% of your code using Awilix will be exclusively DI, and there is no way for Awilix to guarantee that constructor(service: Service) is upheld. However, in practice I have found that is rarely (if ever) an issue.
Awilix makes that trade-off in order to not invade your codebase; in other words, your application code should not be littered with Awilix imports.
I've been looking for DI libraries for several days and this is the best I've found so far, but when integrating with typescript it is quite annoying to have to infer the types in each resolve:
Isn't there a way for the types to be auto-inferred in the register method?
The text was updated successfully, but these errors were encountered: