-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
321 lines (272 loc) · 9.37 KB
/
index.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
declare module 'json-immutability-helper' {
const SHARED_UNSET_TOKEN: unique symbol;
type DirectiveFn = <T>(
old: Readonly<T>,
args: ReadonlyArray<unknown>,
context: Readonly<Context>,
) => T | typeof SHARED_UNSET_TOKEN;
type ConditionFn = (
args: ReadonlyArray<unknown>,
context: Readonly<Context>,
) => (actual: unknown) => boolean;
type EqualFn = (x: unknown, y: unknown) => boolean;
type CopyFn = <T>(o: Readonly<T>) => T;
interface Limits {
stringLength: number;
recursionDepth: number;
recursionBreadth: number;
}
type RpnValue = number | string;
type RpnOperator = [number, number, (...args: RpnValue[]) => RpnValue];
type Primitive = undefined | boolean | number | bigint | string | null;
export interface Extension {
commands?: Record<string, DirectiveFn>;
conditions?: Record<string, ConditionFn>;
rpnOperators?: Record<string, RpnOperator>;
rpnConstants?: Record<string, RpnValue>;
limits?: Partial<Limits>;
isEquals?: EqualFn;
copy?: CopyFn;
}
export type Condition<T> =
| (T extends Primitive
? ['=' | '!=' | '~=' | '!~=', ...T[]]
: T extends ReadonlyArray<infer U>
? ArrayCondition<U>
: ObjectCondition<T>)
| (T extends number ? ['>' | '>=' | '<' | '<=', number] : never)
| ['and' | 'or', ...Condition<T>[]]
| ['not', Condition<T>]
| ['exists'];
type ArrayCondition<T> =
| ['length', Condition<number>]
| ['some' | 'every' | 'none', Condition<T>]
| { [index: number]: Condition<T> };
type ObjectCondition<T> = { [K in keyof T]?: Condition<T[K]> };
type Locator<T, R> = [R, Condition<T>] | R;
export type SingleLocator<T> = Locator<T, 'first' | 'last' | number>;
export type MultiLocator<T> = Locator<T, 'first' | 'last' | 'all' | number>;
type If<T, True> = T extends true ? True : never;
export type Spec<T, Unsettable extends boolean = false> =
| (T extends string
? StringSpec
: T extends boolean
? BooleanSpec
: T extends number
? NumberSpec
: T extends ReadonlyArray<infer U>
? ArraySpec<U>
: ObjectSpec<T>)
| ['=', T | If<Unsettable, typeof SHARED_UNSET_TOKEN>]
| ['init', T]
| ['if', Condition<T>, Spec<T, Unsettable>, Spec<T, Unsettable>?]
| ['seq', ...Spec<T, Unsettable>[]]
| If<Unsettable, ['unset']>;
type StringSpec = ['replaceAll', string, string] | ['rpn', ...(number | string)[]];
type BooleanSpec = ['~'];
type NumberSpec = ['+', number] | ['-', number] | ['rpn', ...(number | string)[]];
type ArraySpec<T> =
| ['push', ...T[]]
| ['unshift', ...T[]]
| (T extends Primitive ? ['addUnique', ...T[]] : never)
| ['splice', ...([number, number?] | [number, number, ...T[]])[]]
| ['insert', 'before' | 'after', MultiLocator<T>, ...T[]]
| ['update', MultiLocator<T>, Spec<T, true>, T?]
| ['delete', MultiLocator<T>]
| ['swap', SingleLocator<T>, SingleLocator<T>]
| ['move', MultiLocator<T>, 'before' | 'after', SingleLocator<T>]
| { [index: number]: Spec<T, true> };
type ObjectSpec<T> =
| ['merge', Partial<Readonly<T>>, Readonly<T>?]
| { [K in keyof T]?: Spec<T[K], {} extends Pick<T, K> ? true : false> };
interface Options {
path?: string;
}
type CombineFn = <T, Unsettable extends boolean>(
specs: Spec<T, Unsettable>[],
) => Spec<T, Unsettable>;
function updateFn<T>(
object: T,
spec: Spec<T, false>,
options?: Options & { allowUnset?: false },
): T;
function updateFn<T>(
object: T,
spec: Spec<T, true>,
options?: Options & { allowUnset?: false },
): T | undefined;
function updateFn<T>(
object: T,
spec: Spec<T, true>,
options: Options & { allowUnset: true },
): T | typeof SHARED_UNSET_TOKEN;
type Update = typeof updateFn & {
readonly context: Context;
readonly combine: CombineFn;
readonly UNSET_TOKEN: typeof SHARED_UNSET_TOKEN;
readonly with: (...extensions: Extension[]) => Update;
};
class Context {
public readonly isEquals: EqualFn;
public readonly copy: CopyFn;
public readonly update: Update;
public readonly combine: CombineFn;
public readonly limits: Readonly<Limits>;
public readonly UNSET_TOKEN: typeof SHARED_UNSET_TOKEN;
public with(...extensions: Extension[]): Context;
public makeConditionPredicate<T>(condition: Condition<T>[]): (v: T) => boolean;
public invariant(condition: unknown, message?: string | (() => string)): asserts condition;
}
export const context: Context;
export const update: typeof context.update;
export const combine: typeof context.combine;
export const invariant: typeof context.invariant;
export const UNSET_TOKEN: typeof context.UNSET_TOKEN;
export default context;
}
declare module 'json-immutability-helper/commands/list' {
import type { Extension } from 'json-immutability-helper';
const extension: Readonly<Extension>;
export default extension;
}
declare module 'json-immutability-helper/commands/math' {
import type { Extension } from 'json-immutability-helper';
const extension: Readonly<Extension>;
export default extension;
}
declare module 'json-immutability-helper/commands/string' {
import type { Extension } from 'json-immutability-helper';
const extension: Readonly<Extension>;
export default extension;
}
declare module 'json-immutability-helper/helpers/scoped' {
import type { context, Spec, Condition } from 'json-immutability-helper';
type Context = typeof context;
type SubSpecOptions<T> = { initialisePath?: boolean; initialiseValue?: T };
type ArrayPath<T> = number | Condition<T>;
type UnknownPath = string | ArrayPath<unknown>;
type Reducer<T> = { state: T; dispatch: (s: Spec<T>) => void };
export function makeScopedSpec<T>(path: [], spec: Spec<T>, options?: SubSpecOptions<T>): Spec<T>;
export function getScopedState<T>(c: Context, state: T, path: [], defaultValue?: T): T;
export function makeScopedReducer<T>(
c: Context,
reducer: Reducer<T>,
path: [],
defaultValue?: T,
): Reducer<T>;
export function makeScopedSpec<T>(
path: [ArrayPath<T>],
spec: Spec<T>,
options?: SubSpecOptions<T>,
): Spec<T[]>;
export function getScopedState<T>(
c: Context,
state: T[],
path: [ArrayPath<T>],
defaultValue?: T,
): T;
export function makeScopedReducer<T>(
c: Context,
reducer: Reducer<T[]>,
path: [ArrayPath<T>],
defaultValue?: T,
): Reducer<T>;
export function makeScopedSpec<T, K extends keyof T>(
path: [K],
spec: Spec<T[K]>,
options?: SubSpecOptions<T>,
): Spec<T>;
export function getScopedState<T, K extends keyof T>(
c: Context,
state: T,
path: [K],
defaultValue?: T[K],
): T[K];
export function makeScopedReducer<T, K extends keyof T>(
c: Context,
reducer: Reducer<T>,
path: [K],
defaultValue?: T[K],
): Reducer<T[K]>;
export function makeScopedSpec<T, U>(
path: UnknownPath[],
spec: Spec<U>,
options?: SubSpecOptions<U>,
): Spec<T>;
export function getScopedState<T, U>(
c: Context,
state: T,
path: UnknownPath[],
defaultValue?: U,
): U;
export function makeScopedReducer<T, U>(
c: Context,
reducer: Reducer<T>,
path: UnknownPath[],
defaultValue?: U,
): Reducer<U>;
}
declare module 'json-immutability-helper/helpers/hooks' {
import type { context, Spec, Condition } from 'json-immutability-helper';
type ArrayPath<T> = number | Condition<T>;
type UnknownPath = string | ArrayPath<unknown>;
type Reducer<T, S> = [T, (s: S) => void];
type SpecReducer<T> = { state: T; dispatch: (s: Spec<T>) => void };
type State<T> = [T, (value: T | ((prevState: T) => T)) => void];
type UseState = <T>(initialValue: T | (() => T)) => State<T>;
type UseEvent = <Fn>(fn: Fn) => Fn;
type UseReducer = (<T, S, I>(
reducer: (v: T, s: S) => T,
initializerArg: I,
initializer: (v: I) => T,
) => Reducer<T, S>) &
(<T, S>(
reducer: (v: T, s: S) => T,
initializerArg: T,
initializer?: undefined,
) => Reducer<T, S>);
type UseEffect = (fn: () => void, deps: unknown[]) => void;
type UseRef = <T>(init: T) => { current: T };
type InputHooks = {
useState: UseState;
useReducer?: UseReducer;
} & (
| { useEvent: UseEvent }
| ({ useRef: UseRef } & ({ useLayoutEffect: UseEffect } | { useEffect: UseEffect }))
);
interface ScopedReducerOptions<T> {
initialisePath?: boolean;
initialiseValue?: T;
}
type UseJSONReducer = (<T, I>(initializerArg: I, initializer: (v: I) => T) => SpecReducer<T>) &
(<T>(initializerArg: T, initializer?: undefined) => SpecReducer<T>);
type UseScopedReducer = (<T>(
next: SpecReducer<T>,
path: [],
options?: ScopedReducerOptions<T>,
) => SpecReducer<T>) &
(<T>(
next: SpecReducer<T[]>,
path: [ArrayPath<T>],
options?: ScopedReducerOptions<T>,
) => SpecReducer<T>) &
(<T, K extends keyof T>(
next: SpecReducer<T>,
path: [K],
options?: ScopedReducerOptions<T[K]>,
) => SpecReducer<T[K]>) &
(<T, U>(
next: SpecReducer<T>,
path: UnknownPath[],
options?: ScopedReducerOptions<U>,
) => SpecReducer<U>);
export function makeHooks(
c: typeof context,
hooks: InputHooks,
): {
useEvent: UseEvent;
useJSONReducer: UseJSONReducer;
useWrappedJSONReducer: <T>(next: State<T>) => SpecReducer<T>;
useScopedReducer: UseScopedReducer;
};
}