Reputation: 329
I just found a code snipped in TypeScript what I do not really understand.
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);
}
}
}
I am especially talking about the template: new() => TView here. I can see that it is a generic class and if a type parameter "string" was used, the code in the constructor would resolve to:
new() => string
here.
But I cannot help myself understanding the sense of that. Its not a function declaration - I never saw "new" in such a context. Also I do not know what:
const model = new this.template();
means than. Can anybody explain?
Upvotes: 1
Views: 745
Reputation: 10874
This means that template
is a constructor with no parameters, that returns a TView
when invoked with new
. It is described here in the typescript handbook.
Upvotes: 1