I have a Typescript cat class:
class Kitty {
constructor(
public name: string,
public age: number,
public color: string
) {}
}
const mittens = new Kitty('Mittens', 5, 'gray')
Now I want to create a clone of the instance, but update one property. If this were a plain object, it would be simple:
const whiskers = {...mittens, name: 'Whiskers'}
But that doesn't result in an instance of the Kitty
class. To achieve that, I need to manually specify each property:
const Whiskers = new Kitty('Whiskers', mittens.age, mittens.color)
Is there a more efficient way to duplicate a class instance, rather than a plain object, while modifying specific properties?