I'm trying to figure out how to implement the transformer
function in JavaScript. This function is supposed to take an object that contains various string keys bonded to functions, and transform it such that the functions expect a 'this' argument of type 'MyClass'.
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)])
)
}
My challenge lies in achieving intelligent typing for this function so that the editor/linter/compiler recognizes that the output maintains the same structure as the input but with the necessary 'this' parameter modified. I'm also struggling with a simpler case where I need to create a function that binds a single function needing a 'this' of type 'MyClass' along with additional parameters and return types.
function transform(fn) {
return fn.bind(new MyClass())
}
I would greatly appreciate any pointers or insights on how to approach this problem. Generics may play a key role here, but I'm unsure where to begin. Any suggestions for further reading materials on this topic would be highly valued!