-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
46 lines (40 loc) · 1.43 KB
/
index.d.ts
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
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;