UNPKG

3.24 kBTypeScriptView Raw
1export = bind;
2
3//#region bind():
4/**
5 * For a given function, creates a bound function that has the same body as the original function.
6 * The this object of the bound function is associated with the specified object, and has the specified initial parameters.
7 * @param thisArg The object to be used as the this object.
8 * @param args Arguments to bind to the parameters of the function.
9 */
10declare function bind<T>(this: T, thisArg: ThisParameterType<T>): OmitThisParameter<T>;
11
12// CallableFunction:
13declare function bind<T, AX extends readonly unknown[], A extends readonly unknown[], R>(
14 this: (this: T, ...args: [...bound: AX, ...args: A]) => R,
15 thisArg: T,
16 ...bound: AX
17): (...args: A) => R;
18
19// NewableFunction:
20declare function bind<AX extends readonly unknown[], A extends readonly unknown[], R>(
21 this: new (...args: [...bound: AX, ...args: A]) => R,
22 thisArg: unknown,
23 ...bound: AX
24): new (...args: A) => R;
25//#endregion
26
27declare namespace bind {
28 //#region bind.call():
29 // CallableFunction:
30 function call<T, AX extends readonly unknown[], A extends readonly unknown[], R>(
31 func: (this: T, ...args: [...bound: AX, ...args: A]) => R,
32 thisArg: T,
33 ...bound: AX
34 ): (...args: A) => R;
35
36 // NewableFunction:
37 function call<AX extends readonly unknown[], A extends readonly unknown[], R>(
38 func: new (...args: [...bound: AX, ...args: A]) => R,
39 thisArg: unknown,
40 ...bound: AX
41 ): new (...args: A) => R;
42 //#endregion
43
44 //#region bind.apply():
45 // CallableFunction:
46 function apply<T, AX extends readonly unknown[], A extends readonly unknown[], R>(
47 func: (this: T, ...args: [...bound: AX, ...args: A]) => R,
48 args: readonly [thisArg: T, ...bound: AX],
49 ): (...args: A) => R;
50
51 // NewableFunction:
52 // TODO: Figure out why this is necessary:
53 function apply<A extends readonly unknown[], R>(
54 func: new (...args: A) => R,
55 args: readonly [thisArg: unknown]
56 ): new (...args: A) => R;
57 function apply<A0, A extends readonly unknown[], R>(
58 func: new (bound_0: A0, ...args: A) => R,
59 args: readonly [thisArg: unknown, bound_0: A0],
60 ): new (...args: A) => R;
61 function apply<A0, A1, A extends readonly unknown[], R>(
62 func: new (bound_0: A0, bound_1: A1, ...args: A) => R,
63 args: readonly [thisArg: unknown, bound_0: A0, bound_1: A1],
64 ): new (...args: A) => R;
65 function apply<A0, A1, A2, A extends readonly unknown[], R>(
66 func: new (bound_0: A0, bound_1: A1, bound_2: A2, ...args: A) => R,
67 args: readonly [thisArg: unknown, bound_0: A0, bound_1: A1, bound_2: A2],
68 ): new (...args: A) => R;
69 function apply<A0, A1, A2, A3, A extends readonly unknown[], R>(
70 func: new (bound_0: A0, bound_1: A1, bound_2: A2, bound_3: A3, ...args: A) => R,
71 args: readonly [thisArg: unknown, bound_0: A0, bound_1: A1, bound_2: A2, bound_3: A3],
72 ): new (...args: A) => R;
73 function apply<AX extends readonly unknown[], A extends readonly unknown[], R>(
74 func: new (...args: [...bound: AX, ...args: A]) => R,
75 args: readonly [thisArg: unknown, ...bound: AX],
76 ): new (...args: A) => R;
77 //#endregion
78}