I want to implement a function that loops over the properties of an object and applies updates to another object of the same type.
interface Car {
tires: number;
name: string;
}
function updateCar(car: Car, updates: Partial<Car>) {
Object.entries(updates).forEach(([key, value]) => {
car[key] = value;
})
}
The issue here is that there is an error
No index signature with a parameter of type 'string' was found on type 'Car'
when casting the key to keyof Car
it works, but as soon as using "noImplicitAny": true
there is again an error
Type 'string | number' is not assignable to type 'never'
Is there a way to solve this issue in a type-safe matter. Casting the car to any would work but I'd like to avoid it.
Many thanks Bene
Partial<Car>
would allow me to pass{tires: 4, name: undefined}
. Do you want that? – AndriaObject.assign(car, updates)
– Andria