I need to update a Feature object in my code. Here is a simplified version of the structure:
type Feature = {id: number, name: string, deletedAt?: Date}
const newData: Partial<Feature> & {id: number} = {
id: 1, deletedAt: new Date()
};
const oldFeature: Feature = {
id: 1, name: 'Abc', deletedAt: undefined
};
for (const key of Object.keys(newData) as (keyof Feature)[]) {
oldFeature[key] = newData[key];
}
Both oldFeature[key]
and newData[key]
have types of
string | number | Date | undefined
. However, despite having the same possible types for each given key in both objects, I am encountering an error when trying to make the assignment oldFeature[key] = newData[key]
:
Type 'string | number | Date | undefined' is not assignable to type 'never'.
Type 'undefined' is not assignable to type 'never'.ts(2322)
Why is this happening? And what is the proper way to handle this issue?