UNPKG

3.17 kBPlain TextView Raw
1import type { Middleware } from 'redux'
2
3/**
4 * return True if T is `any`, otherwise return False
5 * taken from https://github.com/joonhocho/tsdef
6 *
7 * @internal
8 */
9export type IsAny<T, True, False = never> =
10 // test if we are going the left AND right path in the condition
11 true | false extends (T extends never ? true : false) ? True : False
12
13/**
14 * return True if T is `unknown`, otherwise return False
15 * taken from https://github.com/joonhocho/tsdef
16 *
17 * @internal
18 */
19export type IsUnknown<T, True, False = never> = unknown extends T
20 ? IsAny<T, False, True>
21 : False
22
23export type FallbackIfUnknown<T, Fallback> = IsUnknown<T, Fallback, T>
24
25/**
26 * @internal
27 */
28export type IfMaybeUndefined<P, True, False> = [undefined] extends [P]
29 ? True
30 : False
31
32/**
33 * @internal
34 */
35export type IfVoid<P, True, False> = [void] extends [P] ? True : False
36
37/**
38 * @internal
39 */
40export type IsEmptyObj<T, True, False = never> = T extends any
41 ? keyof T extends never
42 ? IsUnknown<T, False, IfMaybeUndefined<T, False, IfVoid<T, False, True>>>
43 : False
44 : never
45
46/**
47 * returns True if TS version is above 3.5, False if below.
48 * uses feature detection to detect TS version >= 3.5
49 * * versions below 3.5 will return `{}` for unresolvable interference
50 * * versions above will return `unknown`
51 *
52 * @internal
53 */
54export type AtLeastTS35<True, False> = [True, False][IsUnknown<
55 ReturnType<<T>() => T>,
56 0,
57 1
58>]
59
60/**
61 * @internal
62 */
63export type IsUnknownOrNonInferrable<T, True, False> = AtLeastTS35<
64 IsUnknown<T, True, False>,
65 IsEmptyObj<T, True, IsUnknown<T, True, False>>
66>
67
68/**
69 * Combines all dispatch signatures of all middlewares in the array `M` into
70 * one intersected dispatch signature.
71 */
72export type DispatchForMiddlewares<M> = M extends ReadonlyArray<any>
73 ? UnionToIntersection<
74 M[number] extends infer MiddlewareValues
75 ? MiddlewareValues extends Middleware<infer DispatchExt, any, any>
76 ? DispatchExt extends Function
77 ? IsAny<DispatchExt, never, DispatchExt>
78 : never
79 : never
80 : never
81 >
82 : never
83
84/**
85 * Convert a Union type `(A|B)` to an intersection type `(A&B)`
86 */
87export type UnionToIntersection<U> = (
88 U extends any ? (k: U) => void : never
89) extends (k: infer I) => void
90 ? I
91 : never
92
93/**
94 * Helper type. Passes T out again, but boxes it in a way that it cannot
95 * "widen" the type by accident if it is a generic that should be inferred
96 * from elsewhere.
97 *
98 * @internal
99 */
100export type NoInfer<T> = [T][T extends any ? 0 : never]
101
102export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>
103
104export interface HasMatchFunction<T> {
105 match: (v: any) => v is T
106}
107
108export const hasMatchFunction = <T>(
109 v: Matcher<T>
110): v is HasMatchFunction<T> => {
111 return v && typeof (v as HasMatchFunction<T>).match === 'function'
112}
113
114/** @public */
115export type Matcher<T> = HasMatchFunction<T> | ((v: any) => v is T)
116
117/** @public */
118export type ActionFromMatcher<M extends Matcher<any>> = M extends Matcher<
119 infer T
120>
121 ? T
122 : never
123
\No newline at end of file