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