While this response may not directly address the question at hand, I would like to share my approach. In my TypeScript projects, I typically utilize classes for object definitions and constructors for deep copying objects of the same type. This allows for passing objects of the same type or their attributes directly using two different constructors.
export class MyObject {
myAttribute: string;
otherAttributes: Array<string>;
constructor();
constructor(myAttributeOrObj?: MyObject | string);
constructor(myAttributeOrObj?: any, otherAttributes: Array<string>) {
if (myAttributeOrObj && myAttributeOrObj.myAttribute) {
this.myAttribute = myAttributeOrObj.myAttribute;
this.createOtherAttributes(myAttributeOrObj.otherAttributes);
} else {
this.myAttribute = myAttributeOrObj;
this.createOtherAttributes(otherAttributes);
}
}
createOtherAttributes(arr) {
this.otherAttributes = [];
for (var i = 0; i < arr.length; i++) {
this.otherAttributes.push(arr[i]);
}
}
}
Although this method might introduce some overhead compared to utilizing existing libraries, it gives you complete control over your objects, types, and ensures a deep copy instead of a shallow one.
For more insights on constructor overload in TypeScript, refer to this informative discussion Constructor overload in TypeScript.