Support for injections of functions that return litteral objects #1639
Closed
alexis-valois
started this conversation in
Ideas
Replies: 1 comment 4 replies
-
Hey @alexis-valois, it would definitively be a great feature to have. I'm currently focused on internal refactors and fixing bugs, but I think it would make a lot of sense to implement this feature in the future. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I know some JavaScript developpers dislike the use of the "class" and "this" keywords. It has to do with the fact that "this" could lead to some difficult to debug behaviours when passing an instance method to a callback without binding "this" to the class in the constructor, beforehand.
There's other ways to create Object that would not need the use of those keywords.
Here's an example :
Here, the function's argument serves as the object's construction dependencies injection hook, and the TypeScript type "Consumer" refers to its interface.
(Note the absence of "new")
The JavaScript closure mechanism makes it possible to use the returned method "consume" anywhere, even if it references the "someService" variable, as it is accessible in its declaration scope.
(Note the absence of "this")
Also, the "return" statement gives us an elegent solution to export only public stuff, making the prive ones "actually private", even at runtime.
At the moment, i don't think inversify's able to bind such "function object", because the binding interfaces expects "newable" arguments.
Ex :
container.bind<Consumer>(TYPES.Consumer).to(LiteralConsumer ).inSingletonScope();
The above line would lead to "Argument of type '(someService: SomeService) =>Consumer' is not assignable to parameter of type 'Newable'."
Is there a plan to support this kind of bindings in the near future, or have this already been discussed ?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions