I'm trying to have a constructor on my class where I don't need to manually type out every field like so:
this.x = data.x;
this.y = data.y;
Instead I'd like to assign all fields at once from a single object. I found examples online that suggested the following method:
export class GameState {
x: number;
y: number;
constructor(fields: GameState) {
Object.assign(this, fields);
}
}
However my code won't compile. I'm getting the following error:
Property 'x' has no initializer and is not definitely assigned in the constructor.
Property 'y' has no initializer and is not definitely assigned in the constructor.
Is this a viable approach and if so what am I missing?
If not, what's a better approach?