如何将两个类相交作为Typescript中扩展类的基类
我需要创建HOF(高阶函数),它将修改/添加属性到所提供类的原型中。有可能吗如何将两个类相交作为Typescript中扩展类的基类,typescript,types,constructor,casting,extends,Typescript,Types,Constructor,Casting,Extends,我需要创建HOF(高阶函数),它将修改/添加属性到所提供类的原型中。有可能吗 interface IStore { new (): {}; } interface IWatchable { new() : { watch: boolean; }; } const Store = <T extends IStore>(clazz: T): T & IWatchable => { return class extends clazz {
interface IStore {
new (): {};
}
interface IWatchable {
new() : {
watch: boolean;
};
}
const Store = <T extends IStore>(clazz: T): T & IWatchable => {
return class extends clazz {
watch = true;
};
};
class X extends Store(class {})
// ^^^^^^^^^^^^^^
// Base constructors must all have the same return type.
接口晶体管{
新的():{};
}
接口IWatchable{
新的(){
手表:布尔型;
};
}
常量存储=(clazz:T):T&IWatchable=>{
返回类扩展了clazz{
观察=真实;
};
};
类X扩展存储(类{})
// ^^^^^^^^^^^^^^
//基本构造函数必须具有相同的返回类型。
如果您想进行混音,这是可以工作的模式(根据):
如果您想进行混音,这是可以工作的模式(根据):
interface IStore {
new(...a: any[]): {};
}
const Store = <T extends IStore>(clazz: T) => {
return class extends clazz {
watch = true;
};
};
class X extends Store(class { }) { }
interface IWatchable {
new(): {
watch: boolean;
};
}
let w : IWatchable = X //ok