How can I copy/clone a typescript class? The parent class contains a nested class and also a collection of the nested class. Each type of class contains a getter. I would like if the copied class did not lose the getters, and the nested class and the array items have a new reference.
My problems are, the JSON.parse(JSON.stringify( obj )); not copied the getters. and the Object.assign(target, soure); copied the getters, but the array items keep the original references.
Here is the structure of classes
export interface IClassA {
code: number;
description: string;
}
export class ClassA implements IClassA {
code: number;
description: string;
get descrAndCode() {
return 'Getter A ' + this.description + ':' + this.code;
}
}
export interface IClassB {
name: string;
code: number;
classList: Array<ClassA>;
}
export class ClassB implements IClassB {
name: string;
code: number;
get codeAndName(): string {
return 'Getter B' + this.code + ':' + this.name;
}
nested: ClassA;
classList: Array<ClassA>;
}