@injectable()
export class PluginRegistry {
constructor (private readonly autoFactory: AutoFactory) {
}
}
@injectable()
export class ApplicationPluginRegistryMain extends PluginRegistry {
}
container.resolve(ApplicationPluginRegistryMain); // throws
Error: The number of constructor arguments in the derived class ApplicationPluginRegistryMain must be >= than the number of constructor arguments of its base class.
It seems like a bug to throw this in cases where the derived class doesn't provide its own constructor, because intuitively the derived class would defer to its parent class's constructor. This is a problem for me because I'd like users to be able to drop new classes into my system without them needing to provide their own constructors (I don't want them to have to care about what injectables the base class needs).
Looking at the code, it seems this is by design.
// Throw if a derived class does not implement its constructor explicitly
// We do this to prevent errors when a base class (parent) has dependencies
// and one of the derived classes (children) has no dependencies
Hi @matthewjh this is by design as reported by @btkostner but there are a few way to solve this problem as documented in our wiki I will close this issue but please feel free to create a new one if you have more questions.
None of the workarounds I saw seemed to give me what I wanted, which I think is the same thing you're wanting. In my case, just adding @optional() to the base class constructor parameter gets rid of the error, and the parameter is correctly injected when resolving the derived class from the container.
@injectable()
export class PluginRegistry {
constructor (@optional() private readonly autoFactory: AutoFactory) {
}
}
@injectable()
export class ApplicationPluginRegistryMain extends PluginRegistry {
}
container.resolve(ApplicationPluginRegistryMain); // ok now
Most helpful comment
Looking at the code, it seems this is by design.