I need to write in typeScript types for a function that accepts a config object, in the properties of which functions are written, and returns an object with the same properties as the config, but the values contain the result of the function:
function init (config) { return Object .keys(config) .reduce((prev, key)=> { prev[key] = config[key]() return prev }, {}) }
Tell me, please, how can TypeScript describe the type of such a function?
Those. The init function will be a library, and other developers will be able to call it, for example, like this:
let temp = init({a:()=>5 as number, b:()=>[3,4] as number[]}).
Then the object will be recorded in temp {a: 5, b: [3,4]} , and I want typescript to automatically deduce its (temp) types, and swear, for example, to call temp.a.map () t. to. The temp property of the object 'a' is a number, not an array. In the parameter of the function init in the property 'a' there was a function that returns a number, therefore after the execution of the function init there will be a number. I want to understand how you can specify the types for the init function, what would it work ...
config
. - Stepan Kasyanenko