If I have an object structured like this
let obj = {
property1:()=>{ return Date()} // for example, it doesn't have to be a date
property2:()=>{ return 1}
}
Now, I want to convert this to the following type structure
{
property1: Date,
property2: number
}
How do I define this in TypeScript? Every approach I've tried so far has not been successful.
I am aware of the properties, so I know that it's something like
type Transform<T> = Record<keyof T, ?>
But how can I individually transform each property so that the final object can be properly typed as well?
Let's consider a more concrete example:
Suppose we are working on a React app.
let dependencies = {user: UserContext}: {[key:string]: React.Context<any>}
We can transform all the React contexts into their actual instances by using something like
Object.entries(contextObject).map(([key, context]) =>{
return {[key]: useContext(context)}
}).reduce((a, b) =>{
return {...a, ...b}
},{})
This result will contain all the transformed properties.
I receive a configuration object and transform the properties while maintaining everything else the same.
This transformation could involve converting some parameters into database tables, converting dependencies for addition to a class, without requiring instantiation.
The process itself is not difficult. The challenge lies in correctly typing the transformed object so that upon completion of the transformation, I have full knowledge of the object's new type.