interface Foo {
id: string;
bar?: number
}
function setIfChanged(newProduct: Foo, existingProduct: Foo, key: keyof Foo): boolean {
if (newProduct[key] !== existingProduct[key]) {
existingProduct[key] = newProduct[key]; // error here
console.log(`Product ${key} updated:`, newProduct);
return true;
}
return false;
}
< /code>
Type 'string | number | undefined' is not assignable to type 'never'.
Type 'undefined' is not assignable to type 'never'
< /code>
I don't want to use // @ts-ignore
Я хочу установить поле, если изменилось. Я попробовал: < /p> [code]interface Foo { id: string; bar?: number } function setIfChanged(newProduct: Foo, existingProduct: Foo, key: keyof Foo): boolean {
if (newProduct[key] !== existingProduct[key]) { existingProduct[key] = newProduct[key]; // error here console.log(`Product ${key} updated:`, newProduct); return true; } return false; } < /code> Type 'string | number | undefined' is not assignable to type 'never'. Type 'undefined' is not assignable to type 'never' < /code> I don't want to use // @ts-ignore[/code] или утверждение типа ([code]as[/code]).