-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.ts
60 lines (53 loc) · 1.67 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
/**
* Make a Partial<T> type with all its field types as partial - recursively
*/
export type RecursivePartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? RecursivePartial<U>[]
: // eslint-disable-next-line @typescript-eslint/ban-types
T[P] extends object
? RecursivePartial<T[P]>
: T[P];
};
/**
* Nullable<T> could be T or undefined or null
*/
export type Nullable<T> = T | undefined | null;
/**
* Extract the type of object property
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export type PropertyType<T extends object, K extends keyof T> = T[K];
/**
* Extract the type of array item
*/
export type ArrayItemType<T extends unknown[]> = T[number];
/**
* Extract the types array of function parameters
* @deprecated please use ts built-in Parameters<T> instead.
*/
export type ParametersTypes<T> = T extends (...args: infer P) => unknown ? P : [];
/**
* Convert interface to type, so it can be used to work with Record<string, unknown>
* check out this thread for details
* https://github.com./microsoft/TypeScript/issues/15300#issuecomment-760165845
* @example foo as Typify<typeof foo>
*/
export type Typify<T> = { [K in keyof T]: T[K] };
/**
* convert object into typified instance, it's nothing more than `as Typify<typeof foo>`
* just works as an shorthand of explicit cast.
* @param target interface type object
* @returns the target itself, typified
*/
export function typify<T>(target: T): Typify<T> {
return target;
}
/**
* Covert type T to Promise<T> except for Promises
*/
export type Promisify<T> = T extends Promise<unknown>
? T
: T extends boolean
? Promise<boolean>
: Promise<T>;