Repo for reproducing a problem with InversifyJS default values.
To reproduce:
npm start
- Open http://localhost:1234/ in browser.
- Open dev console to observe what is going on.
Expected:
- both
ComponentWithDefault
andComponentWithoutDefault
should get the same config object injected into the constuctor, because they are identified by the sameidentifier
.
Actual:
ComponentWithDefault
ignores bindings set up in the container and the optional parameter always has the default value.
Extra info:
- Inversify determines the number of parameters it has to inject into a function by using
Function.length
- see source code. Function.length
"excludes the rest parameter and only includes parameters before the first one with a default value". So it returns2
forComponentWithoutDefault
's constructor and1
forComponentWithDefault
's constructor. This works as expected.- However, as a result, Inversify only injects 1 parameter into
ComponentWithDefault
. Because of that the 2nd parameter value is never different from default. - One more thing is that metadata seems to not be added to optional constructor parameters. (Possibly, that's why Inversify cannot recognize what kind of dependency it is.) It could relate to how
babel-plugin-transform-typescript-metadata
plugin transpiles TypeScript rather than Inversify directly.