我正在尝试修剪从 REST API 返回的对象数组中的值。
这是interface
我期待的对象。
interface IProduct {
productId: number;
qty: number;
code: string;
customer: string;
description: string;
}
我正在尝试遍历对象数组并修剪对象的所有值。
products.forEach(record => {
if (record) {
Object.keys(record).map(key => {
record[key] = record[key].trim();
});
}
});
我收到以下错误。
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IProduct'.
No index signature with a parameter of type 'string' was found on type 'IProduct'.
我尝试向对象添加索引签名,但没有运气。[key: string]: string
还有key: {[index: string]: string}
。
有什么我遗漏的吗,我认为这足以让 TS 编译器停止抱怨。
这是对象的参考。
const products: Product[] = [
{
productId: 1,
qty: 100,
code: 'code',
customer: 'customer',
description: 'the description',
},
{
productId: 2,
qty: 102,
code: 'code',
customer: 'customer',
description: 'the description',
},
];
非常感谢