I need to perform some transformations on a typescript object while being able to preserve it's full shape.
So, I have an object:
const simpleObject = {
one: () => ({}),
two: (a: number) => ({ a }),
};
type SimpleObject<K, V> = {
[P in keyof K]: V; // Not sure how to type V correctly to be a function that takes any arguments and outputs an object
};
where the values are generic functions that output an object.
I need to perform some transformations on the object, so that:
type AugmentedObject<K, V> = {
[P in keyof K]: V; // again, typing of V is not correct here, it should be a generic function that outputs an object
};
function augmentObject<K, V>(simpleObject: SimpleObject<K, V>): AugmentedObject<K, V> {
// perform some transformation
// ...
return simpleObject;
}
augmentObject(simpleObject).one; // the type should resolve to a () => object
augmentObject(simpleObject).two; // the type should resolve to (a: number) => object
I cannot figure out how to maintain the correct typing for one or two as I can't figure out how to generically pass a function