Although it may seem generic, I am eager to create a class that inherits all props and prototypes from a generic type in this way:
class CustomExtend<T> extends T {
constructor(data: T) {
// finding a workaround to distinguish these two classes
const prototype = { ...CustomExtend.prototype };
Object.assign(prototype, Object.getPrototypeOf(data));
Object.setPrototypeOf(this, prototype);
Object.assign(this, data);
}
CustomMethod() { }
}
From now on, I would be able to create an instance of the CustomExtend
class and utilize the types of both classes like so:
const obj = new CustomExtend<Model>(data);
obj.CustomMethod(); // works fine
obj.ModelMethod(); // excellent!
One solution could be using intersection, similar to this:
const obj: CustomExtend & Model = new CustomExtend(data) as any;
Although this method worked, I feel there might be a better alternative. Any suggestions?