我刚刚在TypeScript中找到了我不太了解的code snipped。
export abstract class Presenter<TView> {
public viewModel: TView;
constructor(private template: new() => TView,
) {
}
public reset(): void {
const model = new this.template();
if (this.viewModel == null) {
this.viewModel = model;
} else {
Object.assign(this.viewModel, model);
}
}
}
我在这里特别谈论template:new()=> TView。我可以看到它是一个通用类,如果使用类型参数“ string”,则构造函数中的代码将解析为:
new() => string
这里。
但是我无法帮助自己理解这种感觉。它不是函数声明-在这种情况下我从未见过“ new”。我也不知道是什么:
const model = new this.template();
大于有人可以解释吗?
这意味着template
是一个没有参数的构造函数,当用TView
调用时会返回new
。在打字手册中有here的描述。