我有一个超类是父(Entity
)对于很多子类(Customer
,Product
,ProductCategory
...)
我正在寻找一个可以动态克隆在Typescript中包含不同子对象的对象的方法。
例如:Customer
具有不同的Product
人,具有ProductCategory
var cust:Customer = new Customer ();
cust.name = "someName";
cust.products.push(new Product(someId1));
cust.products.push(new Product(someId2));
为了克隆整个对象树,我在其中创建了一个函数 Entity
public clone():any {
var cloneObj = new this.constructor();
for (var attribut in this) {
if(typeof this[attribut] === "object"){
cloneObj[attribut] = this.clone();
} else {
cloneObj[attribut] = this[attribut];
}
}
return cloneObj;
}
在new
上升时,它被transpiled为JavaScript以下错误:error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
虽然该脚本有效,但我想摆脱已编译的错误