I have a Typescript application where there is a lot of code like this:
class Model {
prop1: string;
prop2: string;
prop3: string;
constructor(input: ModelInput) {
this.prop1 = input.prop1;
this.prop2 = input.prop1;
this.prop3 = input.prop1;
}
}
type ModelInput = {
prop1: string,
prop2: string,
prop3: string,
}
I'd like to remove some of the boilerplate, specifically by replacing the constructor assignments with:
Object.assign(this, input);
This guarantees that the input fields are set on the constructed instance. However, Typescript doesn't seem to recognize the effect of this. It complains:
Property 'prop{1,2,3}' has no initializer and is not definitely assigned in the constructor.
Is there any way around this problem, or am I stuck repeating every property name in the constructor?