我想初始化一个命令如下:
const command = new CreateProductCommand(payload);
// In another file
export class Payload {
public readonly type: string;
public readonly name?: string;
}
我的命令定义如下:
export class CreateProductTypeACommand {
public readonly name?: string;
}
我尝试在构造函数中创建一个抽象命令来执行此操作:
export class AbstractCommand<T> implements ICommand {
constructor(args: T) {
Object.assign(this, args);
}
}
// in another file
export class CreateProductTypeACommand extends AbstractCommand<CreateProductTypeACommand> {
public readonly name?: string;
}
但这不起作用:孩子的构造函数覆盖了父母,并且 name 的值被
undefined
覆盖。此外,该对象最终具有 type
字段。
有什么想法吗?我宁愿不在我的命令类中放置任何样板代码或定义
CreateProductTypeACommandArgs
类型。