I have multiple functions which call each other:
let config;
let callback;
function setConfig(c) {
config = {};
for(const i of c.items) config.items[i] = getData(i);
return {
setCallback: (cb) => { callback = cb; }
};
}
x.addEventListener("someEvent", () => callback(config));
// usage
setConfig({items: ["a", "b", "c"]}).setCallback(function myCallback(config) {
console.log(config.items["a"]);
console.log(config.items["b"]);
console.log(config.items["c"]);
})
Is it possible to type this in a way that the c argument of setConfig is generic (depends on the actual config object) and then automatically infer the type of the config argument of myCallback?
I already tried to put the adding of the event listener into the setCallback function, so I could carry around a generic type for setConfig, I would like to do this in another place and also be able to modify config, but still keep the type inferrence.
itemssupposed to be? What type doesgetData()return?