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});