Bind Every Method to Its Object

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Passing a method on its own, like const { increment } = counter, loses this. Export a type FunctionKeys<T>, the keys of T whose values are functions, and write bindAll(obj): return a new object holding every method of obj, its own function properties and the methods from its class (but not constructor), each bound to obj so it still works when called on its own. Fields that aren't functions are left out. Type the result as exactly the function properties of obj, so reading a field from it is a type error.

Your code must pass

  • Detached methods still use the object
  • Binds own function properties too
  • Leaves out fields that aren't functions
  • Has no type errors
  • FunctionKeys lists only the methods
  • bindAll returns just the bound methods