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 键入脚本:新建()=>;具有受保护构造函数的T参数_Typescript_Generics_Typescript Generics - Fatal编程技术网

Typescript 键入脚本:新建()=>;具有受保护构造函数的T参数

Typescript 键入脚本:新建()=>;具有受保护构造函数的T参数,typescript,generics,typescript-generics,Typescript,Generics,Typescript Generics,我试图强迫用户在实例化子类时提供所有必要的参数,而不需要在每个子类中使用构造函数(params:Partial)。以下系统在公共构造函数可用时工作,但在受保护时不工作 基类: abstract class Message { messageType: number = 0; (((protected))) constructor() {} static construct<T>(this: new () => T, params: Omit<T

我试图强迫用户在实例化子类时提供所有必要的参数,而不需要在每个子类中使用
构造函数(params:Partial)
。以下系统在公共构造函数可用时工作,但在受保护时不工作

基类:

abstract class Message {
    messageType: number = 0;

    (((protected))) constructor() {}

    static construct<T>(this: new () => T, params: Omit<T, 'messageType'>): T {
        return Object.assign(new this(), params);
    }
}
实例化:

MessageExample.construct({field: 123});
是否有一种方法允许使用受保护的构造函数将
MessageExample
传递到
construct()
(并且可能阻止传递抽象类)

MessageExample.construct({field: 123});