How can I ensure that TypeScript is aware of all the inheritances that occur in the Base
variable of the Factory
class so that I don't encounter any errors? It seems like there should be a way to achieve this since I do get the desired result in the end, but I keep getting errors like
Property 'X' does not exist on type 'Rect'
.
interface IPrintable {
print(): void;
}
interface ILoggable {
log(): void;
}
class Factory<T extends new (...args: any) => IShape> {
constructor(public Base: T) {}
Printable() {
this.Base = class extends this.Base implements IPrintable {
print() {
console.log(`${this.x}:${this.y}`);
}
}
return this;
}
Loggable() {
this.Base = class extends this.Base implements ILoggable {
log() {
console.log(`${this.x}:${this.y}`);
}
}
return this;
}
}
interface IShape {
x: number,
y: number
}
class Rect implements IShape {
constructor(
public x: number,
public y: number
) {}
}
const RectMaxed = new Factory(Rect).Printable().Loggable().Base;
const rectMaxed = new RectMaxed(10, 20);
rectMaxed.print();
rectMaxed.log();