| [a762898] | 1 | /**
|
|---|
| 2 | * Creates a function that invokes the method at `object[key]` with `partialArgs` prepended to the arguments it receives.
|
|---|
| 3 | *
|
|---|
| 4 | * This method differs from `bind` by allowing bound functions to reference methods that may be redefined or don't yet exist.
|
|---|
| 5 | *
|
|---|
| 6 | * The `bindKey.placeholder` value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
|
|---|
| 7 | *
|
|---|
| 8 | * @template T - The type of the object to bind.
|
|---|
| 9 | * @template K - The type of the key to bind.
|
|---|
| 10 | * @param {T} object - The object to invoke the method on.
|
|---|
| 11 | * @param {K} key - The key of the method.
|
|---|
| 12 | * @param {...any} partialArgs - The arguments to be partially applied.
|
|---|
| 13 | * @returns {T[K] extends (...args: any[]) => any ? (...args: any[]) => ReturnType<T[K]> : never} - Returns the new bound function.
|
|---|
| 14 | *
|
|---|
| 15 | * @example
|
|---|
| 16 | * const object = {
|
|---|
| 17 | * user: 'fred',
|
|---|
| 18 | * greet: function (greeting, punctuation) {
|
|---|
| 19 | * return greeting + ' ' + this.user + punctuation;
|
|---|
| 20 | * },
|
|---|
| 21 | * };
|
|---|
| 22 | *
|
|---|
| 23 | * let bound = bindKey(object, 'greet', 'hi');
|
|---|
| 24 | * bound('!');
|
|---|
| 25 | * // => 'hi fred!'
|
|---|
| 26 | *
|
|---|
| 27 | * object.greet = function (greeting, punctuation) {
|
|---|
| 28 | * return greeting + 'ya ' + this.user + punctuation;
|
|---|
| 29 | * };
|
|---|
| 30 | *
|
|---|
| 31 | * bound('!');
|
|---|
| 32 | * // => 'hiya fred!'
|
|---|
| 33 | *
|
|---|
| 34 | * // Bound with placeholders.
|
|---|
| 35 | * bound = bindKey(object, 'greet', bindKey.placeholder, '!');
|
|---|
| 36 | * bound('hi');
|
|---|
| 37 | * // => 'hiya fred!'
|
|---|
| 38 | */
|
|---|
| 39 | declare function bindKey(object: object, key: string, ...partialArgs: any[]): (...args: any[]) => any;
|
|---|
| 40 | declare namespace bindKey {
|
|---|
| 41 | var placeholder: typeof bindKeyPlaceholder;
|
|---|
| 42 | }
|
|---|
| 43 | declare const bindKeyPlaceholder: unique symbol;
|
|---|
| 44 |
|
|---|
| 45 | export { bindKey };
|
|---|