让我说我有以下界面:
export interface MyInterfaceType{
field1: string;
field2: string;
field3: string;
field4: number;
field5: string;
field6: string;
field7: string;
field8: string;
field9: number;
}
现在可以说我有以下temp
变量:
let temp: MyInterfaceType = someVariableOfType_MyInterfaceType as MyInterfaceType;
在上面,我不能像下面那样直接编辑temp
的属性
temp.field5 = xxxxxx // doesnt work
因为我会得到“无法分配为只读对象的属性'field5'”
要绕过此问题,请执行以下操作:
let finalObject: MyInterfaceType = {
field1:temp.field1,
field2:temp.field2,
field3:temp.field3,
field4:temp.field4,
field5:xxxxxx, // something random
field6:temp.field6,
field7:temp.field7,
field8:temp.field8,
field9:temp.field9
};
在类似情况下,是否有一种更干净的方法来编辑接口类型的属性?