I have a method which takes an object which is a Partial I wuold like to itterate though that object using Object.entries and then based on the key I wuold like to perform some operations. The interface has keys which are string but values which are string|number|string[]|DateTime. I would like the function I pass it to to know whats being passed in from the key.
The code i currently have is
public partialUpdate = async (id: number, fieldsToUpdate: Partial<IClient>) => {
type temp = Partial<Record<keyof IClient, (val : string) => void>>;
const mapping : temp = {
name: (val) => client.setName(val),
website: (val) => client.setWebsite(val),
sic_codes: (val) => client.setSicCodes(val),
phone_number: (val) => client.setPhoneNumber(val),
};
Object.entries(fieldsToUpdate).forEach(([key, value]) => mapping[key](value));
return this.clientRepository.update(id, client);
};
and the interface is
export interface IClient
{
id: number,
creation_date: DateTime,
name:string,
website:string,
phone_number:string,
industry:string,
sic_codes:string[],
}
The problem i'm currently having is if it were all string types then no issue but because of sic_codes being a string[] it will cause a type error. Is there any way to have const mapping to know what type val is from the key?