I am trying to trim the values from an array of objects that is returned from a REST API.
This is the interface
for the object I'm expecting.
interface IProduct {
productId: number;
qty: number;
code: string;
customer: string;
description: string;
}
I'm trying to loop through the array of objects and trim all the values of the object.
products.forEach(record => {
if (record) {
Object.keys(record).map(key => {
record[key] = record[key].trim();
});
}
});
I'm getting the following error.
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'.
I've tried adding an index signature to the object but had no luck. [key: string]: string
and also key: {[index: string]: string}
.
Is there something I'm missing, I thought this would be enough to make the TS compiler to stop complaining.
Here is a reference of the object.
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',
},
];
Many thanks
The definition for
Object.keys
is:This is because you can write code like this:
To fix your error, you could use a type assertion:
However, if you know there will be no extra keys, you could add this overload:
This way, you won't need a type assertion, but this is technically not type safe.