I have an object called Item, I have defined an interface that contains its fields, and an array of fields that can be changed and defined. When the user wants to update the item, he sends a POST request with the updated item, and I go through the array and update the fields according to the user's data. The problem is that Typescript throws an error: Type 'string | number 'is not assignable to type' never '. Type 'string' is not assignable to type 'never'.ts (2322)
I would appreciate any help on the subject
interface Item {
id?: number;
title?: string;
price?: number;
}
const requiredItemFields: Array<keyof Item> = [
"title",
"price",
]
app.post('/update-item', (req, res) => {
const existItem: Item = req.body.item;
const newItem: Item = {};
for (let filed of requiredItemFields) {
if (existItem.hasOwnProperty(filed)) {
newItem[filed] = existItem[filed]; << ERROR
}
}
})