How would I type the following transformer function?
class MyClass {
public id: string = ''
}
const instance = new MyClass()
function transformer(funcs) {
return Object.fromEntries(
Object.entries(funcs)
.map(([key, func]) => [key, func.bind(instance)])
)
}
The crux of my conundrum: More than just passing muster with the linter and compiler, I want intelligent typing for this, such that passing in an object with various string keys bonded to function values (each which expect a this arg of type MyClass) gets transformed such that the output is identical to the input except that it's had its requisite this param "burnt-in," and this is known to the editor/linter/compiler.
In fact, I can't even solve for the simpler solitary case, a function that takes a single param of a function needing a this of type MyClass, along with any number of additional params, and a certain return type… and it spits back a function that's typed identically, except its this has been "burnt-in."
function transform(fn) {
return fn.bind(new MyClass())
}
Even partial answers or further insight would be helpful here! I'd think that we'd need some clever and deep use of generics here, but don't even exactly know where to start. And any answers that can point to further documentation or reference material on the concepts used are especially appreciated!