I am writing a package and the base class which a user usually uses to access the package functions doesn’t have any members just accepts a parameter in the constructor and is acting as a proxy for other classes with members. So the base class will act like a funnel for other classes and their members to pass on one class at a time when user wants to access it.
Now there can be a number of classes other than the base class and when a user passes the name of a particular class can somehow get to access members of that class.
There can only be one class proxied at a time. I was wondering if there is a way to inherit a class conditionally and provide its members when the parent class's instance is created. For example
class A {
classAFunction() {
console.log('Class A')
}
}
class B {
classBFunction() {
console.log('Class B')
}
}
class C {
constructor(classToInherit: string) {
if(classToInherit === 'A') {
// inherit class A and provide its functions
} else {
// inherit class B and provide its functions
}
}
}
Inherit in such a way that when class C is instantiated all the inherited members are available. For example
const classC = new C('A')
classC.classAFunction()
I am not sure if it's the right way of doing as I have shown in the examples but the basic idea is to bind other class members with the current class in the constructor so they are accessible when creating instance of class C in the example.
Thank you very much
Edit: I was wondering if it’s possible to pass the constructor parameters from the base class to the class it will extend.