| 1 | type RemoveFromTuple<
|
|---|
| 2 | Tuple extends readonly unknown[],
|
|---|
| 3 | RemoveCount extends number,
|
|---|
| 4 | Index extends 1[] = []
|
|---|
| 5 | > = Index["length"] extends RemoveCount
|
|---|
| 6 | ? Tuple
|
|---|
| 7 | : Tuple extends [infer First, ...infer Rest]
|
|---|
| 8 | ? RemoveFromTuple<Rest, RemoveCount, [...Index, 1]>
|
|---|
| 9 | : Tuple;
|
|---|
| 10 |
|
|---|
| 11 | type ConcatTuples<
|
|---|
| 12 | Prefix extends readonly unknown[],
|
|---|
| 13 | Suffix extends readonly unknown[]
|
|---|
| 14 | > = [...Prefix, ...Suffix];
|
|---|
| 15 |
|
|---|
| 16 | type ExtractFunctionParams<T> = T extends (this: infer TThis, ...args: infer P extends readonly unknown[]) => infer R
|
|---|
| 17 | ? { thisArg: TThis; params: P; returnType: R }
|
|---|
| 18 | : never;
|
|---|
| 19 |
|
|---|
| 20 | type BindFunction<
|
|---|
| 21 | T extends (this: any, ...args: any[]) => any,
|
|---|
| 22 | TThis,
|
|---|
| 23 | TBoundArgs extends readonly unknown[],
|
|---|
| 24 | ReceiverBound extends boolean
|
|---|
| 25 | > = ExtractFunctionParams<T> extends {
|
|---|
| 26 | thisArg: infer OrigThis;
|
|---|
| 27 | params: infer P extends readonly unknown[];
|
|---|
| 28 | returnType: infer R;
|
|---|
| 29 | }
|
|---|
| 30 | ? ReceiverBound extends true
|
|---|
| 31 | ? (...args: RemoveFromTuple<P, Extract<TBoundArgs["length"], number>>) => R extends [OrigThis, ...infer Rest]
|
|---|
| 32 | ? [TThis, ...Rest] // Replace `this` with `thisArg`
|
|---|
| 33 | : R
|
|---|
| 34 | : <U, RemainingArgs extends RemoveFromTuple<P, Extract<TBoundArgs["length"], number>>>(
|
|---|
| 35 | thisArg: U,
|
|---|
| 36 | ...args: RemainingArgs
|
|---|
| 37 | ) => R extends [OrigThis, ...infer Rest]
|
|---|
| 38 | ? [U, ...ConcatTuples<TBoundArgs, Rest>] // Preserve bound args in return type
|
|---|
| 39 | : R
|
|---|
| 40 | : never;
|
|---|
| 41 |
|
|---|
| 42 | declare function callBind<
|
|---|
| 43 | const T extends (this: any, ...args: any[]) => any,
|
|---|
| 44 | Extracted extends ExtractFunctionParams<T>,
|
|---|
| 45 | const TBoundArgs extends Partial<Extracted["params"]> & readonly unknown[],
|
|---|
| 46 | const TThis extends Extracted["thisArg"]
|
|---|
| 47 | >(
|
|---|
| 48 | args: [fn: T, thisArg: TThis, ...boundArgs: TBoundArgs]
|
|---|
| 49 | ): BindFunction<T, TThis, TBoundArgs, true>;
|
|---|
| 50 |
|
|---|
| 51 | declare function callBind<
|
|---|
| 52 | const T extends (this: any, ...args: any[]) => any,
|
|---|
| 53 | Extracted extends ExtractFunctionParams<T>,
|
|---|
| 54 | const TBoundArgs extends Partial<Extracted["params"]> & readonly unknown[]
|
|---|
| 55 | >(
|
|---|
| 56 | args: [fn: T, ...boundArgs: TBoundArgs]
|
|---|
| 57 | ): BindFunction<T, Extracted["thisArg"], TBoundArgs, false>;
|
|---|
| 58 |
|
|---|
| 59 | declare function callBind<const TArgs extends readonly unknown[]>(
|
|---|
| 60 | args: [fn: Exclude<TArgs[0], Function>, ...rest: TArgs]
|
|---|
| 61 | ): never;
|
|---|
| 62 |
|
|---|
| 63 | // export as namespace callBind;
|
|---|
| 64 | export = callBind;
|
|---|