I am looking for a way of a "mapped" object type in typescript.
I have a the following typings:
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;
}
What I want is that the return type of map is correctly determined.
So when doing this:
let mapped = map({
foo: {deserialize: ()=>'hello world'},
foo2: {deserialize: ()=>123},
foo3: {deserialize: ()=>({foo4: 'foo4'})}
});
mapped should be typed as {foo: string, foo2: number, foo3: {foo4: string}}.
MyDictis not generic (andmapis not generic either) - it's unlikely you can have that.