1 | import type { MergeParameters } from './versionedTypes'
|
2 | export type { MergeParameters } from './versionedTypes'
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export type Selector<
|
16 |
|
17 | State = any,
|
18 |
|
19 | Result = unknown,
|
20 |
|
21 | Params extends never | readonly any[] = any[]
|
22 |
|
23 |
|
24 | > = [Params] extends [never]
|
25 | ? (state: State) => Result
|
26 | : (state: State, ...params: Params) => Result
|
27 |
|
28 |
|
29 | export interface OutputSelectorFields<Combiner extends UnknownFunction, Keys> {
|
30 |
|
31 | resultFunc: Combiner
|
32 |
|
33 | memoizedResultFunc: Combiner & Keys
|
34 |
|
35 | lastResult: () => ReturnType<Combiner>
|
36 |
|
37 | dependencies: SelectorArray
|
38 |
|
39 | recomputations: () => number
|
40 |
|
41 | resetRecomputations: () => number
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export type OutputSelector<
|
50 | S extends SelectorArray,
|
51 | Result,
|
52 | Combiner extends UnknownFunction,
|
53 | Params extends readonly any[] = never,
|
54 | Keys = {}
|
55 | > = Selector<GetStateFromSelectors<S>, Result, Params> &
|
56 | OutputSelectorFields<Combiner, Keys>
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | export type ParametricSelector<State, Props, Result> = Selector<
|
62 | State,
|
63 | Result,
|
64 | [Props, ...any]
|
65 | >
|
66 |
|
67 |
|
68 | export type OutputParametricSelector<
|
69 | State,
|
70 | Props,
|
71 | Result,
|
72 | Combiner extends UnknownFunction,
|
73 | Keys = {}
|
74 | > = ParametricSelector<State, Props, Result> &
|
75 | OutputSelectorFields<Combiner, Keys>
|
76 |
|
77 |
|
78 | export type SelectorArray = ReadonlyArray<Selector>
|
79 |
|
80 |
|
81 | export type EqualityFn = (a: any, b: any) => boolean
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | export type SelectorResultArray<Selectors extends SelectorArray> =
|
91 | ExtractReturnType<Selectors>
|
92 |
|
93 |
|
94 | export type GetStateFromSelectors<S extends SelectorArray> =
|
95 | MergeParameters<S>[0]
|
96 |
|
97 |
|
98 | export type GetParamsFromSelectors<
|
99 | S extends SelectorArray,
|
100 | RemainingItems extends readonly unknown[] = Tail<MergeParameters<S>>
|
101 | > = RemainingItems
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | export type UnknownFunction = (...args: any[]) => any
|
111 |
|
112 |
|
113 | export type ExtractReturnType<T extends readonly UnknownFunction[]> = {
|
114 | [index in keyof T]: T[index] extends T[number] ? ReturnType<T[index]> : never
|
115 | }
|
116 |
|
117 |
|
118 | export type Head<T> = T extends [any, ...any[]] ? T[0] : never
|
119 |
|
120 | export type Tail<A> = A extends [any, ...infer Rest] ? Rest : never
|
121 |
|
122 |
|
123 | export type ReverseHead<S extends readonly unknown[][]> = Tail<S> extends [
|
124 | unknown
|
125 | ]
|
126 | ? S
|
127 | : Tail<S> extends readonly unknown[][]
|
128 | ? ReverseHead<Tail<S>>
|
129 | : never
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | export type ReverseTail<S> = _ReverseTail<_ReverseTail<S>>
|
137 | type _ReverseTail<S> = Tail<S> extends [unknown]
|
138 | ? [Head<S>]
|
139 | : Tail<S> extends unknown[]
|
140 | ? [Head<S>, ..._ReverseTail<Tail<S>>]
|
141 | : never
|
142 |
|
143 |
|
144 | export type AllArrayKeys<A extends readonly any[]> = A extends any
|
145 | ? {
|
146 | [K in keyof A]: K
|
147 | }[number]
|
148 | : never
|
149 |
|
150 | export type List<A = any> = ReadonlyArray<A>
|
151 |
|
152 | export type Has<U, U1> = [U1] extends [U] ? 1 : 0
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | export type UnionToIntersection<Union> =
|
165 |
|
166 |
|
167 |
|
168 | (
|
169 | Union extends unknown
|
170 | ?
|
171 |
|
172 | (distributedUnion: Union) => void
|
173 | :
|
174 | never
|
175 | ) extends
|
176 |
|
177 | (mergedIntersection: infer Intersection) => void
|
178 | ? Intersection
|
179 | : never
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | export type Bool = '0' | '1'
|
186 | export type Obj<T> = { [k: string]: T }
|
187 | export type And<A extends Bool, B extends Bool> = ({
|
188 | 1: { 1: '1' } & Obj<'0'>
|
189 | } & Obj<Obj<'0'>>)[A][B]
|
190 |
|
191 | export type Matches<V, T> = V extends T ? '1' : '0'
|
192 | export type IsArrayType<T> = Matches<T, any[]>
|
193 |
|
194 | export type Not<T extends Bool> = { '1': '0'; '0': '1' }[T]
|
195 | export type InstanceOf<V, T> = And<Matches<V, T>, Not<Matches<T, V>>>
|
196 | export type IsTuple<T extends { length: number }> = And<
|
197 | IsArrayType<T>,
|
198 | InstanceOf<T['length'], number>
|
199 | >
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | type Push<T extends any[], V> = [...T, V]
|
206 |
|
207 | type LastOf<T> = UnionToIntersection<
|
208 | T extends any ? () => T : never
|
209 | > extends () => infer R
|
210 | ? R
|
211 | : never
|
212 |
|
213 |
|
214 | export type TuplifyUnion<
|
215 | T,
|
216 | L = LastOf<T>,
|
217 | N = [T] extends [never] ? true : false
|
218 | > = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | export type ObjValueTuple<
|
225 | T,
|
226 | KS extends any[] = TuplifyUnion<keyof T>,
|
227 | R extends any[] = []
|
228 | > = KS extends [infer K, ...infer KT]
|
229 | ? ObjValueTuple<T, KT, [...R, T[K & keyof T]]>
|
230 | : R
|
231 |
|
232 |
|
233 | export type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
|
234 | ? U
|
235 | : never
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | export type Expand<T> = T extends (...args: infer A) => infer R
|
242 | ? (...args: Expand<A>) => Expand<R>
|
243 | : T extends infer O
|
244 | ? { [K in keyof O]: O[K] }
|
245 | : never
|
246 |
|
247 | export type ExpandRecursively<T> = T extends (...args: infer A) => infer R
|
248 | ? (...args: ExpandRecursively<A>) => ExpandRecursively<R>
|
249 | : T extends object
|
250 | ? T extends infer O
|
251 | ? { [K in keyof O]: ExpandRecursively<O[K]> }
|
252 | : never
|
253 | : T
|
254 |
|
255 | type Identity<T> = T
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | export type Mapped<T> = Identity<{ [k in keyof T]: T[k] }>
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | type ComputeDeep<A, Seen = never> = A extends BuiltIn
|
268 | ? A
|
269 | : If2<
|
270 | Has<Seen, A>,
|
271 | A,
|
272 | A extends Array<any>
|
273 | ? A extends Array<Record<Key, any>>
|
274 | ? Array<
|
275 | {
|
276 | [K in keyof A[number]]: ComputeDeep<A[number][K], A | Seen>
|
277 | } & unknown
|
278 | >
|
279 | : A
|
280 | : A extends ReadonlyArray<any>
|
281 | ? A extends ReadonlyArray<Record<Key, any>>
|
282 | ? ReadonlyArray<
|
283 | {
|
284 | [K in keyof A[number]]: ComputeDeep<A[number][K], A | Seen>
|
285 | } & unknown
|
286 | >
|
287 | : A
|
288 | : { [K in keyof A]: ComputeDeep<A[K], A | Seen> } & unknown
|
289 | >
|
290 |
|
291 | export type If2<B extends Boolean2, Then, Else = never> = B extends 1
|
292 | ? Then
|
293 | : Else
|
294 |
|
295 | export type Boolean2 = 0 | 1
|
296 |
|
297 | export type Key = string | number | symbol
|
298 |
|
299 | export type BuiltIn =
|
300 | | Function
|
301 | | Error
|
302 | | Date
|
303 | | { readonly [Symbol.toStringTag]: string }
|
304 | | RegExp
|
305 | | Generator
|