In my current project, I am dealing with a class hierarchy that looks like this:
class Base {
public BaseMethod() {
// doesSomeStuff
}
}
class Derived extends Base {
constructor() {
super();
}
public DerivedMethod() {
// do some stuff
BaseMethod();
}
}
The issue I'm facing is that during runtime, I encounter an instance of the Base
class and I need to somehow extend that instance to be of type Derived
. Unfortunately, I don't have control over the construction of Base
nor am I the owner of the class itself.
I'm trying to figure out how I can dynamically extend that instance at runtime so that it transforms into an instance of type Derived
.
function someFunction(base: Base) {
let derived: Derived = extend(base);
derived.BaseMethod();
derived.DerivedMethod();
}
function extend(base: Base): Derived {
// What should I implement here?
}
PS: It's important that I target ES5 for this solution!