AK-21/Graphite-Industrial-Intelligence
0
1type RemoveFromTuple<2 Tuple extends readonly unknown[],3 RemoveCount extends number,4 Index extends 1[] = []5> = Index["length"] extends RemoveCount6 ? Tuple7 : Tuple extends [infer First, ...infer Rest]8 ? RemoveFromTuple<Rest, RemoveCount, [...Index, 1]>9 : Tuple;10 11type ConcatTuples<12 Prefix extends readonly unknown[],13 Suffix extends readonly unknown[]14> = [...Prefix, ...Suffix];15 16type ExtractFunctionParams<T> = T extends (this: infer TThis, ...args: infer P extends readonly unknown[]) => infer R17 ? { thisArg: TThis; params: P; returnType: R }18 : never;19 20type BindFunction<21 T extends (this: any, ...args: any[]) => any,22 TThis,23 TBoundArgs extends readonly unknown[],24 ReceiverBound extends boolean25> = ExtractFunctionParams<T> extends {26 thisArg: infer OrigThis;27 params: infer P extends readonly unknown[];28 returnType: infer R;29}30 ? ReceiverBound extends true31 ? (...args: RemoveFromTuple<P, Extract<TBoundArgs["length"], number>>) => R extends [OrigThis, ...infer Rest]32 ? [TThis, ...Rest] // Replace `this` with `thisArg`33 : R34 : <U, RemainingArgs extends RemoveFromTuple<P, Extract<TBoundArgs["length"], number>>>(35 thisArg: U,36 ...args: RemainingArgs37 ) => R extends [OrigThis, ...infer Rest]38 ? [U, ...ConcatTuples<TBoundArgs, Rest>] // Preserve bound args in return type39 : R40 : never;41 42declare 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 51declare 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 59declare function callBind<const TArgs extends readonly unknown[]>(60 args: [fn: Exclude<TArgs[0], Function>, ...rest: TArgs]61): never;62 63// export as namespace callBind;64export = callBind;65 