Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将两个类相交作为Typescript中扩展类的基类_Typescript_Types_Constructor_Casting_Extends - Fatal编程技术网

如何将两个类相交作为Typescript中扩展类的基类

如何将两个类相交作为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 {

我需要创建HOF(高阶函数),它将修改/添加属性到所提供类的原型中。有可能吗

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