I'm trying to apply a transformation function (dt: Date) => Date
to all given Date
properties of an object. I think I'm close but still stuck. Note that I'm trying to do it as type-safely as possible.
What I have so far is:
const transform = (dt: Date) => dt;
function process<T extends { [k in K]: Date }, K extends keyof T>(obj: T, ...props: K[]) {
props.forEach(p => (obj[p] = transform(obj[p])));
}
The problem with the above process
function is that it's failing in the assignment obj[p] = transformDate(obj[p])
part saying that:
Type 'Date' is not assignable to type 'T[K]'.
But at the same time it seems to recognize T[K]
is a Date
as it doesn't complain with transform(obj[p])
.
How can I update the above function to work?