-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
97 lines (92 loc) · 3.52 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Bind a function or object deeply
* @module bind-deep
* @author Evelyn Hathaway
* @license MIT
*/
import type {List, Number} from "ts-toolbelt";
/*
Iterate through keys from the object/array/etc.
*/
export declare type BoundDeepProperties<
ToBind,
BoundArguments extends ReadonlyArray<unknown>,
// `object` is being used as the reverse of `Primitive` only for the conditional type, so this is a desired use case
// eslint-disable-next-line @typescript-eslint/ban-types
> = ToBind extends object ? {
[key in keyof ToBind]: BoundDeepFunction<ToBind[key], BoundArguments>;
} : ToBind;
/*
Return a bound function
- These `ts-toolbelt` utils use iteration magic, so if issues arise from long argument lists, type the
function manually
- Removes `this` while also removing the arguments provided to `bindDeep`
- Existing arguments and return value are inferred, so if you will have to manually retype the function if you also
expect these values to be different after binding. `ReturnType<T>` and other TS util types are your friends!
- `this` is removed to allow the bound function to be called (counterintuitive, I know)
*/
export declare type BoundDeepFunction<
ToBind,
BoundArguments extends ReadonlyArray<unknown>,
> = (
// Is callable function? Infer arguments and return values
ToBind extends (...args: infer OriginalArguments) => infer ReturnValue ? (
((...args:
// Leave original arguments if none are being bound
BoundArguments["length"] extends 0 ? OriginalArguments
// Remove the arguments that were bound otherwise
: List.Remove<
OriginalArguments,
"0",
Number.Minus<
Number.NumberOf<BoundArguments["length"]>,
"1"
>
>
) => ReturnValue)
) :
// Is newable function? Infer arguments and return values
ToBind extends new (...args: infer OriginalArguments) => infer ReturnValue ? (
(new (...args:
// Leave original arguments if none are being bound
BoundArguments["length"] extends 0 ? OriginalArguments
// Remove the arguments that were bound otherwise
: List.Remove<
OriginalArguments,
"0",
Number.Minus<
Number.NumberOf<BoundArguments["length"]>,
"1"
>
>
) => ReturnValue)
) : unknown // Not a function
) & BoundDeepProperties<ToBind, BoundArguments>; // Bind properties of the object or return the primitive
/*
Call signature for `bindDeep`
*/
/**
* Bind a function or object deeply
* @function
* @param {Function|Object|Array} object Function or object to bind itself and all of its methods
* @param {Object} thisArg The value bound to `this` for each bound function and method when called
* @param {...*} [args] Arguments provided to the bound function when the bound function is invoked
* @returns {Function|Object|Array} The function or object passed as `object` but with itself and all methods bound to `thisArg`
* @example <caption>Binding a function and methods with arguments</caption>
* const myFunction = function (arg1, arg2) { return this; };
* myFunction.primitive = "string";
* myFunction.method = function (arg1) { return this; };
*
* const newThis = { newThis: "that's me!"};
*
* const boundFunction = bindDeep(myFunction, newThis, "add arg1 for each function");
* boundFunction("arg2 goes here"); // returns `newThis`
* boundFunction.primitive; // still "string"
* boundFunction.method(); // returns `newThis`
*/
export default function bindDeep<
ToBind,
BoundArguments extends ReadonlyArray<unknown>,
> (
object: ToBind, thisArgument: ThisParameterType<ToBind>, ...args: BoundArguments,
) : BoundDeepFunction<ToBind, BoundArguments>;