I am looking to modify the designated field within an object from a date type to a timestamp
Desired function
let formData = {
username: "tom",
startDate: new Date(),
endtDate: new Date(),
...
}
formData = convertObjectDateToTimestamp(formData, ["startDate", "endDate"]);
// formData
// { username: "tome", startDate: 1629874054212, endDate: 1629874054212};
** Current function **
function convertObjectDateToTimestamp<
T extends Record<string, unknown>,
U extends Array<keyof T>,
>(target: T, keys: U) {
const obj = { ...target };
(Object.keys(target) as Array<keyof T>).forEach((key) => {
const value = obj[key];
if (keys.includes(key) && value instanceof Date) {
obj[key] = value.getTime();
// Type 'number' cannot be assigned to type't [keyof t] '
// alternative:value.getTime() as any
}
});
return obj;
}