-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.d.ts
146 lines (126 loc) · 3.24 KB
/
util.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* null or undefined
*/
export type Nil = null | undefined;
/**
* Types that can be expressed as a literal
*/
export type LiteralTypes = Nil | string | number | bigint | boolean | symbol;
/**
* All native types in JavaScript
*/
export type NativeTypes = {
string: string;
number: number;
boolean: boolean;
null: null;
undefined: undefined;
object: object;
function: Function;
symbol: symbol;
bigint: bigint;
};
/**
* Any function
*/
export type AnyFunction = (...args: any[]) => any;
/**
* Any object
*/
export type AnyObject = Record<string | number | symbol, any>;
/**
* A generic Class or constructor
*/
export type AnyConstructor = new (...args: any[]) => any;
/**
* A generic Struct interface
*/
export type AnyStruct = {
[k in string | number | symbol]: Predicate | AnyStruct;
};
/**
* A base Predicate type
*/
export type Predicate = (x: any) => boolean;
/**
* Extract the guarded type from a type guard, defaults to never.
* Override default by passing a second type param.
*/
export type GuardedType<T, Default = unknown> =
// -
T extends (x: any) => x is infer T ? T : Default;
/**
* A Predicate with defined guard
*/
export type GuardedPredicate<T> = (x: any) => x is T;
/**
* Map a type of predicates to the guarded types represented by them
*/
export type PredicatesToGuards<T> = MappedId<
{ [K in keyof T]: GuardedType<T[K], never> }
>;
/**
* Get props that can be assigned U
*/
export type PickPropType<T, U> = {
[K in keyof T]: U extends T[K] ? K : never;
}[keyof T];
/**
* Get props that can be assigned undefined
*/
export type Undefinables<T> = {
[K in PickPropType<T, undefined>]: T[K];
};
/**
* Get props except those that can be assigned U
*/
export type ExcludePropType<T, U> = {
[K in keyof T]: U extends T[K] ? never : K;
}[keyof T];
/**
* Get props that cannot be assigned undefined
*/
export type Defined<T> = {
[K in ExcludePropType<T, undefined>]: T[K];
};
/**
* A trick to unroll complex computed types and reveal the resolved type
*/
export type MappedId<T> = {} & { [P in keyof T]: T[P] };
/**
* Make props that can be undefined optional
*/
export type UndefinedOptional<T> = MappedId<
Defined<T> & Partial<Undefinables<T>>
>;
/**
* Take an object of predicates and return
* an object type containing the guarded types
*/
export type GuardedStruct<Struct> = Struct extends AnyFunction
? GuardedType<Struct, never>
: UndefinedOptional<{ [K in keyof Struct]: GuardedStruct<Struct[K]> }>;
// Undocumented and unexported because doesn't seem useful
// outside of CreateStructGuard
type StructGuardValue<T> = T extends {}
?
| GuardedPredicate<T>
// value can be a predicate guarding a few props of T and props not in T (extends)
| GuardedPredicate<Partial<T> & {}>
| (Partial<CreateStructGuard<T>> & AnyStruct) // value can be an object literal with type guards for a few or all props of T and other predicate props
: GuardedPredicate<T>;
/**
* Take an object of types and return
* an object of corresponding type guards
*/
export type CreateStructGuard<T> =
//
{} & { [K in keyof T]: StructGuardValue<T[K]> };
/**
* Convert a union to an intersection
*/
export type UnionToIntersection<U> =
// -
(U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never;