I currently have a problem changing a detailed model to a less detailed update model when I have more data than expected in an object. Despite Typescript it can be that my backend sends me more data than I expect in my model and then also send back too much data, because I don't know of certain properties that they exist.
Let me give you a small example:
CarModel
- manufacturer
- model
- color
UpdateCarModel
- color
Recived Object from Backend:
- manufacturer
- model
- color
- yearOfConstruction
Here's my current code, which doesnt detect and remove the additional propery:
import { merge as lodashMerge } from 'lodash';
public static buildFromCarModel(car: CarModel): UpdateCarModel {
let clone = lodashMerge(new UpdateCarModel(), car);
delete clone.manufacturer;
delete clone.model;
return clone;
}
The problem is now that the backend expects an update model that only contains the color, but implicitly the year of construction has also been dragged through the code. Is there a way to give the object a model as a kind of template and keep only the properties that are in my expected model?
EDIT: The solution proposed in the marked duplicate is difficult for me to use because I have to specify all properties and additionally all methods, I would prefer a solution that only reads and transmits the properties, but not the functions