-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.d.ts
298 lines (262 loc) · 7.68 KB
/
types.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// deno-lint-ignore-file
type Maybe<T extends any> = T | undefined;
type Obj<T extends string = string> = Record<string, T>;
/**
* Matches any primitive value.
* @see https://mdn.io/Primitive
*/
type Primitive =
| null
| undefined
| string
| number
| boolean
| symbol
| bigint;
/**
* Matches a `class` constructor.
* @see https://mdn.io/Classes.
*/
type Class<T = unknown, Arguments extends any[] = any[]> = new (
...arguments_: Arguments
) => T;
/**
* Matches any [typed array](https://mdn.io/TypedArray).
* @see https://mdn.io/TypedArray
*/
type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;
type Falsey = false | 0 | 0n | "" | null | undefined;
/**
* Promise, or maybe not
*/
type Awaitable<T> = T | PromiseLike<T>;
/**
* Null or whatever
*/
type Nullable<T> = T | null | undefined;
/**
* Array, or not yet
*/
type Arrayable<T> = T | Array<T>;
/**
* Function
*/
type Method<T = void> = import("./src/type.ts").Method<T>;
type Fn<T = void, A = any> = import("./src/type.ts").Fn<T, A>;
/**
* Constructor
*/
type Constructor<T = void, A = void> = new (...args: A[]) => T;
interface Constructable {
new (...args: any[]): any;
}
/**
* Defines an intersection type of all union items.
*
* @param U Union of any types that will be intersected.
* @returns U items intersected
* @see https://stackoverflow.com/a/50375286/9259330
*/
type UnionToIntersection<U> =
(U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I
: never;
/**
* Infers the arguments type of a function
*/
type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;
type MergeInsertions<T> = T extends object
? { [K in keyof T]: MergeInsertions<T[K]> }
: T;
type DeeperMerge<F, S> = MergeInsertions<
{
[K in keyof F | keyof S]: K extends keyof S & keyof F
? DeepMerge<F[K], S[K]>
: K extends keyof S ? S[K]
: K extends keyof F ? F[K]
: never;
}
>;
/**
* Infers the element type of an array
*/
type ElementOf<T> = T extends (infer E)[] ? E : never;
type StringLiteralUnion<T extends U, U = string> = T | (U & {});
type MockMap = Map<
string,
Record<string, string | null | (() => unknown)>
>;
type MutableArray<T extends readonly any[]> = {
-readonly [k in keyof T]: T[k];
};
/**
* How does recursive typing works ?
*
* Deep merging process is handled through `DeepMerge<T, U, Options>` type.
* If both T and U are Records, we recursively merge them,
* else we treat them as primitives.
*
* Merging process is handled through `Merge<T, U>` type, in which
* we remove all maps, sets, arrays and records so we can handle them
* separately depending on merging strategy:
*
* Merge<
* {foo: string},
* {bar: string, baz: Set<unknown>},
* > // "foo" and "bar" will be handled with `MergeRightOmitComplexes`
* // "baz" will be handled with `MergeAll*` type
*
* `MergeRightOmitComplexes<T, U>` will do the above: all T's
* exclusive keys will be kept, though common ones with U will have their
* typing overridden instead:
*
* MergeRightOmitComplexes<
* {foo: string, baz: number},
* {foo: boolean, bar: string}
* > // {baz: number, foo: boolean, bar: string}
* // "baz" was kept from T
* // "foo" was overridden by U's typing
* // "bar" was added from U
*
* For Maps, Arrays, Sets and Records, we use `MergeAll*<T, U>` utility
* types. They will extract relevant data structure from both T and U
* (providing that both have same data data structure, except for typing).
*
* From these, `*ValueType<T>` will extract values (and keys) types to be
* able to create a new data structure with an union typing from both
* data structure of T and U:
*
* MergeAllSets<
* {foo: Set<number>},
* {foo: Set<string>}
* > // `SetValueType` will extract "number" for T
* // `SetValueType` will extract "string" for U
* // `MergeAllSets` will infer type as Set<number|string>
* // Process is similar for Maps, Arrays, and Sets
*
* `DeepMerge<T, U, Options>` is taking a third argument to be handle to
* infer final typing depending on merging strategy:
*
* & (Options extends { sets: "replace" } ? PartialByType<U, Set<unknown>>
* : MergeAllSets<T, U>)
*
* In the above line, if "Options" have its merging strategy for Sets set to
* "replace", instead of performing merging of Sets type, it will take the
* typing from right operand (U) instead, effectively replacing the typing.
*
* An additional note, we use `ExpandRecursively<T>` utility type to expand
* the resulting typing and hide all the typing logic of deep merging so it is
* more user friendly.
*/
/** Force intellisense to expand the typing to hide merging typings */
type ExpandRecursively<T> = T extends Record<PropertyKey, unknown>
? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
: T;
/** Filter of keys matching a given type */
type PartialByType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K];
};
/** Get set values type */
type SetValueType<T> = T extends Set<infer V> ? V : never;
/** Merge all sets types definitions from keys present in both objects */
type MergeAllSets<
T,
U,
X = PartialByType<T, Set<unknown>>,
Y = PartialByType<U, Set<unknown>>,
Z = {
[K in keyof X & keyof Y]: Set<SetValueType<X[K]> | SetValueType<Y[K]>>;
},
> = Z;
/** Get array values type */
type ArrayValueType<T> = T extends Array<infer V> ? V : never;
/** Merge all sets types definitions from keys present in both objects */
type MergeAllArrays<
T,
U,
X = PartialByType<T, Array<unknown>>,
Y = PartialByType<U, Array<unknown>>,
Z = {
[K in keyof X & keyof Y]: Array<
ArrayValueType<X[K]> | ArrayValueType<Y[K]>
>;
},
> = Z;
/** Get map values types */
type MapKeyType<T> = T extends Map<infer K, unknown> ? K : never;
/** Get map values types */
type MapValueType<T> = T extends Map<unknown, infer V> ? V : never;
/** Merge all sets types definitions from keys present in both objects */
type MergeAllMaps<
T,
U,
X = PartialByType<T, Map<unknown, unknown>>,
Y = PartialByType<U, Map<unknown, unknown>>,
Z = {
[K in keyof X & keyof Y]: Map<
MapKeyType<X[K]> | MapKeyType<Y[K]>,
MapValueType<X[K]> | MapValueType<Y[K]>
>;
},
> = Z;
/** Merge all records types definitions from keys present in both objects */
type MergeAllRecords<
T,
U,
Options,
X = PartialByType<T, Record<PropertyKey, unknown>>,
Y = PartialByType<U, Record<PropertyKey, unknown>>,
Z = {
[K in keyof X & keyof Y]: DeepMerge<X[K], Y[K], Options>;
},
> = Z;
/** Exclude map, sets and array from type */
type OmitComplexes<T> = Omit<
T,
keyof PartialByType<
T,
| Map<unknown, unknown>
| Set<unknown>
| Array<unknown>
| Record<PropertyKey, unknown>
>
>;
/** Object with keys in either T or U but not in both */
type ObjectXorKeys<
T,
U,
X = Omit<T, keyof U> & Omit<U, keyof T>,
Y = { [K in keyof X]: X[K] },
> = Y;
/** Merge two objects, with left precedence */
type MergeRightOmitComplexes<
T,
U,
X = ObjectXorKeys<T, U> & OmitComplexes<{ [K in keyof U]: U[K] }>,
> = X;
/** Merge two objects */
type Merge<
T,
U,
Options,
X =
& MergeRightOmitComplexes<T, U>
& MergeAllRecords<T, U, Options>
& (Options extends { sets: "replace" } ? PartialByType<U, Set<unknown>>
: MergeAllSets<T, U>)
& (Options extends { arrays: "replace" } ? PartialByType<U, Array<unknown>>
: MergeAllArrays<T, U>)
& (Options extends { maps: "replace" }
? PartialByType<U, Map<unknown, unknown>>
: MergeAllMaps<T, U>),
> = ExpandRecursively<X>;