I two objects with many properties. I want to copy properties from one object to the other only if it's defined.
Here is the object interface:
interface Settings {
locale: string;
currency: string;
style: string;
isDark: boolean;
// There are more fields here
}
The two objects are defined like this:
const settings: Settings = {/*assign all fields here*/}
const settingsChange: Partial<Settings> = {/*change some fields*/}
Now I need to assign the fields in settingsChange
to settings
, I am trying to do this just like I were to do it in javascript
Object.entries(settingsChange).forEach(([key, value])=>{
settings[key] = settingsChange[key] || settings[key]
})
Here is the typescript linting error I receive:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Settings'.
No index signature with a parameter of type 'string' was found on type 'Settings'.ts(7053)
How do I solve this ? I know that Object.assign
would work but Object.entries
gives me more freedom to apply whatever logic I need.