-
Notifications
You must be signed in to change notification settings - Fork 10
/
type-utils.ts
272 lines (244 loc) · 6.82 KB
/
type-utils.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
import type {Primitive, UnionToIntersection} from 'type-fest'
export type {
Merge,
SetNonNullable,
SetOptional,
SetRequired,
Split,
UnionToIntersection,
ValueOf,
} from 'type-fest'
export type AnyArray<T> = T[] | readonly T[]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyFunction<T = any> = (...args: any[]) => T
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyRecord = Record<string, any>
export type NonEmptyArray<T> = [T, ...T[]]
export type Builtin = Primitive | AnyFunction | Date | Error | RegExp
/**
* Make a runtime mapping type for a literal union
*/
export type EnumOf<E extends string | number | symbol> = {[K in E]: K}
/**
* Same as EnumOf, but values are for display rather than key
*/
export type DisplayOf<E extends string | number | symbol> = {[K in E]: string}
/**
* Create a type that represents either the value or an array of the value type
*/
export type MaybeArray<T> = T | T[]
/**
* Create a type that represents either the value or a promise of the value type
*/
export type MaybePromise<T> = T | Promise<T>
/**
* Like `Partial` but recursive
*/
export declare type PartialDeep<T> = T extends Builtin
? T
: T extends Map<infer K, infer V>
? Map<PartialDeep<K>, PartialDeep<V>>
: T extends ReadonlyMap<infer K, infer V>
? ReadonlyMap<PartialDeep<K>, PartialDeep<V>>
: T extends WeakMap<infer K, infer V>
? WeakMap<PartialDeep<K>, PartialDeep<V>>
: T extends Set<infer U>
? Set<PartialDeep<U>>
: T extends ReadonlySet<infer U>
? ReadonlySet<PartialDeep<U>>
: T extends WeakSet<infer U>
? WeakSet<PartialDeep<U>>
: T extends Array<infer U>
? T extends IsTuple<T>
? {[K in keyof T]?: PartialDeep<T[K]>}
: Array<PartialDeep<U>>
: T extends Promise<infer U>
? Promise<PartialDeep<U>>
: T extends Branded<infer U, infer B>
? Branded<U, B>
: T extends {}
? {[K in keyof T]?: PartialDeep<T[K]>}
: Partial<T>
/**
* Like `PartialDeep` but Array/Map/Set/Promise elements are unchanged
*/
export declare type ObjectPartialDeep<T> = T extends Builtin
? T
: T extends Map<infer K, infer V>
? Map<K, V>
: T extends ReadonlyMap<infer K, infer V>
? ReadonlyMap<K, V>
: T extends WeakMap<infer K, infer V>
? WeakMap<K, V>
: T extends Set<infer U>
? Set<U>
: T extends ReadonlySet<infer U>
? ReadonlySet<U>
: T extends WeakSet<infer U>
? WeakSet<U>
: T extends Array<infer U>
? T extends NonEmptyArray<U>
? T
: U[]
: T extends Promise<infer U>
? Promise<U>
: T extends Branded<infer U, infer B>
? Branded<U, B>
: T extends {}
? {[K in keyof T]?: ObjectPartialDeep<T[K]>}
: T extends unknown
? unknown
: Partial<T>
/**
* Check whether a type is a tuple type
*/
export type IsTuple<T extends {length: number}> = number extends T['length']
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends NonEmptyArray<any>
? T
: never
: T
/**
* Prevent usage of type `T` from being inferred in other generics
* @see https://github.com/Microsoft/TypeScript/issues/14829#issuecomment-504042546
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type NoInfer<T> = [T][T extends any ? 0 : never]
/**
* Mark some properties as required, leaving others optional
*/
export type RequiredOnly<T, K extends keyof T> = Partial<Omit<T, K>> &
Required<Pick<T, K>>
/**
* Make properties non-nullable, leaving others unchanged
*/
export type NonNullableOnly<T, K extends keyof T> = Omit<T, K> & {
[P in K]-?: NonNullable<T[P]>
}
/**
* Distribute the Omit across a union
* @see https://davidgomes.com/pick-omit-over-union-types-in-typescript/
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type DistributiveOmit<T, K extends keyof T> = T extends any
? Omit<T, K>
: never
/**
* Distribute the Pick across a union
* @see https://davidgomes.com/pick-omit-over-union-types-in-typescript/
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type DistributivePick<T, K extends keyof T> = T extends any
? Pick<T, K>
: never
/**
* Remove keys with possibly undefined value from an object
*/
export type PickRequiredKeys<T extends object> = Pick<T, RequiredKeys<T>>
/**
* Extract keys that map to a required value
*/
export type RequiredKeys<T extends object> = Exclude<
{[K in keyof T]: undefined extends T[K] ? never : K}[keyof T],
undefined
>
/**
* Swap keys and values in an object
*/
export type Invert<T extends Record<PropertyKey, PropertyKey>> = {
[V in T[keyof T]]: KeyFromValue<V, T>
}
/**
* Find key in an object corresponding to the given value
*/
export type KeyFromValue<V, T extends Record<PropertyKey, PropertyKey>> = {
[K in keyof T]: V extends T[K] ? K : never
}[keyof T]
/**
* Generates a union of "dotted paths" for the given type.
* Capped at 3 levels of depth by default
*/
export type PathsOf<T, TMaxDepth extends number = 2> = NonNullable<
PathsOfObject<T, T, TMaxDepth>
>
export type PathsOfObject<
T,
// Keep track of self and ancestors to prevent circular reference issues
TSelf = T,
TMaxDepth extends number = 2,
TDepth extends number = 0,
> =
// Array
number extends keyof T
? PathsOfProp<number, NonNullable<T[number]>, TSelf, TMaxDepth, TDepth>
: // Map
string extends keyof T
? PathsOfProp<string, NonNullable<T[string]>, TSelf, TMaxDepth, TDepth>
: // Plain object
{
[P in keyof T]: T[P] extends infer V | null | undefined
? PathsOfProp<P, V, TSelf, TMaxDepth, TDepth>
: never
}[keyof T]
export type PathsOfProp<
P,
V,
TSelf,
TMaxDepth extends number = 2,
TDepth extends number = 0,
> = V extends unknown
? {
0: `${string & P}`
1:
| `${string & P}`
| `${string & P}.${PathsOfObject<
V,
V | TSelf,
TMaxDepth,
Increment<TDepth>
>}`
}[TDepth extends TMaxDepth
? 0
: V extends object
? V extends string
? 0
: TSelf extends V
? V extends TSelf
? 0
: 1
: 1
: 0]
: never
type Increment<T extends number> = T extends keyof IncrementMap
? IncrementMap[T]
: never
interface IncrementMap {
0: 1
1: 2
2: 3
3: 4
4: 5
5: 6
6: 7
7: 8
8: 9
9: 10
}
export type NonDiscriminatedUnion<T> = {
[K in AllUnionKeys<T> & string]: Indexify<T>[K]
}
type AllUnionKeys<T> = keyof UnionToIntersection<{[K in keyof T]: undefined}>
type Indexify<T> = T & Record<string, undefined>
/**
* Inspired by https://github.com/krzkaczor/ts-essentials#exhaustive-switch-cases
*/
export class UnreachableCaseError extends Error {
override name = 'UnreachableCaseError'
constructor(message: never) {
super(message)
Object.setPrototypeOf(this, UnreachableCaseError.prototype)
}
}
export function infer<T>() {
return <U extends T>(input: U) => input
}