I am searching for a method to create a "mapped" object type in TypeScript.
Here are the typings I currently have:
interface Factory<T>{
serialize: (val: T)=>void,
deserialize: ()=>T,
}
interface MyDict{
[key: string]: Factory<any>
}
function deserialize(dict: MyDict){
let mapped = {};
for(let k in dict){
mapped[k] = dict[k].deserialize();
}
return mapped;
}
My goal is to ensure that the returned type of the map function is accurately determined.
For example, when using it like this:
let mapped = map({
foo: {deserialize: ()=>'hello world'},
foo2: {deserialize: ()=>123},
foo3: {deserialize: ()=>({foo4: 'foo4'})}
});
The expected typing for mapped should be
{foo: string, foo2: number, foo3: {foo4: string}}
.