I have a Typescript class:
class Cat {
constructor(
public name: string,
public age: number,
public color: string
) {}
}
const sophie = new Cat('Sophie', 17, 'black')
Now I want to make a copy of the instance, but change one property. If this were a literal object, that would be easy:
const tipper = {...sophie, name: 'Tipper'}
But that doesn't create an instance of the Cat
class. To do that I have to laboriously list every property:
const Tipper = new Cat('Tipper', sophie.age, sophie.color)
Is there a cleaner way to copy a class instance, not literal object, while modifying selected properties?