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