1 | type RemoveFromTuple<
|
---|
2 | Tuple extends unknown[],
|
---|
3 | RemoveCount extends number,
|
---|
4 | Index extends 1[] = []
|
---|
5 | > = Index["length"] extends RemoveCount
|
---|
6 | ? Tuple
|
---|
7 | : Tuple extends [first: unknown, ...infer Rest]
|
---|
8 | ? RemoveFromTuple<Rest, RemoveCount, [...Index, 1]>
|
---|
9 | : Tuple;
|
---|
10 |
|
---|
11 | type ConcatTuples<
|
---|
12 | Prefix extends unknown[],
|
---|
13 | Suffix extends unknown[]
|
---|
14 | > = [...Prefix, ...Suffix];
|
---|
15 |
|
---|
16 | type ReplaceThis<T, NewThis> = T extends (this: infer OldThis, ...args: infer A) => infer R
|
---|
17 | ? (this: NewThis, ...args: A) => R
|
---|
18 | : never;
|
---|
19 |
|
---|
20 | type BindFunction<
|
---|
21 | TThis,
|
---|
22 | T extends (this: TThis, ...args: any[]) => any, // Allow specific types to propagate
|
---|
23 | TBoundArgs extends unknown[],
|
---|
24 | ReceiverBound extends boolean
|
---|
25 | > = ReceiverBound extends true
|
---|
26 | ? (...args: RemoveFromTuple<Parameters<T>, TBoundArgs["length"] & number>) => ReturnType<ReplaceThis<T, TThis>>
|
---|
27 | : (...args: ConcatTuples<[TThis], RemoveFromTuple<Parameters<T>, TBoundArgs["length"] & number>>) => ReturnType<T>;
|
---|
28 |
|
---|
29 | declare function callBind<
|
---|
30 | TThis,
|
---|
31 | T extends (this: TThis, ...args: any[]) => any,
|
---|
32 | TBoundArgs extends Partial<Parameters<T>>
|
---|
33 | >(
|
---|
34 | args: [fn: T, thisArg: TThis, ...boundArgs: TBoundArgs]
|
---|
35 | ): BindFunction<TThis, T, TBoundArgs, true>;
|
---|
36 |
|
---|
37 | declare function callBind<
|
---|
38 | TThis,
|
---|
39 | T extends (this: TThis, ...args: any[]) => any,
|
---|
40 | TBoundArgs extends Partial<Parameters<T>>
|
---|
41 | >(
|
---|
42 | args: [fn: T, ...boundArgs: TBoundArgs]
|
---|
43 | ): BindFunction<TThis, T, TBoundArgs, false>;
|
---|
44 |
|
---|
45 | export as namespace callBind;
|
---|
46 | export = callBind;
|
---|