typescript - 编辑接口类型变量的属性
问题描述
假设我有以下界面:
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
};
在类似情况下是否有更简洁的方法来编辑接口类型的属性?
解决方案
推荐阅读
- laravel - Laravel:加密上传的文件/图像
- nginx - Nginx 子目录 404
- c++ - 由于使用科学计数法而出错
- python - 如何正确更改 odoo 12 视图显示?
- javascript - 刷新浏览器时如何将应用程序重定向到主页?
- regex - VBA中的标记数学(中缀)表达式
- keras - 尝试对 ImageDataGenerator().flow_from_directory 应用 k 折交叉验证
- multithreading - 使用 WaitGroups 和 goroutine 优化多线程的正确方法?
- google-colaboratory - 无法从 Google Colaboratory 查询 AWS athena
- r - 基于经验分布和 copula 生成 n-dim 随机样本