Suppose I have a Typescript class like the following:
class MyClass {
constructor(public x: boolean, public y: string, public z: number) {}
}
I create an instance of the class:
const myInstance = new MyClass(true, 'hi', 45);
If I want to copy the values of only the properties x
and z
to another object, I could do the following:
const myInstance = new MyClass(true, 'hi', 45);
const { x, z } = myInstance;
const myObject = { x, z };
Is there any better (and simple) way of achieving this?
Or is this the simplest way?
Note, the above example class is small. Imagine if the class has, say, 20 properties, and I want to get only 5 of them.
It would be good to see a generic solution to the problem.