-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.d.ts
229 lines (185 loc) · 5.73 KB
/
global.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
declare module '*.sass'
declare function isNaN(value: any): boolean
declare function isFinite(value: any): boolean
declare function parseInt(value: any, radix?: string): number
declare function parseFloat(value: any): number
interface URLSearchParams {
append(key: string, value: any): void
}
type Fallish = undefined | null | false | 0 | ''
/**
* Transforms values union into intersaction
* @param U - values union
*/
type UnionToIntersection<U> = (
U extends any
? (k: U) => void
: never
) extends ((k: infer I) => void) ? I : never
/**
* Recursively iterates over given object and makes its properties optional
* @param T - object to iterate over
*/
type DeepPartial<T> = T extends object
? T extends AnyFunc
? T
: { [P in keyof T]?: DeepPartial<T[P]> }
: T
type __ExtractKeysWithOptionalValueObject<O extends Record<string, any>> = keyof ExcludeObjectValueTypes<
{
[K in keyof NarrowObjectToValueTypes<
ExcludeObjectValueTypes<
Required<O>,
undefined
>,
object
>]: undefined extends O[K] ? O[K] : never
},
never
>
/**
* Recursively merges two objects
* @param O1 - object
* @param O2 - object
*/
type DeepMerge<
O1 extends Record<string, any>,
O2 extends Record<string, any>,
BothObjectKeys = keyof NarrowObjectToValueTypes<Required<O1>, object> & keyof NarrowObjectToValueTypes<Required<O2>, object>
> =
{
[K in (
RequiredKeys<NarrowObjectToValueTypes<O1, object>> & RequiredKeys<NarrowObjectToValueTypes<O2, object>>
)]: DeepMerge<O1[K], O2[K]>
} &
{
[K in (
__ExtractKeysWithOptionalValueObject<O1> & __ExtractKeysWithOptionalValueObject<O2>
)]?: DeepMerge<Partial<NonNullable<O1[K]>>, Partial<NonNullable<O2[K]>>>
} &
{
[K in (
RequiredKeys<NarrowObjectToValueTypes<O1, object>> & __ExtractKeysWithOptionalValueObject<O2>
)]: DeepMerge<O1[K], Partial<NonNullable<O2[K]>>>
} &
{
[K in (
__ExtractKeysWithOptionalValueObject<O1> & RequiredKeys<NarrowObjectToValueTypes<O2, object>>
)]: DeepMerge<Partial<NonNullable<O1[K]>>, O2[K]>
} &
{ [K in Exclude<RequiredKeys<O1> & OptionalKeys<O2>, BothObjectKeys>]: Exclude<O1[K] | O2[K], undefined> } &
{ [K in Exclude<RequiredKeys<O2>, BothObjectKeys>]: O2[K] } &
{ [K in Exclude<OptionalKeys<O1> & OptionalKeys<O2>, BothObjectKeys>]?: O1[K] | O2[K] } &
{ [K in keyof Omit<O1, keyof O2>]: O1[K] } &
{ [K in keyof Omit<O2, keyof O1>]: O2[K] }
/**
* Simple object that has string key and optional fields
* @param V - object values. Default: any
*/
type Obj<V = any> = Partial<Record<string, V>>
/** Any function with any parameters and return type */
type AnyFunc = (...args: any[]) => any
/**
* Extracts given object's values
* @param O - object
*/
type Values<O extends Obj> = O[keyof O]
/**
* Makes all the object properties optional with type never
* @param O - object
*/
type Never<O extends Obj> = {
[k in keyof O]?: never
}
/**
* Extracts only object's required properties keys
* @param O - object
*/
type RequiredKeys<O> = { [K in keyof O]-?: object extends Pick<O, K> ? never : K }[keyof O]
/**
* Extracts only object's optional properties keys
* @param O - object
*/
type OptionalKeys<O> = { [K in keyof O]-?: object extends Pick<O, K> ? K : never }[keyof O]
/**
* Exclude null and undefined from a properties of a given object
* @param O - object
*/
type NonNullableProps<O extends Obj> = {
[k in keyof O]: NonNullable<O[k]>
}
/**
* Makes fields to be partial in a given object
* @param O - object
* @param K - object fields
*/
type MakePartialFields<
O extends Obj,
K extends keyof O
> = Omit<O, K> & Partial<Pick<O, K>>
/**
* Makes fields to be required in a given object
* @param O - object
* @param K - object fields
*/
type MakeRequiredFields<
O extends Obj,
K extends keyof O
> = Omit<O, K> & Required<Pick<O, K>>
/**
* Keeps only object properties thats are equal to a given value
* @param O - object
* @param V - value
*/
type NarrowObjectToValueTypes<O extends Obj, V> = {
[K in keyof O as O[K] extends V ? K : never]: V
}
/**
* From a given object excludes object properties thats are equal to a given value
* @param O - object
* @param V - value
*/
type ExcludeObjectValueTypes<O extends Obj, V> = {
[K in keyof O as O[K] extends V ? never : K ]: O[K]
}
/**
* Transforms object into an array of all possible property paths
* @param O - object
*/
type PathsOf<T, R = Required<T>> = Values<{
[P in keyof R]: NonNullable<R[P]> extends Obj
? [P] | [P, ...PathsOf<NonNullable<R[P]>>]
: [P]
}>
/**
* Takes array T and returns same array but without first element
* @param T - array
*/
type Tail<T extends any[]> = ((...t: T) => void) extends ((h: any, ...r: infer R) => void) ? R : never
/**
* Checks whether a _V is null | undefined
* @return {true} if _V ex null | undefined
*/
type IsNullable<_V> = Extract<_V, undefined | null> extends never ? false : true
/**
* Represents all valid css properties
*/
type CSSWithVariables = {
[key: `--${string}`]: string | number
} & React.CSSProperties
/**
* Represents HTML Tag attributes for a given HTMLElement
* @param E - HTMLElement
* @param A - optional React HTML Tag attributes. Default is React.HTMLAttributes<E>
*/
type ReactTagAttributes<
E = HTMLElement,
A = React.HTMLAttributes<E>
> = {
[key: `data-${string}`]: string | boolean | number | undefined
} & A & React.RefAttributes<E>
/**
* Represents react store created with useState() hook
* @param S - store's state
*/
type ReactStore<S> = [ S, React.Dispatch<React.SetStateAction<S>> ]