-
Notifications
You must be signed in to change notification settings - Fork 673
/
Copy pathtypes.ts
306 lines (266 loc) · 9.24 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
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
299
300
301
302
303
304
305
306
import type { MergeParameters } from './versionedTypes'
export type { MergeParameters } from './versionedTypes'
export type { Immutable } from './immutable'
/*
*
* Reselect Data Types
*
*/
/** A standard selector function, which takes three generic type arguments:
* @param State The first value, often a Redux root state object
* @param Result The final result returned by the selector
* @param Params All additional arguments passed into the selector
*/
export type Selector<
// The state can be anything
State = any,
// The result will be inferred
Result = unknown,
// There are either 0 params, or N params
Params extends never | readonly any[] = any[]
// If there are 0 params, type the function as just State in, Result out.
// Otherwise, type it as State + Params in, Result out.
> = [Params] extends [never]
? (state: State) => Result
: (state: State, ...params: Params) => Result
/** Selectors generated by Reselect have several additional fields attached: */
export interface OutputSelectorFields<Combiner extends UnknownFunction, Keys> {
/** The final function passed to `createSelector` */
resultFunc: Combiner
/** The same function, memoized */
memoizedResultFunc: Combiner & Keys
/** Returns the last result calculated by the selector */
lastResult: () => ReturnType<Combiner>
/** An array of the input selectors */
dependencies: SelectorArray
/** Counts the number of times the output has been recalculated */
recomputations: () => number
/** Resets the count of recomputations count to 0 */
resetRecomputations: () => number
}
/** Represents the actual selectors generated by `createSelector`.
* The selector is:
* - "a function that takes this state + params and returns a result"
* - plus the attached additional fields
*/
export type OutputSelector<
S extends SelectorArray,
Result,
Combiner extends UnknownFunction,
Params extends readonly any[] = never, // MergeParameters<S>
Keys = {}
> = Selector<GetStateFromSelectors<S>, Result, Params> &
OutputSelectorFields<Combiner, Keys>
/** A selector that is assumed to have one additional argument, such as
* the props from a React component
*/
export type ParametricSelector<State, Props, Result> = Selector<
State,
Result,
[Props, ...any]
>
/** A generated selector that is assumed to have one additional argument */
export type OutputParametricSelector<
State,
Props,
Result,
Combiner extends UnknownFunction,
Keys = {}
> = ParametricSelector<State, Props, Result> &
OutputSelectorFields<Combiner, Keys>
/** An array of input selectors */
export type SelectorArray = ReadonlyArray<Selector>
/** A standard function returning true if two values are considered equal */
export type EqualityFn = (a: any, b: any) => boolean
/*
*
* Reselect Internal Types
*
*/
/** Extracts an array of all return types from all input selectors */
export type SelectorResultArray<Selectors extends SelectorArray> =
ExtractReturnType<Selectors>
/** Determines the combined single "State" type (first arg) from all input selectors */
export type GetStateFromSelectors<S extends SelectorArray> =
MergeParameters<S>[0]
/** Determines the combined "Params" type (all remaining args) from all input selectors */
export type GetParamsFromSelectors<
S extends SelectorArray,
RemainingItems extends readonly unknown[] = Tail<MergeParameters<S>>
> = RemainingItems
/*
*
* Reselect Internal Utility Types
*
*/
/** Any function with arguments */
export type UnknownFunction = (...args: any[]) => any
/** Extract the return type from all functions as a tuple */
export type ExtractReturnType<T extends readonly UnknownFunction[]> = {
[index in keyof T]: T[index] extends T[number] ? ReturnType<T[index]> : never
}
/** First item in an array */
export type Head<T> = T extends [any, ...any[]] ? T[0] : never
/** All other items in an array */
export type Tail<A> = A extends [any, ...infer Rest] ? Rest : never
/** Last item in an array. Recursion also enables this to work with rest syntax - where the type of rest is extracted */
export type ReverseHead<S extends readonly unknown[][]> = Tail<S> extends [
unknown
]
? S
: Tail<S> extends readonly unknown[][]
? ReverseHead<Tail<S>>
: never
/** All elements in array except last
*
* Recursion makes this work also when rest syntax has been used
* Runs _ReverseTail twice, because first pass turns last element into "never", and second pass removes it.
**/
export type ReverseTail<S> = _ReverseTail<_ReverseTail<S>>
type _ReverseTail<S> = Tail<S> extends [unknown]
? [Head<S>]
: Tail<S> extends unknown[]
? [Head<S>, ..._ReverseTail<Tail<S>>]
: never
/** Extract only numeric keys from an array type */
export type AllArrayKeys<A extends readonly any[]> = A extends any
? {
[K in keyof A]: K
}[number]
: never
export type List<A = any> = ReadonlyArray<A>
export type Has<U, U1> = [U1] extends [U] ? 1 : 0
/*
*
* External/Copied Utility Types
*
*/
/** The infamous "convert a union type to an intersection type" hack
* Source: https://github.com/sindresorhus/type-fest/blob/main/source/union-to-intersection.d.ts
* Reference: https://github.com/microsoft/TypeScript/issues/29594
*/
export type UnionToIntersection<Union> =
// `extends unknown` is always going to be the case and is used to convert the
// `Union` into a [distributive conditional
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
(
Union extends unknown
? // The union type is used as the only argument to a function since the union
// of function arguments is an intersection.
(distributedUnion: Union) => void
: // This won't happen.
never
) extends // Infer the `Intersection` type since TypeScript represents the positional
// arguments of unions of functions as an intersection of the union.
(mergedIntersection: infer Intersection) => void
? Intersection
: never
/**
* Assorted util types for type-level conditional logic
* Source: https://github.com/KiaraGrouwstra/typical
*/
export type Bool = '0' | '1'
export type Obj<T> = { [k: string]: T }
export type And<A extends Bool, B extends Bool> = ({
1: { 1: '1' } & Obj<'0'>
} & Obj<Obj<'0'>>)[A][B]
export type Matches<V, T> = V extends T ? '1' : '0'
export type IsArrayType<T> = Matches<T, any[]>
export type Not<T extends Bool> = { '1': '0'; '0': '1' }[T]
export type InstanceOf<V, T> = And<Matches<V, T>, Not<Matches<T, V>>>
export type IsTuple<T extends { length: number }> = And<
IsArrayType<T>,
InstanceOf<T['length'], number>
>
/**
* Code to convert a union of values into a tuple.
* Source: https://stackoverflow.com/a/55128956/62937
*/
type Push<T extends any[], V> = [...T, V]
type LastOf<T> = UnionToIntersection<
T extends any ? () => T : never
> extends () => infer R
? R
: never
// TS4.1+
export type TuplifyUnion<
T,
L = LastOf<T>,
N = [T] extends [never] ? true : false
> = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>
/**
* Converts "the values of an object" into a tuple, like a type-level `Object.values()`
* Source: https://stackoverflow.com/a/68695508/62937
*/
export type ObjValueTuple<
T,
KS extends any[] = TuplifyUnion<keyof T>,
R extends any[] = []
> = KS extends [infer K, ...infer KT]
? ObjValueTuple<T, KT, [...R, T[K & keyof T]]>
: R
/** Utility type to infer the type of "all params of a function except the first", so we can determine what arguments a memoize function accepts */
export type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
? U
: never
/**
* Expand an item a single level, or recursively.
* Source: https://stackoverflow.com/a/69288824/62937
*/
export type Expand<T> = T extends (...args: infer A) => infer R
? (...args: Expand<A>) => Expand<R>
: T extends infer O
? { [K in keyof O]: O[K] }
: never
export type ExpandRecursively<T> = T extends (...args: infer A) => infer R
? (...args: ExpandRecursively<A>) => ExpandRecursively<R>
: T extends object
? T extends infer O
? { [K in keyof O]: ExpandRecursively<O[K]> }
: never
: T
type Identity<T> = T
/**
* Another form of type value expansion
* Source: https://github.com/microsoft/TypeScript/issues/35247
*/
export type Mapped<T> = Identity<{ [k in keyof T]: T[k] }>
/**
* Fully expand a type, deeply
* Source: https://github.com/millsp/ts-toolbelt (`Any.Compute`)
*/
type ComputeDeep<A, Seen = never> = A extends BuiltIn
? A
: If2<
Has<Seen, A>,
A,
A extends Array<any>
? A extends Array<Record<Key, any>>
? Array<
{
[K in keyof A[number]]: ComputeDeep<A[number][K], A | Seen>
} & unknown
>
: A
: A extends ReadonlyArray<any>
? A extends ReadonlyArray<Record<Key, any>>
? ReadonlyArray<
{
[K in keyof A[number]]: ComputeDeep<A[number][K], A | Seen>
} & unknown
>
: A
: { [K in keyof A]: ComputeDeep<A[K], A | Seen> } & unknown
>
export type If2<B extends Boolean2, Then, Else = never> = B extends 1
? Then
: Else
export type Boolean2 = 0 | 1
export type Key = string | number | symbol
export type BuiltIn =
| Function
| Error
| Date
| { readonly [Symbol.toStringTag]: string }
| RegExp
| Generator