@voidhash/mimic 0.0.1-alpha.5 → 0.0.1-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +13 -13
- package/dist/index.cjs +140 -46
- package/dist/index.d.cts +229 -115
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +229 -115
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +140 -46
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/client/ClientDocument.ts +1 -1
- package/src/client/errors.ts +10 -10
- package/src/primitives/Array.ts +45 -21
- package/src/primitives/Boolean.ts +8 -6
- package/src/primitives/Either.ts +13 -11
- package/src/primitives/Literal.ts +7 -5
- package/src/primitives/Number.ts +13 -11
- package/src/primitives/String.ts +14 -12
- package/src/primitives/Struct.ts +66 -17
- package/src/primitives/Tree.ts +103 -32
- package/src/primitives/TreeNode.ts +49 -27
- package/src/primitives/Union.ts +44 -17
- package/src/primitives/shared.ts +106 -5
- package/src/server/errors.ts +6 -6
- package/tests/primitives/Struct.test.ts +250 -0
- package/tests/primitives/Tree.test.ts +122 -0
package/dist/index.d.mts
CHANGED
|
@@ -141,11 +141,18 @@ type TransformResult = {
|
|
|
141
141
|
/**
|
|
142
142
|
* Base interface that all primitives must implement.
|
|
143
143
|
* Provides type inference helpers and internal operations.
|
|
144
|
+
*
|
|
145
|
+
* @typeParam TState - The state type this primitive holds
|
|
146
|
+
* @typeParam TProxy - The proxy type for interacting with this primitive
|
|
147
|
+
* @typeParam TDefined - Whether the value is guaranteed to be defined (via required() or default())
|
|
148
|
+
* @typeParam THasDefault - Whether this primitive has a default value
|
|
144
149
|
*/
|
|
145
|
-
interface Primitive<TState, TProxy> {
|
|
150
|
+
interface Primitive<TState, TProxy, TDefined extends boolean = false, THasDefault extends boolean = false> {
|
|
146
151
|
readonly _tag: string;
|
|
147
152
|
readonly _State: TState;
|
|
148
153
|
readonly _Proxy: TProxy;
|
|
154
|
+
readonly _TDefined: TDefined;
|
|
155
|
+
readonly _THasDefault: THasDefault;
|
|
149
156
|
readonly _internal: PrimitiveInternal<TState, TProxy>;
|
|
150
157
|
}
|
|
151
158
|
/**
|
|
@@ -171,15 +178,15 @@ interface PrimitiveInternal<TState, TProxy> {
|
|
|
171
178
|
/**
|
|
172
179
|
* Any primitive type - used for generic constraints.
|
|
173
180
|
*/
|
|
174
|
-
type AnyPrimitive = Primitive<any, any>;
|
|
181
|
+
type AnyPrimitive = Primitive<any, any, any, any>;
|
|
175
182
|
/**
|
|
176
183
|
* Infer the state type from a primitive.
|
|
177
184
|
*/
|
|
178
|
-
type InferState<T> = T extends Primitive<infer S, any> ? S : never;
|
|
185
|
+
type InferState<T> = T extends Primitive<infer S, any, any, any> ? S : never;
|
|
179
186
|
/**
|
|
180
187
|
* Infer the proxy type from a primitive.
|
|
181
188
|
*/
|
|
182
|
-
type InferProxy<T> = T extends Primitive<any, infer P> ? P : never;
|
|
189
|
+
type InferProxy<T> = T extends Primitive<any, infer P, any, any> ? P : never;
|
|
183
190
|
/**
|
|
184
191
|
* Helper type to conditionally add undefined based on TDefined.
|
|
185
192
|
* When TDefined is true, the value is guaranteed to be defined (via required() or default()).
|
|
@@ -190,9 +197,36 @@ type MaybeUndefined<T, TDefined extends boolean> = TDefined extends true ? T : T
|
|
|
190
197
|
* Infer the snapshot type from a primitive.
|
|
191
198
|
* The snapshot is a readonly, type-safe structure suitable for rendering.
|
|
192
199
|
*/
|
|
193
|
-
type InferSnapshot<T> = T extends Primitive<any, infer P> ? P extends {
|
|
200
|
+
type InferSnapshot<T> = T extends Primitive<any, infer P, any, any> ? P extends {
|
|
194
201
|
toSnapshot(): infer S;
|
|
195
202
|
} ? S : never : never;
|
|
203
|
+
/**
|
|
204
|
+
* Extract THasDefault from a primitive.
|
|
205
|
+
*/
|
|
206
|
+
type HasDefault<T> = T extends Primitive<any, any, any, infer H> ? H : false;
|
|
207
|
+
/**
|
|
208
|
+
* Extract TDefined from a primitive.
|
|
209
|
+
*/
|
|
210
|
+
type IsDefined<T> = T extends Primitive<any, any, infer D, any> ? D : false;
|
|
211
|
+
/**
|
|
212
|
+
* Determines if a field is required for set() operations.
|
|
213
|
+
* A field is required if: TDefined is true AND THasDefault is false
|
|
214
|
+
*/
|
|
215
|
+
type IsRequiredForSet<T> = T extends Primitive<any, any, true, false> ? true : false;
|
|
216
|
+
/**
|
|
217
|
+
* Extract keys of fields that are required for set() (required without default).
|
|
218
|
+
*/
|
|
219
|
+
type RequiredSetKeys<TFields$1 extends Record<string, AnyPrimitive>> = { [K in keyof TFields$1]: IsRequiredForSet<TFields$1[K]> extends true ? K : never }[keyof TFields$1];
|
|
220
|
+
/**
|
|
221
|
+
* Extract keys of fields that are optional for set() (has default OR not required).
|
|
222
|
+
*/
|
|
223
|
+
type OptionalSetKeys<TFields$1 extends Record<string, AnyPrimitive>> = { [K in keyof TFields$1]: IsRequiredForSet<TFields$1[K]> extends true ? never : K }[keyof TFields$1];
|
|
224
|
+
/**
|
|
225
|
+
* Compute the input type for set() operations on a struct.
|
|
226
|
+
* Required fields (required without default) must be provided.
|
|
227
|
+
* Optional fields (has default or not required) can be omitted.
|
|
228
|
+
*/
|
|
229
|
+
type StructSetInput<TFields$1 extends Record<string, AnyPrimitive>> = { readonly [K in RequiredSetKeys<TFields$1>]: InferState<TFields$1[K]> } & { readonly [K in OptionalSetKeys<TFields$1>]?: InferState<TFields$1[K]> };
|
|
196
230
|
declare class ValidationError extends Error {
|
|
197
231
|
readonly _tag = "ValidationError";
|
|
198
232
|
constructor(message: string);
|
|
@@ -218,6 +252,19 @@ declare function runValidators<T>(value: T, validators: readonly {
|
|
|
218
252
|
* @returns True if the operation is compatible, false otherwise.
|
|
219
253
|
*/
|
|
220
254
|
declare function isCompatibleOperation(operation: Operation<any, any, any>, operationDefinitions: Record<string, OperationDefinition<any, any, any>>): boolean;
|
|
255
|
+
/**
|
|
256
|
+
* Applies default values to a partial input, recursively handling nested structs.
|
|
257
|
+
*
|
|
258
|
+
* Uses a two-layer approach:
|
|
259
|
+
* 1. First, get the struct's initial state (which includes struct-level defaults)
|
|
260
|
+
* 2. Then, layer the provided values on top
|
|
261
|
+
* 3. Finally, ensure nested structs are recursively processed
|
|
262
|
+
*
|
|
263
|
+
* @param primitive - The primitive definition containing field information
|
|
264
|
+
* @param value - The partial value provided by the user
|
|
265
|
+
* @returns The value with defaults applied for missing fields
|
|
266
|
+
*/
|
|
267
|
+
declare function applyDefaults<T extends AnyPrimitive>(primitive: T, value: Partial<InferState<T>>): InferState<T>;
|
|
221
268
|
//#endregion
|
|
222
269
|
//#region src/primitives/String.d.ts
|
|
223
270
|
interface StringProxy<TDefined extends boolean = false> {
|
|
@@ -233,84 +280,95 @@ interface StringPrimitiveSchema {
|
|
|
233
280
|
readonly defaultValue: string | undefined;
|
|
234
281
|
readonly validators: readonly Validator<string>[];
|
|
235
282
|
}
|
|
236
|
-
declare class StringPrimitive<TDefined extends boolean = false> implements Primitive<string, StringProxy<TDefined
|
|
283
|
+
declare class StringPrimitive<TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<string, StringProxy<TDefined>, TDefined, THasDefault> {
|
|
237
284
|
readonly _tag: "StringPrimitive";
|
|
238
285
|
readonly _State: string;
|
|
239
286
|
readonly _Proxy: StringProxy<TDefined>;
|
|
287
|
+
readonly _TDefined: TDefined;
|
|
288
|
+
readonly _THasDefault: THasDefault;
|
|
240
289
|
private readonly _schema;
|
|
241
290
|
private readonly _opDefinitions;
|
|
242
291
|
constructor(schema: StringPrimitiveSchema);
|
|
243
292
|
/** Mark this string as required */
|
|
244
|
-
required(): StringPrimitive<true>;
|
|
293
|
+
required(): StringPrimitive<true, THasDefault>;
|
|
245
294
|
/** Set a default value for this string */
|
|
246
|
-
default(defaultValue: string): StringPrimitive<true>;
|
|
295
|
+
default(defaultValue: string): StringPrimitive<true, true>;
|
|
247
296
|
/** Add a custom validation rule */
|
|
248
|
-
refine(fn: (value: string) => boolean, message: string): StringPrimitive<TDefined>;
|
|
297
|
+
refine(fn: (value: string) => boolean, message: string): StringPrimitive<TDefined, THasDefault>;
|
|
249
298
|
/** Minimum string length */
|
|
250
|
-
min(length: number): StringPrimitive<TDefined>;
|
|
299
|
+
min(length: number): StringPrimitive<TDefined, THasDefault>;
|
|
251
300
|
/** Maximum string length */
|
|
252
|
-
max(length: number): StringPrimitive<TDefined>;
|
|
301
|
+
max(length: number): StringPrimitive<TDefined, THasDefault>;
|
|
253
302
|
/** Exact string length */
|
|
254
|
-
length(exact: number): StringPrimitive<TDefined>;
|
|
303
|
+
length(exact: number): StringPrimitive<TDefined, THasDefault>;
|
|
255
304
|
/** Match a regex pattern */
|
|
256
|
-
regex(pattern: RegExp, message?: string): StringPrimitive<TDefined>;
|
|
305
|
+
regex(pattern: RegExp, message?: string): StringPrimitive<TDefined, THasDefault>;
|
|
257
306
|
/** Validate as email format */
|
|
258
|
-
email(): StringPrimitive<TDefined>;
|
|
307
|
+
email(): StringPrimitive<TDefined, THasDefault>;
|
|
259
308
|
/** Validate as URL format */
|
|
260
|
-
url(): StringPrimitive<TDefined>;
|
|
309
|
+
url(): StringPrimitive<TDefined, THasDefault>;
|
|
261
310
|
readonly _internal: PrimitiveInternal<string, StringProxy<TDefined>>;
|
|
262
311
|
}
|
|
263
312
|
/** Creates a new StringPrimitive */
|
|
264
|
-
declare const String: () => StringPrimitive<false>;
|
|
313
|
+
declare const String: () => StringPrimitive<false, false>;
|
|
265
314
|
//#endregion
|
|
266
315
|
//#region src/primitives/Struct.d.ts
|
|
267
316
|
/**
|
|
268
317
|
* Maps a schema definition to its state type.
|
|
269
318
|
* { name: StringPrimitive, age: NumberPrimitive } -> { name: string, age: number }
|
|
270
319
|
*/
|
|
271
|
-
type InferStructState<TFields extends Record<string, AnyPrimitive>> = { readonly [K in keyof TFields]: InferState<TFields[K]> };
|
|
320
|
+
type InferStructState<TFields$1 extends Record<string, AnyPrimitive>> = { readonly [K in keyof TFields$1]: InferState<TFields$1[K]> };
|
|
272
321
|
/**
|
|
273
322
|
* Maps a schema definition to its snapshot type.
|
|
274
323
|
* Each field's snapshot type is inferred from the field primitive.
|
|
275
324
|
*/
|
|
276
|
-
type InferStructSnapshot<TFields extends Record<string, AnyPrimitive>> = { readonly [K in keyof TFields]: InferSnapshot<TFields[K]> };
|
|
325
|
+
type InferStructSnapshot<TFields$1 extends Record<string, AnyPrimitive>> = { readonly [K in keyof TFields$1]: InferSnapshot<TFields$1[K]> };
|
|
326
|
+
/**
|
|
327
|
+
* Maps a schema definition to a partial update type.
|
|
328
|
+
* For nested structs, allows recursive partial updates.
|
|
329
|
+
*/
|
|
330
|
+
type StructUpdateValue<TFields$1 extends Record<string, AnyPrimitive>> = { readonly [K in keyof TFields$1]?: TFields$1[K] extends StructPrimitive<infer F, any> ? StructUpdateValue<F> | InferState<TFields$1[K]> : InferState<TFields$1[K]> };
|
|
277
331
|
/**
|
|
278
332
|
* Maps a schema definition to its proxy type.
|
|
279
333
|
* Provides nested field access + get()/set()/toSnapshot() methods for the whole struct.
|
|
280
334
|
*/
|
|
281
|
-
type StructProxy<TFields extends Record<string, AnyPrimitive>, TDefined extends boolean = false> = { readonly [K in keyof TFields]: InferProxy<TFields[K]> } & {
|
|
335
|
+
type StructProxy<TFields$1 extends Record<string, AnyPrimitive>, TDefined extends boolean = false> = { readonly [K in keyof TFields$1]: InferProxy<TFields$1[K]> } & {
|
|
282
336
|
/** Gets the entire struct value */
|
|
283
|
-
get(): MaybeUndefined<InferStructState<TFields>, TDefined>;
|
|
284
|
-
/** Sets the entire struct value */
|
|
285
|
-
set(value:
|
|
337
|
+
get(): MaybeUndefined<InferStructState<TFields$1>, TDefined>;
|
|
338
|
+
/** Sets the entire struct value (only fields that are required without defaults must be provided) */
|
|
339
|
+
set(value: StructSetInput<TFields$1>): void;
|
|
340
|
+
/** Updates only the specified fields (partial update, handles nested structs recursively) */
|
|
341
|
+
update(value: StructUpdateValue<TFields$1>): void;
|
|
286
342
|
/** Returns a readonly snapshot of the struct for rendering */
|
|
287
|
-
toSnapshot(): MaybeUndefined<InferStructSnapshot<TFields>, TDefined>;
|
|
343
|
+
toSnapshot(): MaybeUndefined<InferStructSnapshot<TFields$1>, TDefined>;
|
|
288
344
|
};
|
|
289
|
-
interface StructPrimitiveSchema<TFields extends Record<string, AnyPrimitive>> {
|
|
345
|
+
interface StructPrimitiveSchema<TFields$1 extends Record<string, AnyPrimitive>> {
|
|
290
346
|
readonly required: boolean;
|
|
291
|
-
readonly defaultValue: InferStructState<TFields> | undefined;
|
|
292
|
-
readonly fields: TFields;
|
|
293
|
-
readonly validators: readonly Validator<InferStructState<TFields>>[];
|
|
347
|
+
readonly defaultValue: InferStructState<TFields$1> | undefined;
|
|
348
|
+
readonly fields: TFields$1;
|
|
349
|
+
readonly validators: readonly Validator<InferStructState<TFields$1>>[];
|
|
294
350
|
}
|
|
295
|
-
declare class StructPrimitive<TFields extends Record<string, AnyPrimitive>, TDefined extends boolean = false> implements Primitive<InferStructState<TFields>, StructProxy<TFields, TDefined
|
|
351
|
+
declare class StructPrimitive<TFields$1 extends Record<string, AnyPrimitive>, TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<InferStructState<TFields$1>, StructProxy<TFields$1, TDefined>, TDefined, THasDefault> {
|
|
296
352
|
readonly _tag: "StructPrimitive";
|
|
297
|
-
readonly _State: InferStructState<TFields>;
|
|
298
|
-
readonly _Proxy: StructProxy<TFields, TDefined>;
|
|
353
|
+
readonly _State: InferStructState<TFields$1>;
|
|
354
|
+
readonly _Proxy: StructProxy<TFields$1, TDefined>;
|
|
355
|
+
readonly _TDefined: TDefined;
|
|
356
|
+
readonly _THasDefault: THasDefault;
|
|
299
357
|
private readonly _schema;
|
|
300
358
|
private readonly _opDefinitions;
|
|
301
|
-
constructor(schema: StructPrimitiveSchema<TFields>);
|
|
359
|
+
constructor(schema: StructPrimitiveSchema<TFields$1>);
|
|
302
360
|
/** Mark this struct as required */
|
|
303
|
-
required(): StructPrimitive<TFields, true>;
|
|
361
|
+
required(): StructPrimitive<TFields$1, true, THasDefault>;
|
|
304
362
|
/** Set a default value for this struct */
|
|
305
|
-
default(defaultValue:
|
|
363
|
+
default(defaultValue: StructSetInput<TFields$1>): StructPrimitive<TFields$1, true, true>;
|
|
306
364
|
/** Get the fields schema */
|
|
307
|
-
get fields(): TFields;
|
|
365
|
+
get fields(): TFields$1;
|
|
308
366
|
/** Add a custom validation rule (useful for cross-field validation) */
|
|
309
|
-
refine(fn: (value: InferStructState<TFields>) => boolean, message: string): StructPrimitive<TFields, TDefined>;
|
|
310
|
-
readonly _internal: PrimitiveInternal<InferStructState<TFields>, StructProxy<TFields, TDefined>>;
|
|
367
|
+
refine(fn: (value: InferStructState<TFields$1>) => boolean, message: string): StructPrimitive<TFields$1, TDefined, THasDefault>;
|
|
368
|
+
readonly _internal: PrimitiveInternal<InferStructState<TFields$1>, StructProxy<TFields$1, TDefined>>;
|
|
311
369
|
}
|
|
312
370
|
/** Creates a new StructPrimitive with the given fields */
|
|
313
|
-
declare const Struct: <TFields extends Record<string, AnyPrimitive>>(fields: TFields) => StructPrimitive<TFields, false>;
|
|
371
|
+
declare const Struct: <TFields$1 extends Record<string, AnyPrimitive>>(fields: TFields$1) => StructPrimitive<TFields$1, false, false>;
|
|
314
372
|
//#endregion
|
|
315
373
|
//#region src/primitives/Boolean.d.ts
|
|
316
374
|
interface BooleanProxy<TDefined extends boolean = false> {
|
|
@@ -326,23 +384,25 @@ interface BooleanPrimitiveSchema {
|
|
|
326
384
|
readonly defaultValue: boolean | undefined;
|
|
327
385
|
readonly validators: readonly Validator<boolean>[];
|
|
328
386
|
}
|
|
329
|
-
declare class BooleanPrimitive<TDefined extends boolean = false> implements Primitive<boolean, BooleanProxy<TDefined
|
|
387
|
+
declare class BooleanPrimitive<TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<boolean, BooleanProxy<TDefined>, TDefined, THasDefault> {
|
|
330
388
|
readonly _tag: "BooleanPrimitive";
|
|
331
389
|
readonly _State: boolean;
|
|
332
390
|
readonly _Proxy: BooleanProxy<TDefined>;
|
|
391
|
+
readonly _TDefined: TDefined;
|
|
392
|
+
readonly _THasDefault: THasDefault;
|
|
333
393
|
private readonly _schema;
|
|
334
394
|
private readonly _opDefinitions;
|
|
335
395
|
constructor(schema: BooleanPrimitiveSchema);
|
|
336
396
|
/** Mark this boolean as required */
|
|
337
|
-
required(): BooleanPrimitive<true>;
|
|
397
|
+
required(): BooleanPrimitive<true, THasDefault>;
|
|
338
398
|
/** Set a default value for this boolean */
|
|
339
|
-
default(defaultValue: boolean): BooleanPrimitive<true>;
|
|
399
|
+
default(defaultValue: boolean): BooleanPrimitive<true, true>;
|
|
340
400
|
/** Add a custom validation rule */
|
|
341
|
-
refine(fn: (value: boolean) => boolean, message: string): BooleanPrimitive<TDefined>;
|
|
401
|
+
refine(fn: (value: boolean) => boolean, message: string): BooleanPrimitive<TDefined, THasDefault>;
|
|
342
402
|
readonly _internal: PrimitiveInternal<boolean, BooleanProxy<TDefined>>;
|
|
343
403
|
}
|
|
344
404
|
/** Creates a new BooleanPrimitive */
|
|
345
|
-
declare const Boolean: () => BooleanPrimitive<false>;
|
|
405
|
+
declare const Boolean: () => BooleanPrimitive<false, false>;
|
|
346
406
|
//#endregion
|
|
347
407
|
//#region src/primitives/Number.d.ts
|
|
348
408
|
interface NumberProxy<TDefined extends boolean = false> {
|
|
@@ -358,33 +418,35 @@ interface NumberPrimitiveSchema {
|
|
|
358
418
|
readonly defaultValue: number | undefined;
|
|
359
419
|
readonly validators: readonly Validator<number>[];
|
|
360
420
|
}
|
|
361
|
-
declare class NumberPrimitive<TDefined extends boolean = false> implements Primitive<number, NumberProxy<TDefined
|
|
421
|
+
declare class NumberPrimitive<TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<number, NumberProxy<TDefined>, TDefined, THasDefault> {
|
|
362
422
|
readonly _tag: "NumberPrimitive";
|
|
363
423
|
readonly _State: number;
|
|
364
424
|
readonly _Proxy: NumberProxy<TDefined>;
|
|
425
|
+
readonly _TDefined: TDefined;
|
|
426
|
+
readonly _THasDefault: THasDefault;
|
|
365
427
|
private readonly _schema;
|
|
366
428
|
private readonly _opDefinitions;
|
|
367
429
|
constructor(schema: NumberPrimitiveSchema);
|
|
368
430
|
/** Mark this number as required */
|
|
369
|
-
required(): NumberPrimitive<true>;
|
|
431
|
+
required(): NumberPrimitive<true, THasDefault>;
|
|
370
432
|
/** Set a default value for this number */
|
|
371
|
-
default(defaultValue: number): NumberPrimitive<true>;
|
|
433
|
+
default(defaultValue: number): NumberPrimitive<true, true>;
|
|
372
434
|
/** Add a custom validation rule */
|
|
373
|
-
refine(fn: (value: number) => boolean, message: string): NumberPrimitive<TDefined>;
|
|
435
|
+
refine(fn: (value: number) => boolean, message: string): NumberPrimitive<TDefined, THasDefault>;
|
|
374
436
|
/** Minimum value (inclusive) */
|
|
375
|
-
min(value: number): NumberPrimitive<TDefined>;
|
|
437
|
+
min(value: number): NumberPrimitive<TDefined, THasDefault>;
|
|
376
438
|
/** Maximum value (inclusive) */
|
|
377
|
-
max(value: number): NumberPrimitive<TDefined>;
|
|
439
|
+
max(value: number): NumberPrimitive<TDefined, THasDefault>;
|
|
378
440
|
/** Must be positive (> 0) */
|
|
379
|
-
positive(): NumberPrimitive<TDefined>;
|
|
441
|
+
positive(): NumberPrimitive<TDefined, THasDefault>;
|
|
380
442
|
/** Must be negative (< 0) */
|
|
381
|
-
negative(): NumberPrimitive<TDefined>;
|
|
443
|
+
negative(): NumberPrimitive<TDefined, THasDefault>;
|
|
382
444
|
/** Must be an integer */
|
|
383
|
-
int(): NumberPrimitive<TDefined>;
|
|
445
|
+
int(): NumberPrimitive<TDefined, THasDefault>;
|
|
384
446
|
readonly _internal: PrimitiveInternal<number, NumberProxy<TDefined>>;
|
|
385
447
|
}
|
|
386
448
|
/** Creates a new NumberPrimitive */
|
|
387
|
-
declare const Number: () => NumberPrimitive<false>;
|
|
449
|
+
declare const Number: () => NumberPrimitive<false, false>;
|
|
388
450
|
//#endregion
|
|
389
451
|
//#region src/primitives/Literal.d.ts
|
|
390
452
|
/** Valid literal types */
|
|
@@ -402,23 +464,25 @@ interface LiteralPrimitiveSchema<T extends LiteralValue> {
|
|
|
402
464
|
readonly defaultValue: T | undefined;
|
|
403
465
|
readonly literal: T;
|
|
404
466
|
}
|
|
405
|
-
declare class LiteralPrimitive<T extends LiteralValue, TDefined extends boolean = false> implements Primitive<T, LiteralProxy<T, TDefined
|
|
467
|
+
declare class LiteralPrimitive<T extends LiteralValue, TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<T, LiteralProxy<T, TDefined>, TDefined, THasDefault> {
|
|
406
468
|
readonly _tag: "LiteralPrimitive";
|
|
407
469
|
readonly _State: T;
|
|
408
470
|
readonly _Proxy: LiteralProxy<T, TDefined>;
|
|
471
|
+
readonly _TDefined: TDefined;
|
|
472
|
+
readonly _THasDefault: THasDefault;
|
|
409
473
|
private readonly _schema;
|
|
410
474
|
private readonly _opDefinitions;
|
|
411
475
|
constructor(schema: LiteralPrimitiveSchema<T>);
|
|
412
476
|
/** Mark this literal as required */
|
|
413
|
-
required(): LiteralPrimitive<T, true>;
|
|
477
|
+
required(): LiteralPrimitive<T, true, THasDefault>;
|
|
414
478
|
/** Set a default value for this literal */
|
|
415
|
-
default(defaultValue: T): LiteralPrimitive<T, true>;
|
|
479
|
+
default(defaultValue: T): LiteralPrimitive<T, true, true>;
|
|
416
480
|
/** Get the literal value this primitive represents */
|
|
417
481
|
get literal(): T;
|
|
418
482
|
readonly _internal: PrimitiveInternal<T, LiteralProxy<T, TDefined>>;
|
|
419
483
|
}
|
|
420
484
|
/** Creates a new LiteralPrimitive with the given literal value */
|
|
421
|
-
declare const Literal: <T extends LiteralValue>(literal: T) => LiteralPrimitive<T, false>;
|
|
485
|
+
declare const Literal: <T extends LiteralValue>(literal: T) => LiteralPrimitive<T, false, false>;
|
|
422
486
|
//#endregion
|
|
423
487
|
//#region src/primitives/Array.d.ts
|
|
424
488
|
/**
|
|
@@ -440,15 +504,21 @@ interface ArrayEntrySnapshot<TElement extends AnyPrimitive> {
|
|
|
440
504
|
* Snapshot type for arrays - always an array (never undefined)
|
|
441
505
|
*/
|
|
442
506
|
type ArraySnapshot<TElement extends AnyPrimitive> = readonly ArrayEntrySnapshot<TElement>[];
|
|
507
|
+
/**
|
|
508
|
+
* Compute the input type for array element values.
|
|
509
|
+
* If the element is a struct, uses StructSetInput (partial with defaults).
|
|
510
|
+
* Otherwise uses the full state type.
|
|
511
|
+
*/
|
|
512
|
+
type ArrayElementSetInput<TElement extends AnyPrimitive> = TElement extends StructPrimitive<infer TFields, any, any> ? StructSetInput<TFields> : InferState<TElement>;
|
|
443
513
|
interface ArrayProxy<TElement extends AnyPrimitive> {
|
|
444
514
|
/** Gets the current array entries (sorted by position) */
|
|
445
515
|
get(): ArrayState<TElement>;
|
|
446
|
-
/** Replaces the entire array with new values (generates new IDs and positions) */
|
|
447
|
-
set(values: readonly
|
|
448
|
-
/** Appends a value to the end of the array */
|
|
449
|
-
push(value:
|
|
450
|
-
/** Inserts a value at the specified visual index */
|
|
451
|
-
insertAt(index: number, value:
|
|
516
|
+
/** Replaces the entire array with new values (generates new IDs and positions, applies defaults) */
|
|
517
|
+
set(values: readonly ArrayElementSetInput<TElement>[]): void;
|
|
518
|
+
/** Appends a value to the end of the array (applies defaults for struct elements) */
|
|
519
|
+
push(value: ArrayElementSetInput<TElement>): void;
|
|
520
|
+
/** Inserts a value at the specified visual index (applies defaults for struct elements) */
|
|
521
|
+
insertAt(index: number, value: ArrayElementSetInput<TElement>): void;
|
|
452
522
|
/** Removes the element with the specified ID */
|
|
453
523
|
remove(id: string): void;
|
|
454
524
|
/** Moves an element to a new visual index */
|
|
@@ -468,29 +538,31 @@ interface ArrayPrimitiveSchema<TElement extends AnyPrimitive> {
|
|
|
468
538
|
readonly element: TElement;
|
|
469
539
|
readonly validators: readonly Validator<ArrayState<TElement>>[];
|
|
470
540
|
}
|
|
471
|
-
declare class ArrayPrimitive<TElement extends AnyPrimitive> implements Primitive<ArrayState<TElement>, ArrayProxy<TElement
|
|
541
|
+
declare class ArrayPrimitive<TElement extends AnyPrimitive, TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<ArrayState<TElement>, ArrayProxy<TElement>, TDefined, THasDefault> {
|
|
472
542
|
readonly _tag: "ArrayPrimitive";
|
|
473
543
|
readonly _State: ArrayState<TElement>;
|
|
474
544
|
readonly _Proxy: ArrayProxy<TElement>;
|
|
545
|
+
readonly _TDefined: TDefined;
|
|
546
|
+
readonly _THasDefault: THasDefault;
|
|
475
547
|
private readonly _schema;
|
|
476
548
|
private readonly _opDefinitions;
|
|
477
549
|
constructor(schema: ArrayPrimitiveSchema<TElement>);
|
|
478
550
|
/** Mark this array as required */
|
|
479
|
-
required(): ArrayPrimitive<TElement>;
|
|
551
|
+
required(): ArrayPrimitive<TElement, true, THasDefault>;
|
|
480
552
|
/** Set a default value for this array */
|
|
481
|
-
default(defaultValue: ArrayState<TElement>): ArrayPrimitive<TElement>;
|
|
553
|
+
default(defaultValue: ArrayState<TElement>): ArrayPrimitive<TElement, true, true>;
|
|
482
554
|
/** Get the element primitive */
|
|
483
555
|
get element(): TElement;
|
|
484
556
|
/** Add a custom validation rule */
|
|
485
|
-
refine(fn: (value: ArrayState<TElement>) => boolean, message: string): ArrayPrimitive<TElement>;
|
|
557
|
+
refine(fn: (value: ArrayState<TElement>) => boolean, message: string): ArrayPrimitive<TElement, TDefined, THasDefault>;
|
|
486
558
|
/** Minimum array length */
|
|
487
|
-
minLength(length: number): ArrayPrimitive<TElement>;
|
|
559
|
+
minLength(length: number): ArrayPrimitive<TElement, TDefined, THasDefault>;
|
|
488
560
|
/** Maximum array length */
|
|
489
|
-
maxLength(length: number): ArrayPrimitive<TElement>;
|
|
561
|
+
maxLength(length: number): ArrayPrimitive<TElement, TDefined, THasDefault>;
|
|
490
562
|
readonly _internal: PrimitiveInternal<ArrayState<TElement>, ArrayProxy<TElement>>;
|
|
491
563
|
}
|
|
492
564
|
/** Creates a new ArrayPrimitive with the given element type */
|
|
493
|
-
declare const Array$1: <TElement extends AnyPrimitive>(element: TElement) => ArrayPrimitive<TElement>;
|
|
565
|
+
declare const Array$1: <TElement extends AnyPrimitive>(element: TElement) => ArrayPrimitive<TElement, false, false>;
|
|
494
566
|
//#endregion
|
|
495
567
|
//#region src/primitives/Lazy.d.ts
|
|
496
568
|
/**
|
|
@@ -525,7 +597,7 @@ declare const Lazy: <TThunk extends () => AnyPrimitive>(thunk: TThunk) => LazyPr
|
|
|
525
597
|
/**
|
|
526
598
|
* Type constraint for union variants - must be struct primitives
|
|
527
599
|
*/
|
|
528
|
-
type UnionVariants = Record<string, StructPrimitive<any, any>>;
|
|
600
|
+
type UnionVariants = Record<string, StructPrimitive<any, any, any>>;
|
|
529
601
|
/**
|
|
530
602
|
* Infer the union state type from variants
|
|
531
603
|
*/
|
|
@@ -534,14 +606,19 @@ type InferUnionState<TVariants extends UnionVariants> = { [K in keyof TVariants]
|
|
|
534
606
|
* Infer the union snapshot type from variants
|
|
535
607
|
*/
|
|
536
608
|
type InferUnionSnapshot<TVariants extends UnionVariants> = { [K in keyof TVariants]: InferSnapshot<TVariants[K]> }[keyof TVariants];
|
|
609
|
+
/**
|
|
610
|
+
* Compute the input type for union.set() operations.
|
|
611
|
+
* For each variant, uses StructSetInput to make fields with defaults optional.
|
|
612
|
+
*/
|
|
613
|
+
type UnionSetInput<TVariants extends UnionVariants> = { [K in keyof TVariants]: TVariants[K] extends StructPrimitive<infer TFields, any, any> ? StructSetInput<TFields> : InferState<TVariants[K]> }[keyof TVariants];
|
|
537
614
|
/**
|
|
538
615
|
* Proxy for accessing union variants
|
|
539
616
|
*/
|
|
540
|
-
interface UnionProxy<TVariants extends UnionVariants,
|
|
617
|
+
interface UnionProxy<TVariants extends UnionVariants, _TDiscriminator extends string, TDefined extends boolean = false> {
|
|
541
618
|
/** Gets the current union value */
|
|
542
619
|
get(): MaybeUndefined<InferUnionState<TVariants>, TDefined>;
|
|
543
|
-
/** Sets the entire union value */
|
|
544
|
-
set(value:
|
|
620
|
+
/** Sets the entire union value (applies defaults for variant fields) */
|
|
621
|
+
set(value: UnionSetInput<TVariants>): void;
|
|
545
622
|
/** Access a specific variant's proxy (assumes the variant is active) */
|
|
546
623
|
as<K$1 extends keyof TVariants>(variant: K$1): InferProxy<TVariants[K$1]>;
|
|
547
624
|
/** Pattern match on the variant type */
|
|
@@ -555,23 +632,27 @@ interface UnionPrimitiveSchema<TVariants extends UnionVariants, TDiscriminator e
|
|
|
555
632
|
readonly discriminator: TDiscriminator;
|
|
556
633
|
readonly variants: TVariants;
|
|
557
634
|
}
|
|
558
|
-
declare class UnionPrimitive<TVariants extends UnionVariants, TDiscriminator extends string = "type", TDefined extends boolean = false> implements Primitive<InferUnionState<TVariants>, UnionProxy<TVariants, TDiscriminator, TDefined
|
|
635
|
+
declare class UnionPrimitive<TVariants extends UnionVariants, TDiscriminator extends string = "type", TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<InferUnionState<TVariants>, UnionProxy<TVariants, TDiscriminator, TDefined>, TDefined, THasDefault> {
|
|
559
636
|
readonly _tag: "UnionPrimitive";
|
|
560
637
|
readonly _State: InferUnionState<TVariants>;
|
|
561
638
|
readonly _Proxy: UnionProxy<TVariants, TDiscriminator, TDefined>;
|
|
639
|
+
readonly _TDefined: TDefined;
|
|
640
|
+
readonly _THasDefault: THasDefault;
|
|
562
641
|
private readonly _schema;
|
|
563
642
|
private readonly _opDefinitions;
|
|
564
643
|
constructor(schema: UnionPrimitiveSchema<TVariants, TDiscriminator>);
|
|
565
644
|
/** Mark this union as required */
|
|
566
|
-
required(): UnionPrimitive<TVariants, TDiscriminator, true>;
|
|
645
|
+
required(): UnionPrimitive<TVariants, TDiscriminator, true, THasDefault>;
|
|
567
646
|
/** Set a default value for this union */
|
|
568
|
-
default(defaultValue:
|
|
647
|
+
default(defaultValue: UnionSetInput<TVariants>): UnionPrimitive<TVariants, TDiscriminator, true, true>;
|
|
569
648
|
/** Get the discriminator field name */
|
|
570
649
|
get discriminator(): TDiscriminator;
|
|
571
650
|
/** Get the variants */
|
|
572
651
|
get variants(): TVariants;
|
|
573
652
|
/** Find the variant key from a state value */
|
|
574
653
|
private _findVariantKey;
|
|
654
|
+
/** Apply defaults to a variant value based on the discriminator */
|
|
655
|
+
private _applyVariantDefaults;
|
|
575
656
|
readonly _internal: PrimitiveInternal<InferUnionState<TVariants>, UnionProxy<TVariants, TDiscriminator, TDefined>>;
|
|
576
657
|
}
|
|
577
658
|
/** Options for creating a Union primitive */
|
|
@@ -582,14 +663,14 @@ interface UnionOptions<TVariants extends UnionVariants, TDiscriminator extends s
|
|
|
582
663
|
readonly variants: TVariants;
|
|
583
664
|
}
|
|
584
665
|
/** Creates a new UnionPrimitive with the given variants */
|
|
585
|
-
declare function Union<TVariants extends UnionVariants>(options: UnionOptions<TVariants, "type">): UnionPrimitive<TVariants, "type", false>;
|
|
586
|
-
declare function Union<TVariants extends UnionVariants, TDiscriminator extends string>(options: UnionOptions<TVariants, TDiscriminator>): UnionPrimitive<TVariants, TDiscriminator, false>;
|
|
666
|
+
declare function Union<TVariants extends UnionVariants>(options: UnionOptions<TVariants, "type">): UnionPrimitive<TVariants, "type", false, false>;
|
|
667
|
+
declare function Union<TVariants extends UnionVariants, TDiscriminator extends string>(options: UnionOptions<TVariants, TDiscriminator>): UnionPrimitive<TVariants, TDiscriminator, false, false>;
|
|
587
668
|
//#endregion
|
|
588
669
|
//#region src/primitives/Either.d.ts
|
|
589
670
|
/**
|
|
590
671
|
* Scalar primitives that can be used as variants in Either
|
|
591
672
|
*/
|
|
592
|
-
type ScalarPrimitive = StringPrimitive<any> | NumberPrimitive<any> | BooleanPrimitive<any> | LiteralPrimitive<any, any>;
|
|
673
|
+
type ScalarPrimitive = StringPrimitive<any, any> | NumberPrimitive<any, any> | BooleanPrimitive<any, any> | LiteralPrimitive<any, any, any>;
|
|
593
674
|
/**
|
|
594
675
|
* Infer the union state type from a tuple of scalar primitives
|
|
595
676
|
*/
|
|
@@ -625,17 +706,19 @@ interface EitherPrimitiveSchema<TVariants extends readonly ScalarPrimitive[]> {
|
|
|
625
706
|
readonly defaultValue: InferEitherState<TVariants> | undefined;
|
|
626
707
|
readonly variants: TVariants;
|
|
627
708
|
}
|
|
628
|
-
declare class EitherPrimitive<TVariants extends readonly ScalarPrimitive[], TDefined extends boolean = false> implements Primitive<InferEitherState<TVariants>, EitherProxy<TVariants, TDefined
|
|
709
|
+
declare class EitherPrimitive<TVariants extends readonly ScalarPrimitive[], TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<InferEitherState<TVariants>, EitherProxy<TVariants, TDefined>, TDefined, THasDefault> {
|
|
629
710
|
readonly _tag: "EitherPrimitive";
|
|
630
711
|
readonly _State: InferEitherState<TVariants>;
|
|
631
712
|
readonly _Proxy: EitherProxy<TVariants, TDefined>;
|
|
713
|
+
readonly _TDefined: TDefined;
|
|
714
|
+
readonly _THasDefault: THasDefault;
|
|
632
715
|
private readonly _schema;
|
|
633
716
|
private readonly _opDefinitions;
|
|
634
717
|
constructor(schema: EitherPrimitiveSchema<TVariants>);
|
|
635
718
|
/** Mark this either as required */
|
|
636
|
-
required(): EitherPrimitive<TVariants, true>;
|
|
719
|
+
required(): EitherPrimitive<TVariants, true, THasDefault>;
|
|
637
720
|
/** Set a default value for this either */
|
|
638
|
-
default(defaultValue: InferEitherState<TVariants>): EitherPrimitive<TVariants, true>;
|
|
721
|
+
default(defaultValue: InferEitherState<TVariants>): EitherPrimitive<TVariants, true, true>;
|
|
639
722
|
/** Get the variants */
|
|
640
723
|
get variants(): TVariants;
|
|
641
724
|
/**
|
|
@@ -680,9 +763,17 @@ declare class EitherPrimitive<TVariants extends readonly ScalarPrimitive[], TDef
|
|
|
680
763
|
* );
|
|
681
764
|
* ```
|
|
682
765
|
*/
|
|
683
|
-
declare function Either<TVariants extends readonly ScalarPrimitive[]>(...variants: TVariants): EitherPrimitive<TVariants, false>;
|
|
766
|
+
declare function Either<TVariants extends readonly ScalarPrimitive[]>(...variants: TVariants): EitherPrimitive<TVariants, false, false>;
|
|
684
767
|
//#endregion
|
|
685
768
|
//#region src/primitives/TreeNode.d.ts
|
|
769
|
+
/**
|
|
770
|
+
* Branded type for TreeNodeSelf - distinguishable at compile time
|
|
771
|
+
*/
|
|
772
|
+
declare const SelfBrand: unique symbol;
|
|
773
|
+
interface TreeNodeSelfType {
|
|
774
|
+
readonly _tag: "TreeNodeSelf";
|
|
775
|
+
readonly _brand: typeof SelfBrand;
|
|
776
|
+
}
|
|
686
777
|
/**
|
|
687
778
|
* Special placeholder for self-referential tree nodes.
|
|
688
779
|
* Use this in the children array when a node type can contain itself.
|
|
@@ -691,51 +782,59 @@ declare function Either<TVariants extends readonly ScalarPrimitive[]>(...variant
|
|
|
691
782
|
* ```typescript
|
|
692
783
|
* const FolderNode = TreeNode("folder", {
|
|
693
784
|
* data: Struct({ name: String() }),
|
|
694
|
-
* children: [
|
|
785
|
+
* children: [TreeNodeSelf], // Folder can contain other folders
|
|
695
786
|
* });
|
|
696
787
|
* ```
|
|
697
788
|
*/
|
|
698
|
-
declare const TreeNodeSelf:
|
|
789
|
+
declare const TreeNodeSelf: TreeNodeSelfType;
|
|
790
|
+
/**
|
|
791
|
+
* Type utility to resolve Self placeholders to the actual node type
|
|
792
|
+
*/
|
|
793
|
+
type ResolveSelf<T, TSelf extends AnyTreeNodePrimitive> = T extends TreeNodeSelfType ? TSelf : T;
|
|
794
|
+
/**
|
|
795
|
+
* Type utility to resolve all children in a tuple, replacing Self with the node type
|
|
796
|
+
*/
|
|
797
|
+
type ResolveChildrenUnion<TChildren, TSelf extends AnyTreeNodePrimitive> = TChildren extends readonly (infer U)[] ? ResolveSelf<U, TSelf> : never;
|
|
699
798
|
/**
|
|
700
799
|
* The type for children - either a direct array or a lazy function (for self-referential nodes).
|
|
701
|
-
* Using `Function` type allows self-references without explicit type annotations.
|
|
702
800
|
*/
|
|
703
|
-
type
|
|
801
|
+
type TreeNodeChildrenInput = readonly (AnyTreeNodePrimitive | TreeNodeSelfType)[] | (() => readonly (AnyTreeNodePrimitive | TreeNodeSelfType)[]);
|
|
704
802
|
/**
|
|
705
803
|
* Any TreeNodePrimitive type - used for generic constraints.
|
|
706
804
|
*/
|
|
707
|
-
type AnyTreeNodePrimitive = TreeNodePrimitive<string, StructPrimitive<any
|
|
805
|
+
type AnyTreeNodePrimitive = TreeNodePrimitive<string, StructPrimitive<any>, any>;
|
|
708
806
|
/**
|
|
709
807
|
* Infer the data state type from a TreeNodePrimitive
|
|
710
808
|
*/
|
|
711
|
-
type InferTreeNodeDataState<T extends AnyTreeNodePrimitive> = T extends TreeNodePrimitive<any, infer TData> ? InferState<TData> : never;
|
|
809
|
+
type InferTreeNodeDataState<T extends AnyTreeNodePrimitive> = T extends TreeNodePrimitive<any, infer TData, any> ? InferState<TData> : never;
|
|
712
810
|
/**
|
|
713
811
|
* Infer the type literal from a TreeNodePrimitive
|
|
714
812
|
*/
|
|
715
|
-
type InferTreeNodeType<T extends AnyTreeNodePrimitive> = T extends TreeNodePrimitive<infer TType, any> ? TType : never;
|
|
813
|
+
type InferTreeNodeType<T extends AnyTreeNodePrimitive> = T extends TreeNodePrimitive<infer TType, any, any> ? TType : never;
|
|
716
814
|
/**
|
|
717
|
-
* Infer the allowed children from a TreeNodePrimitive
|
|
815
|
+
* Infer the allowed children from a TreeNodePrimitive
|
|
718
816
|
*/
|
|
719
|
-
type InferTreeNodeChildren<
|
|
817
|
+
type InferTreeNodeChildren<T> = T extends TreeNodePrimitive<any, any, infer TChildren> ? TChildren : never;
|
|
720
818
|
/**
|
|
721
819
|
* Configuration for a TreeNode primitive
|
|
722
820
|
*/
|
|
723
|
-
interface TreeNodeConfig<TData$1 extends StructPrimitive<any
|
|
821
|
+
interface TreeNodeConfig<TData$1 extends StructPrimitive<any>, TChildren extends readonly (AnyTreeNodePrimitive | TreeNodeSelfType)[]> {
|
|
724
822
|
readonly data: TData$1;
|
|
725
|
-
readonly children:
|
|
823
|
+
readonly children: TChildren | (() => TChildren);
|
|
726
824
|
}
|
|
727
825
|
/**
|
|
728
826
|
* TreeNodePrimitive - defines a node type with its data schema and allowed children
|
|
729
827
|
*/
|
|
730
|
-
declare class TreeNodePrimitive<TType extends string, TData$1 extends StructPrimitive<any
|
|
828
|
+
declare class TreeNodePrimitive<TType extends string, TData$1 extends StructPrimitive<any>, TChildren extends AnyTreeNodePrimitive = AnyTreeNodePrimitive> {
|
|
731
829
|
readonly _tag: "TreeNodePrimitive";
|
|
732
830
|
readonly _Type: TType;
|
|
733
831
|
readonly _Data: TData$1;
|
|
832
|
+
readonly _Children: TChildren;
|
|
734
833
|
private readonly _type;
|
|
735
834
|
private readonly _data;
|
|
736
835
|
private readonly _children;
|
|
737
836
|
private _resolvedChildren;
|
|
738
|
-
constructor(type: TType, config: TreeNodeConfig<TData$1>);
|
|
837
|
+
constructor(type: TType, config: TreeNodeConfig<TData$1, readonly (AnyTreeNodePrimitive | TreeNodeSelfType)[]>);
|
|
739
838
|
/** Get the node type identifier */
|
|
740
839
|
get type(): TType;
|
|
741
840
|
/** Get the data primitive */
|
|
@@ -746,7 +845,7 @@ declare class TreeNodePrimitive<TType extends string, TData$1 extends StructPrim
|
|
|
746
845
|
isChildAllowed(childType: string): boolean;
|
|
747
846
|
}
|
|
748
847
|
/** Creates a new TreeNodePrimitive with the given type and config */
|
|
749
|
-
declare const TreeNode: <TType extends string, TData$1 extends StructPrimitive<any
|
|
848
|
+
declare const TreeNode: <TType extends string, TData$1 extends StructPrimitive<any>, const TChildren extends readonly (AnyTreeNodePrimitive | TreeNodeSelfType)[]>(type: TType, config: TreeNodeConfig<TData$1, TChildren>) => TreeNodePrimitive<TType, TData$1, ResolveChildrenUnion<TChildren, TreeNodePrimitive<TType, TData$1, any>>>;
|
|
750
849
|
//#endregion
|
|
751
850
|
//#region src/primitives/Tree.d.ts
|
|
752
851
|
/**
|
|
@@ -772,7 +871,7 @@ interface TypedTreeNodeState<TNode extends AnyTreeNodePrimitive> {
|
|
|
772
871
|
/**
|
|
773
872
|
* The state type for trees - a flat array of nodes
|
|
774
873
|
*/
|
|
775
|
-
type TreeState<
|
|
874
|
+
type TreeState<_TRoot extends AnyTreeNodePrimitive> = readonly TreeNodeState[];
|
|
776
875
|
/**
|
|
777
876
|
* Snapshot of a single node for UI rendering (data properties spread at node level)
|
|
778
877
|
*/
|
|
@@ -785,6 +884,15 @@ type TreeNodeSnapshot<TNode extends AnyTreeNodePrimitive> = {
|
|
|
785
884
|
* Infer the snapshot type for a tree (recursive tree structure for UI)
|
|
786
885
|
*/
|
|
787
886
|
type InferTreeSnapshot<T extends TreePrimitive<any>> = T extends TreePrimitive<infer TRoot> ? TreeNodeSnapshot<TRoot> : never;
|
|
887
|
+
/**
|
|
888
|
+
* Helper type to infer the update value type from a TreeNode's data
|
|
889
|
+
*/
|
|
890
|
+
type TreeNodeUpdateValue<TNode extends AnyTreeNodePrimitive> = TNode["data"] extends StructPrimitive<infer TFields, any, any> ? StructUpdateValue<TFields> : never;
|
|
891
|
+
/**
|
|
892
|
+
* Helper type to infer the input type for node data (with defaults applied).
|
|
893
|
+
* Uses StructSetInput for struct data, making fields with defaults optional.
|
|
894
|
+
*/
|
|
895
|
+
type TreeNodeDataSetInput<TNode extends AnyTreeNodePrimitive> = TNode["data"] extends StructPrimitive<infer TFields, any, any> ? StructSetInput<TFields> : InferTreeNodeDataState<TNode>;
|
|
788
896
|
/**
|
|
789
897
|
* Typed proxy for a specific node type - provides type-safe data access
|
|
790
898
|
*/
|
|
@@ -797,11 +905,13 @@ interface TypedNodeProxy<TNode extends AnyTreeNodePrimitive> {
|
|
|
797
905
|
readonly data: InferProxy<TNode["data"]>;
|
|
798
906
|
/** Get the raw node state */
|
|
799
907
|
get(): TypedTreeNodeState<TNode>;
|
|
908
|
+
/** Updates only the specified data fields (partial update, handles nested structs recursively) */
|
|
909
|
+
update(value: TreeNodeUpdateValue<TNode>): void;
|
|
800
910
|
}
|
|
801
911
|
/**
|
|
802
912
|
* Node proxy with type narrowing capabilities
|
|
803
913
|
*/
|
|
804
|
-
interface TreeNodeProxyBase<
|
|
914
|
+
interface TreeNodeProxyBase<_TRoot extends AnyTreeNodePrimitive> {
|
|
805
915
|
/** The node ID */
|
|
806
916
|
readonly id: string;
|
|
807
917
|
/** The node type (string) */
|
|
@@ -827,16 +937,16 @@ interface TreeProxy<TRoot$1 extends AnyTreeNodePrimitive> {
|
|
|
827
937
|
children(parentId: string | null): TreeNodeState[];
|
|
828
938
|
/** Gets a node proxy by ID with type narrowing capabilities */
|
|
829
939
|
node(id: string): TreeNodeProxyBase<TRoot$1> | undefined;
|
|
830
|
-
/** Insert a new node as the first child */
|
|
831
|
-
insertFirst<TNode extends AnyTreeNodePrimitive>(parentId: string | null, nodeType: TNode, data:
|
|
832
|
-
/** Insert a new node as the last child */
|
|
833
|
-
insertLast<TNode extends AnyTreeNodePrimitive>(parentId: string | null, nodeType: TNode, data:
|
|
834
|
-
/** Insert a new node at a specific index among siblings */
|
|
835
|
-
insertAt<TNode extends AnyTreeNodePrimitive>(parentId: string | null, index: number, nodeType: TNode, data:
|
|
836
|
-
/** Insert a new node after a sibling */
|
|
837
|
-
insertAfter<TNode extends AnyTreeNodePrimitive>(siblingId: string, nodeType: TNode, data:
|
|
838
|
-
/** Insert a new node before a sibling */
|
|
839
|
-
insertBefore<TNode extends AnyTreeNodePrimitive>(siblingId: string, nodeType: TNode, data:
|
|
940
|
+
/** Insert a new node as the first child (applies defaults for node data) */
|
|
941
|
+
insertFirst<TNode extends AnyTreeNodePrimitive>(parentId: string | null, nodeType: TNode, data: TreeNodeDataSetInput<TNode>): string;
|
|
942
|
+
/** Insert a new node as the last child (applies defaults for node data) */
|
|
943
|
+
insertLast<TNode extends AnyTreeNodePrimitive>(parentId: string | null, nodeType: TNode, data: TreeNodeDataSetInput<TNode>): string;
|
|
944
|
+
/** Insert a new node at a specific index among siblings (applies defaults for node data) */
|
|
945
|
+
insertAt<TNode extends AnyTreeNodePrimitive>(parentId: string | null, index: number, nodeType: TNode, data: TreeNodeDataSetInput<TNode>): string;
|
|
946
|
+
/** Insert a new node after a sibling (applies defaults for node data) */
|
|
947
|
+
insertAfter<TNode extends AnyTreeNodePrimitive>(siblingId: string, nodeType: TNode, data: TreeNodeDataSetInput<TNode>): string;
|
|
948
|
+
/** Insert a new node before a sibling (applies defaults for node data) */
|
|
949
|
+
insertBefore<TNode extends AnyTreeNodePrimitive>(siblingId: string, nodeType: TNode, data: TreeNodeDataSetInput<TNode>): string;
|
|
840
950
|
/** Remove a node and all its descendants */
|
|
841
951
|
remove(id: string): void;
|
|
842
952
|
/** Move a node to a new parent at a specific index */
|
|
@@ -851,6 +961,8 @@ interface TreeProxy<TRoot$1 extends AnyTreeNodePrimitive> {
|
|
|
851
961
|
moveToLast(nodeId: string, newParentId: string | null): void;
|
|
852
962
|
/** Returns a typed proxy for a specific node's data */
|
|
853
963
|
at<TNode extends AnyTreeNodePrimitive>(id: string, nodeType: TNode): InferProxy<TNode["data"]>;
|
|
964
|
+
/** Updates only the specified data fields of a node (partial update) */
|
|
965
|
+
updateAt<TNode extends AnyTreeNodePrimitive>(id: string, nodeType: TNode, value: TreeNodeUpdateValue<TNode>): void;
|
|
854
966
|
/** Convert tree to a nested snapshot for UI rendering */
|
|
855
967
|
toSnapshot(): TreeNodeSnapshot<TRoot$1> | undefined;
|
|
856
968
|
}
|
|
@@ -860,22 +972,24 @@ interface TreePrimitiveSchema<TRoot$1 extends AnyTreeNodePrimitive> {
|
|
|
860
972
|
readonly root: TRoot$1;
|
|
861
973
|
readonly validators: readonly Validator<TreeState<TRoot$1>>[];
|
|
862
974
|
}
|
|
863
|
-
declare class TreePrimitive<TRoot$1 extends AnyTreeNodePrimitive> implements Primitive<TreeState<TRoot$1>, TreeProxy<TRoot$1
|
|
975
|
+
declare class TreePrimitive<TRoot$1 extends AnyTreeNodePrimitive, TDefined extends boolean = false, THasDefault extends boolean = false> implements Primitive<TreeState<TRoot$1>, TreeProxy<TRoot$1>, TDefined, THasDefault> {
|
|
864
976
|
readonly _tag: "TreePrimitive";
|
|
865
977
|
readonly _State: TreeState<TRoot$1>;
|
|
866
978
|
readonly _Proxy: TreeProxy<TRoot$1>;
|
|
979
|
+
readonly _TDefined: TDefined;
|
|
980
|
+
readonly _THasDefault: THasDefault;
|
|
867
981
|
private readonly _schema;
|
|
868
982
|
private _nodeTypeRegistry;
|
|
869
983
|
private readonly _opDefinitions;
|
|
870
984
|
constructor(schema: TreePrimitiveSchema<TRoot$1>);
|
|
871
985
|
/** Mark this tree as required */
|
|
872
|
-
required(): TreePrimitive<TRoot$1>;
|
|
986
|
+
required(): TreePrimitive<TRoot$1, true, THasDefault>;
|
|
873
987
|
/** Set a default value for this tree */
|
|
874
|
-
default(defaultValue: TreeState<TRoot$1>): TreePrimitive<TRoot$1>;
|
|
988
|
+
default(defaultValue: TreeState<TRoot$1>): TreePrimitive<TRoot$1, true, true>;
|
|
875
989
|
/** Get the root node type */
|
|
876
990
|
get root(): TRoot$1;
|
|
877
991
|
/** Add a custom validation rule */
|
|
878
|
-
refine(fn: (value: TreeState<TRoot$1>) => boolean, message: string): TreePrimitive<TRoot$1>;
|
|
992
|
+
refine(fn: (value: TreeState<TRoot$1>) => boolean, message: string): TreePrimitive<TRoot$1, TDefined, THasDefault>;
|
|
879
993
|
/**
|
|
880
994
|
* Build a registry of all node types reachable from root
|
|
881
995
|
*/
|
|
@@ -896,9 +1010,9 @@ interface TreeOptions<TRoot$1 extends AnyTreeNodePrimitive> {
|
|
|
896
1010
|
readonly root: TRoot$1;
|
|
897
1011
|
}
|
|
898
1012
|
/** Creates a new TreePrimitive with the given root node type */
|
|
899
|
-
declare const Tree: <TRoot$1 extends AnyTreeNodePrimitive>(options: TreeOptions<TRoot$1>) => TreePrimitive<TRoot$1>;
|
|
1013
|
+
declare const Tree: <TRoot$1 extends AnyTreeNodePrimitive>(options: TreeOptions<TRoot$1>) => TreePrimitive<TRoot$1, false, false>;
|
|
900
1014
|
declare namespace Primitive_d_exports {
|
|
901
|
-
export { AnyPrimitive, AnyTreeNodePrimitive, Array$1 as Array, ArrayEntry, ArrayEntrySnapshot, ArrayPrimitive, ArrayProxy, ArraySnapshot, ArrayState, Boolean, BooleanPrimitive, BooleanProxy, Either, EitherMatchHandlers, EitherPrimitive, EitherProxy, InferEitherSnapshot, InferEitherState, InferLazyProxy, InferLazySnapshot, InferLazyState, InferProxy, InferSnapshot, InferState, InferStructSnapshot, InferStructState, InferTreeNodeChildren, InferTreeNodeDataState, InferTreeNodeType, InferTreeSnapshot, InferUnionSnapshot, InferUnionState, Lazy, LazyPrimitive, Literal, LiteralPrimitive, LiteralProxy, LiteralValue, MaybeUndefined, Number, NumberPrimitive, NumberProxy, Primitive, PrimitiveInternal, ScalarPrimitive, String, StringPrimitive, StringProxy, Struct, StructPrimitive, StructProxy, Tree, TreeNode,
|
|
1015
|
+
export { AnyPrimitive, AnyTreeNodePrimitive, Array$1 as Array, ArrayElementSetInput, ArrayEntry, ArrayEntrySnapshot, ArrayPrimitive, ArrayProxy, ArraySnapshot, ArrayState, Boolean, BooleanPrimitive, BooleanProxy, Either, EitherMatchHandlers, EitherPrimitive, EitherProxy, HasDefault, InferEitherSnapshot, InferEitherState, InferLazyProxy, InferLazySnapshot, InferLazyState, InferProxy, InferSnapshot, InferState, InferStructSnapshot, InferStructState, InferTreeNodeChildren, InferTreeNodeDataState, InferTreeNodeType, InferTreeSnapshot, InferUnionSnapshot, InferUnionState, IsDefined, IsRequiredForSet, Lazy, LazyPrimitive, Literal, LiteralPrimitive, LiteralProxy, LiteralValue, MaybeUndefined, Number, NumberPrimitive, NumberProxy, OptionalSetKeys, Primitive, PrimitiveInternal, RequiredSetKeys, ScalarPrimitive, String, StringPrimitive, StringProxy, Struct, StructPrimitive, StructProxy, StructSetInput, StructUpdateValue, Tree, TreeNode, TreeNodeChildrenInput, TreeNodeConfig, TreeNodeDataSetInput, TreeNodePrimitive, TreeNodeProxyBase, TreeNodeSelf, TreeNodeSelfType, TreeNodeSnapshot, TreeNodeState, TreeNodeUpdateValue, TreeOptions, TreePrimitive, TreeProxy, TreeState, TypedNodeProxy, TypedTreeNodeState, Union, UnionOptions, UnionPrimitive, UnionProxy, UnionSetInput, UnionVariants, ValidationError, Validator, applyDefaults, isCompatibleOperation, runValidators };
|
|
902
1016
|
}
|
|
903
1017
|
declare namespace Transaction_d_exports {
|
|
904
1018
|
export { EncodedTransaction, Transaction, decode, empty, encode, isEmpty, make$2 as make, merge };
|