TypeScript has convenient "parameter property" shorthand for class constructors so that I don't have to explicitly define every property, like this:
class Pizza {
constructor(
private sauce: string,
private cheese: string,
public radius: number
) {}
}
However, I can also use a constructor that takes an object rather than an argument list (essentially, keyword arguments):
const pizza = new Pizza({sauce: 'marinara', cheese: 'Venezuelan Beaver', radius: 8})
This syntax has a number of conveniences, not least of which is the ability to use the spread operator to easily copy objects:
const anotherPizza = new Pizza({...pizza, radius: 10})
But unfortunately it means I can't use the constructor shortcut to avoid all the boilerplate (I think):
class Pizza {
private sauce: string;
private cheese: string;
private radius: number;
constructor({sauce, cheese, radius}: {
sauce: string,
cheese: string,
radius: number
}) {
this.sauce = sauce;
this.cheese = cheese;
this.radius = radius;
}
}
Is there some way to use an object-literal constructor like this and still avoid assigning every property explicitly?