UNPKG

5.08 kBTypeScriptView Raw
1/**
2 * For a given function, creates a bound function that has the same body as the original function.
3 * The this object of the bound function is associated with the specified object, and has the specified initial parameters.
4 *
5 * Equivalent to:
6 * ```js
7 * Function.prototype.call.bind(target, ...)
8 * ```
9 *
10 * @param target The function to be used as the this object for `Function.prototype.call`.
11 * @param args Arguments to bind to the parameters of the function.
12 */
13declare function callBind<T, A extends unknown[], R>(target: (this: T, ...args: A) => R): (thisArg: T, ...args: A) => R;
14declare function callBind<T, A extends unknown[], R>(target: (this: T, ...args: A) => R, thisArg: T): (...args: A) => R;
15declare function callBind<T, A0, A extends unknown[], R>(
16 target: (this: T, arg0: A0, ...args: A) => R,
17 thisArg: T,
18 arg0: A0,
19): (...args: A) => R;
20declare function callBind<T, A0, A1, A extends unknown[], R>(
21 target: (this: T, arg0: A0, arg1: A1, ...args: A) => R,
22 thisArg: T,
23 arg0: A0,
24 arg1: A1,
25): (...args: A) => R;
26declare function callBind<T, A0, A1, A2, A extends unknown[], R>(
27 target: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
28 thisArg: T,
29 arg0: A0,
30 arg1: A1,
31 arg2: A2,
32): (...args: A) => R;
33declare function callBind<T, A0, A1, A2, A3, A extends unknown[], R>(
34 target: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
35 thisArg: T,
36 arg0: A0,
37 arg1: A1,
38 arg2: A2,
39 arg3: A3,
40): (...args: A) => R;
41declare function callBind<T, AX, R>(
42 target: (this: T, ...args: AX[]) => R,
43 thisArg: T,
44 ...args: AX[]
45): (...args: AX[]) => R;
46
47// tslint:disable-next-line: ban-types
48declare function callBind<F extends Function>(
49 target: F,
50): (
51 thisArg: ThisParameterType<F>,
52 ...args: F extends (...args: infer A) => any ? A : unknown[]
53) => F extends (...args: any) => infer R ? R : any;
54
55// tslint:disable-next-line: ban-types
56declare function callBind<F extends Function>(
57 target: F,
58 thisArg: ThisParameterType<F>,
59): (...args: F extends (...args: infer A) => any ? A : unknown[]) => F extends (...args: any) => infer R ? R : any;
60
61// tslint:disable-next-line: ban-types
62declare function callBind<F extends Function>(
63 target: F,
64 thisArg: ThisParameterType<F>,
65 ...args: unknown[]
66): (...args: unknown[]) => F extends (...args: any) => infer R ? R : any;
67
68declare namespace callBind {
69 /**
70 * For a given function, creates a bound function that has the same body as the original function.
71 * The this object of the bound function is associated with the specified object, and has the specified initial parameters.
72 *
73 * Equivalent to:
74 * ```js
75 * Function.prototype.apply.bind(target, ...)
76 * ```
77 *
78 * @param target The function to be used as the this object for `Function.prototype.apply`.
79 * @param args Arguments to bind to the parameters of the function.
80 */
81 function apply<T, A extends unknown[], R>(target: (this: T, ...args: A) => R): (thisArg: T, args: Readonly<A>) => R;
82 function apply<T, A extends unknown[], R>(target: (this: T, ...args: A) => R, thisArg: T): (args: Readonly<A>) => R;
83 function apply<T, A0, A extends unknown[], R>(
84 target: (this: T, arg0: A0, ...args: A) => R,
85 thisArg: T,
86 arg0: A0,
87 ): (args: Readonly<A>) => R;
88 function apply<T, A0, A1, A extends unknown[], R>(
89 target: (this: T, arg0: A0, arg1: A1, ...args: A) => R,
90 thisArg: T,
91 arg0: A0,
92 arg1: A1,
93 ): (args: Readonly<A>) => R;
94 function apply<T, A0, A1, A2, A extends unknown[], R>(
95 target: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R,
96 thisArg: T,
97 arg0: A0,
98 arg1: A1,
99 arg2: A2,
100 ): (args: Readonly<A>) => R;
101 function apply<T, A0, A1, A2, A3, A extends unknown[], R>(
102 target: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R,
103 thisArg: T,
104 arg0: A0,
105 arg1: A1,
106 arg2: A2,
107 arg3: A3,
108 ): (args: Readonly<A>) => R;
109 function apply<T, AX, R>(target: (this: T, ...args: AX[]) => R, thisArg: T, ...args: AX[]): (args: AX[]) => R;
110
111 // tslint:disable-next-line: ban-types
112 function apply<F extends Function>(
113 target: F,
114 ): (
115 thisArg: ThisParameterType<F>,
116 args: F extends (...args: infer A) => any ? Readonly<A> : readonly unknown[],
117 ) => F extends (...args: any) => infer R ? R : any;
118
119 // tslint:disable-next-line: ban-types
120 function apply<F extends Function>(
121 target: F,
122 thisArg: ThisParameterType<F>,
123 ): (
124 args: F extends (...args: infer A) => any ? Readonly<A> : readonly unknown[],
125 ) => F extends (...args: any) => infer R ? R : any;
126
127 // tslint:disable-next-line: ban-types
128 function apply<F extends Function>(
129 target: F,
130 thisArg: ThisParameterType<F>,
131 ...args: unknown[]
132 ): (args: ArrayLike<unknown>) => F extends (...args: any) => infer R ? R : any;
133}
134
135export = callBind;