I’m trying to create a generic bind
function for TypeScript that will apply a function with any number of parameters to a specific this
value.
Without types, the idea is simple:
function bind(self, fn) {
return (...args) => fn.apply(self, ...args);
}
But I can’t seem to string together the right types to satisfy the type system.
function bind<TThis, TFunction extends Function>(self: TThis, fn: TFunction):
(this: TThis, ...args: Parameters<TFunction>) => ReturnType<TFunction> {
return (...args: any[]) => fn.apply(self, ...args);
}