callable-instance.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. export const CallableInstance =
  2. /**
  3. * @type {new <Parameters extends Array<unknown>, Result>(property: string | symbol) => (...parameters: Parameters) => Result}
  4. */
  5. (
  6. /** @type {unknown} */
  7. (
  8. /**
  9. * @this {Function}
  10. * @param {string | symbol} property
  11. * @returns {(...parameters: Array<unknown>) => unknown}
  12. */
  13. function (property) {
  14. const self = this
  15. const constr = self.constructor
  16. const proto = /** @type {Record<string | symbol, Function>} */ (
  17. // Prototypes do exist.
  18. // type-coverage:ignore-next-line
  19. constr.prototype
  20. )
  21. const func = proto[property]
  22. /** @type {(...parameters: Array<unknown>) => unknown} */
  23. const apply = function () {
  24. return func.apply(apply, arguments)
  25. }
  26. Object.setPrototypeOf(apply, proto)
  27. const names = Object.getOwnPropertyNames(func)
  28. for (const p of names) {
  29. const descriptor = Object.getOwnPropertyDescriptor(func, p)
  30. if (descriptor) Object.defineProperty(apply, p, descriptor)
  31. }
  32. return apply
  33. }
  34. )
  35. )