【发布时间】:2021-10-03 01:26:25
【问题描述】:
我有一个 Tyescript 项目,其中有一个带有 JSON 对象的类,我想在其中添加一个新属性。
这是我当前的对象:
let files = [ { codCountry: 'CO', fileName: 'CO_SER.csv' },
{ codCountry: 'CO', fileName: 'CO_INS.csv' },
{ codCountry: 'CO', fileName: 'CO_BRA.csv' } ];
这就是我想要实现的目标:
let files = [ { codCountry: 'CO', fileName: 'CO_SER.csv', exists: 1, error: 0 },
{ codCountry: 'CO', fileName: 'CO_INS.csv', exists: 0, error: 0 },
{ codCountry: 'CO', fileName: 'CO_BRA.csv', exists: 0, error: 1 } ];
这就是我正在做的:
files.push({ exists: 1, error: 0 }, { exists: 0, error: 0 }, { exists: 0, error: 1 });
这是它返回的错误:
无法将“{exists: number; error: number;}”类型的参数分配给“{codCountry: string; fileName: string;}”类型的参数。 对象字面量只能指定已知属性,并且类型'{codCountry: string; 中不存在'exists'。文件名:字符串; } '
【问题讨论】:
标签: arrays json typescript