-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_types.ts
84 lines (65 loc) · 2.2 KB
/
_types.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.
type AnyFn<R = unknown> = (...args: any) => R;
type IsNever<T> = [T] extends [never] ? true : false;
type IsArityX<T extends AnyFn, X extends number> =
Parameters<T>["length"] extends X ? true : false;
type Parameter<T extends AnyFn, X extends number> = Parameters<T> extends []
? never
: Parameters<T>[X];
type FirstParameter<T extends AnyFn> = Parameter<T, 0>;
type PickByFirstParameter<T extends Record<string, AnyFn>, U> = {
[k in keyof T as (U extends FirstParameter<T[k]> ? k : never)]: T[k];
};
type Resolve<T> = T extends Promise<infer X> ? X : T;
type IsPromise<T> = T extends Promise<unknown> ? true : false;
type ReturnTypePromisifyMap<T extends Record<PropertyKey, AnyFn>> = {
[k in keyof T]: (...args: Parameters<T[k]>) => Promise<ReturnType<T[k]>>;
};
type Shift<T> = T extends [unknown, ...infer Rest] ? Rest : never;
type ShiftFnArg<T extends AnyFn> = IsNever<Shift<Parameters<T>>> extends true
? () => ReturnType<T>
: (...args: Shift<Parameters<T>>) => ReturnType<T>;
type Writable<T> = {
-readonly [k in keyof T]: T[k];
};
/** Alias for `Tuple` */
type IsTuple<T extends readonly unknown[]> = number extends T["length"] ? false
: true;
type Primitive = string | number | bigint | symbol | boolean | null | undefined;
type PickOf<T, U> = {
[k in keyof T as (T[k] extends U ? k : never)]: T[k];
};
type OverwriteOf<T, U> = {
[k in keyof T]: U;
};
type Rename<T, From extends PropertyKey, To extends PropertyKey> = {
[k in keyof T as (k extends From ? To : k)]: T[k];
};
type ArrayOfLength<N extends number, C extends any[] = []> = C["length"] extends
N ? C : ArrayOfLength<N, [...C, any]>;
type Minus<N extends number> = ArrayOfLength<N> extends [any, ...infer Rest]
? Rest["length"]
: never;
type PartialByKeys<T, K extends PropertyKey = keyof T> = {
[P in keyof (Omit<T, K> & Partial<Pick<T, keyof T & K>>)]: T[P];
};
export type {
AnyFn,
FirstParameter,
IsArityX,
IsPromise,
IsTuple,
Minus,
OverwriteOf,
PartialByKeys,
PickByFirstParameter,
PickOf,
Primitive,
Rename,
Resolve,
ReturnTypePromisifyMap,
Shift,
ShiftFnArg,
Writable,
};