UNPKG

7.02 kBTypeScriptView Raw
1//#region bind():
2/**
3 * For a given function, creates a bound function that has the same body as the original function.
4 * The this object of the bound function is associated with the specified object, and has the specified initial parameters.
5 * @param thisArg The object to be used as the this object.
6 * @param args Arguments to bind to the parameters of the function.
7 */
8declare function bind<T>(this: T, thisArg: ThisParameterType<T>): OmitThisParameter<T>;
9declare function bind<T, A0, A extends any[], R>(
10 this: (this: T, arg0: A0, ...args: A) => R,
11 thisArg: T,
12 arg0: A0,
13): (...args: A) => R;
14declare function bind<T, A0, A1, A extends any[], R>(
15 this: (this: T, arg0: A0, arg1: A1, ...args: A) => R,
16 thisArg: T,
17 arg0: A0,
18 arg1: A1,
19): (...args: A) => R;
20declare function bind<T, A0, A1, A2, A extends any[], R>(
21 this: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
22 thisArg: T,
23 arg0: A0,
24 arg1: A1,
25 arg2: A2,
26): (...args: A) => R;
27declare function bind<T, A0, A1, A2, A3, A extends any[], R>(
28 this: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
29 thisArg: T,
30 arg0: A0,
31 arg1: A1,
32 arg2: A2,
33 arg3: A3,
34): (...args: A) => R;
35declare function bind<T, AX, R>(this: (this: T, ...args: AX[]) => R, thisArg: T, ...args: AX[]): (...args: AX[]) => R;
36
37declare function bind<A0, A extends any[], R>(
38 this: new (arg0: A0, ...args: A) => R,
39 thisArg: any,
40 arg0: A0,
41): new (...args: A) => R;
42declare function bind<A0, A1, A extends any[], R>(
43 this: new (arg0: A0, arg1: A1, ...args: A) => R,
44 thisArg: any,
45 arg0: A0,
46 arg1: A1,
47): new (...args: A) => R;
48declare function bind<A0, A1, A2, A extends any[], R>(
49 this: new (arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
50 thisArg: any,
51 arg0: A0,
52 arg1: A1,
53 arg2: A2,
54): new (...args: A) => R;
55declare function bind<A0, A1, A2, A3, A extends any[], R>(
56 this: new (arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
57 thisArg: any,
58 arg0: A0,
59 arg1: A1,
60 arg2: A2,
61 arg3: A3,
62): new (...args: A) => R;
63declare function bind<AX, R>(this: new (...args: AX[]) => R, thisArg: any, ...args: AX[]): new (...args: AX[]) => R;
64//#endregion
65
66declare namespace bind {
67 //#region bind.call():
68 /**
69 * Creates a bound function with the specified object as the this value and the specified rest arguments as the arguments.
70 * @param thisArg The object to be used as the this object.
71 * @param args Argument values to be passed to the function.
72 */
73 // CallableFunction:
74 function call<T, A extends any[], R>(func: (this: T, ...args: A) => R, thisArg: T): (...args: A) => R;
75 function call<T, A0, A extends any[], R>(
76 func: (this: T, arg0: A0, ...args: A) => R,
77 thisArg: T,
78 arg0: A0,
79 ): (...args: A) => R;
80 function call<T, A0, A1, A extends any[], R>(
81 func: (this: T, arg0: A0, arg1: A1, ...args: A) => R,
82 thisArg: T,
83 arg0: A0,
84 arg1: A1,
85 ): (...args: A) => R;
86 function call<T, A0, A1, A2, A extends any[], R>(
87 func: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
88 thisArg: T,
89 arg0: A0,
90 arg1: A1,
91 arg2: A2,
92 ): (...args: A) => R;
93 function call<T, A0, A1, A2, A3, A extends any[], R>(
94 func: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
95 thisArg: T,
96 arg0: A0,
97 arg1: A1,
98 arg2: A2,
99 arg3: A3,
100 ): (...args: A) => R;
101 function call<T, AX, R>(func: (this: T, ...args: AX[]) => R, thisArg: T, ...args: AX[]): (...args: AX[]) => R;
102
103 // NewableFunction:
104 function call<A extends any[], R>(func: new (...args: A) => R, thisArg: unknown): new (...args: A) => R;
105 function call<A0, A extends any[], R>(
106 func: new (arg0: A0, ...args: A) => R,
107 thisArg: unknown,
108 arg0: A0,
109 ): new (...args: A) => R;
110 function call<A0, A1, A extends any[], R>(
111 func: new (arg0: A0, arg1: A1, ...args: A) => R,
112 thisArg: unknown,
113 arg0: A0,
114 arg1: A1,
115 ): new (...args: A) => R;
116 function call<A0, A1, A2, A extends any[], R>(
117 func: new (arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
118 thisArg: unknown,
119 arg0: A0,
120 arg1: A1,
121 arg2: A2,
122 ): new (...args: A) => R;
123 function call<A0, A1, A2, A3, A extends any[], R>(
124 func: new (arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
125 thisArg: unknown,
126 arg0: A0,
127 arg1: A1,
128 arg2: A2,
129 arg3: A3,
130 ): new (...args: A) => R;
131 function call<AX, R>(func: new (...args: AX[]) => R, thisArg: unknown, ...args: AX[]): new (...args: AX[]) => R;
132 //#endregion
133
134 //#region bind.apply():
135 /**
136 * Creates a bound function with the specified object as the this value and the elements of specified array as the arguments.
137 * @param thisArg The object to be used as the this object.
138 * @param args An array of argument values to be passed to the function.
139 */
140 // CallableFunction:
141 function apply<T, A extends any[], R>(func: (this: T, ...args: A) => R, args: [T]): (...args: A) => R;
142 function apply<T, A0, A extends any[], R>(
143 func: (this: T, arg0: A0, ...args: A) => R,
144 args: [T, A0],
145 ): (...args: A) => R;
146 function apply<T, A0, A1, A extends any[], R>(
147 func: (this: T, arg0: A0, arg1: A1, ...args: A) => R,
148 args: [T, A0, A1],
149 ): (...args: A) => R;
150 function apply<T, A0, A1, A2, A extends any[], R>(
151 func: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
152 args: [T, A0, A1, A2],
153 ): (...args: A) => R;
154 function apply<T, A0, A1, A2, A3, A extends any[], R>(
155 func: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
156 args: [T, A0, A1, A2, A3],
157 ): (...args: A) => R;
158 function apply<T, AX, R>(func: (this: T, ...args: AX[]) => R, args: [T, ...AX[]]): (...args: AX[]) => R;
159
160 // NewableFunction:
161 function apply<A extends any[], R>(func: new (...args: A) => R, args: [unknown]): new (...args: A) => R;
162 function apply<A0, A extends any[], R>(
163 func: new (arg0: A0, ...args: A) => R,
164 args: [unknown, A0],
165 ): new (...args: A) => R;
166 function apply<A0, A1, A extends any[], R>(
167 func: new (arg0: A0, arg1: A1, ...args: A) => R,
168 args: [unknown, A0, A1],
169 ): new (...args: A) => R;
170 function apply<A0, A1, A2, A extends any[], R>(
171 func: new (arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
172 args: [unknown, A0, A1, A2],
173 ): new (...args: A) => R;
174 function apply<A0, A1, A2, A3, A extends any[], R>(
175 func: new (arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
176 args: [unknown, A0, A1, A2, A3],
177 ): new (...args: A) => R;
178 function apply<AX, R>(func: new (...args: AX[]) => R, args: [unknown, ...AX[]]): new (...args: AX[]) => R;
179 //#endregion
180}
181
182export = bind;