Closed
Description
I would like to have some feature that infers any possible types from the given object in the generic function.
For instance I have a function that receives Object, and I want to have a more specific callback than 'any'.
Current code:
function a(obj: Object, f: (value: any) => any) {
for(var p in obj) if(obj.hasOwnProperty(p)) {
(<any>obj)[p] = f((<any>obj)[p])
}
}
a({x:1}, (value: any) => (<number>value)+1);
I wish I had something with more strict typing.
function a<T extends Object, Value: union_of_properties_types_of(T)>(obj: T, f: (value: Value) => Value) {
for(var p in obj) if(obj.hasOwnProperty(p)) {
(<any>obj)[p] = f(<Value>(<any>obj)[p])
}
}
// 'f' is denoted to number => number
a({x:1}, value => value+1);
// 'f' is string => string
a({x:"x", y:"y"}, value => {
return value+"z";
});