Following generic function takes a object of functions and returns an object with same propnames valued with returning value of the corresponding function.
const combineValues = obj => {
const res = {};
for (const k in obj){
res[k] = obj[k]()
}
return res;
}
const combined = combineValues({
n: () => 1,
s: () => 's'
}); // { n: 1, s: 's' }
Tryed to define a signature and an implementation for this function in Typescript
const combineValues =
<K extends string> (obj: Record<K, () => any>): Record < K, any > => {
const res = {} as Record<K, any>;
for (const k in obj){
res[k] = obj[k]()
}
return res;
}
const combined = combineValues({
n, s
}) // Record<"n" | "s", any>
but combined doesn't keep original typings for values
const combcombineValues = <K extends string, T>(obj: Record<K, () => T>): Record<K, T> => {
const res = {} as Record<K, T>;
for (const k in obj){
res[k] = obj[k]()
}
return res;
}
works only if all function props return same type T
is it possible to fully define it in Typescript ?