@regle/schemas 1.1.0-beta.2 → 1.1.0-beta.3

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.
@@ -1,6 +1,6 @@
1
- import { Maybe, PrimitiveTypes, RegleShortcutDefinition, RegleFieldStatus, RegleCommonStatus, JoinDiscriminatedUnions, RegleRuleStatus, RegleCollectionErrors, RegleErrorTree, DeepMaybeRef, RegleBehaviourOptions, MismatchInfo, DeepReactiveState, LocalRegleBehaviourOptions, NoInferLegacy } from '@regle/core';
1
+ import { Maybe, PrimitiveTypes, RegleShortcutDefinition, RegleCommonStatus, RegleRuleStatus, JoinDiscriminatedUnions, RegleCollectionErrors, RegleErrorTree, DeepReactiveState, NoInferLegacy, RegleBehaviourOptions } from '@regle/core';
2
2
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
- import { Raw, MaybeRef, MaybeRefOrGetter, UnwrapNestedRefs } from 'vue';
3
+ import { Raw, MaybeRef, UnwrapNestedRefs } from 'vue';
4
4
 
5
5
  /**
6
6
  Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
@@ -53,6 +53,102 @@ Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<n
53
53
  */
54
54
  type EmptyObject = {[emptyObjectSymbol]?: never};
55
55
 
56
+ /**
57
+ Extract all required keys from the given type.
58
+
59
+ This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
60
+
61
+ @example
62
+ ```
63
+ import type {RequiredKeysOf} from 'type-fest';
64
+
65
+ declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
66
+
67
+ interface User {
68
+ name: string;
69
+ surname: string;
70
+
71
+ luckyNumber?: number;
72
+ }
73
+
74
+ const validator1 = createValidation<User>('name', value => value.length < 25);
75
+ const validator2 = createValidation<User>('surname', value => value.length < 25);
76
+ ```
77
+
78
+ @category Utilities
79
+ */
80
+ type RequiredKeysOf<BaseType extends object> = Exclude<{
81
+ [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
82
+ ? Key
83
+ : never
84
+ }[keyof BaseType], undefined>;
85
+
86
+ /**
87
+ Returns a boolean for whether the given type is `never`.
88
+
89
+ @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
90
+ @link https://stackoverflow.com/a/53984913/10292952
91
+ @link https://www.zhenghao.io/posts/ts-never
92
+
93
+ Useful in type utilities, such as checking if something does not occur.
94
+
95
+ @example
96
+ ```
97
+ import type {IsNever, And} from 'type-fest';
98
+
99
+ // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
100
+ type AreStringsEqual<A extends string, B extends string> =
101
+ And<
102
+ IsNever<Exclude<A, B>> extends true ? true : false,
103
+ IsNever<Exclude<B, A>> extends true ? true : false
104
+ >;
105
+
106
+ type EndIfEqual<I extends string, O extends string> =
107
+ AreStringsEqual<I, O> extends true
108
+ ? never
109
+ : void;
110
+
111
+ function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
112
+ if (input === output) {
113
+ process.exit(0);
114
+ }
115
+ }
116
+
117
+ endIfEqual('abc', 'abc');
118
+ //=> never
119
+
120
+ endIfEqual('abc', '123');
121
+ //=> void
122
+ ```
123
+
124
+ @category Type Guard
125
+ @category Utilities
126
+ */
127
+ type IsNever<T> = [T] extends [never] ? true : false;
128
+
129
+ /**
130
+ An if-else-like type that resolves depending on whether the given type is `never`.
131
+
132
+ @see {@link IsNever}
133
+
134
+ @example
135
+ ```
136
+ import type {IfNever} from 'type-fest';
137
+
138
+ type ShouldBeTrue = IfNever<never>;
139
+ //=> true
140
+
141
+ type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
142
+ //=> 'bar'
143
+ ```
144
+
145
+ @category Type Guard
146
+ @category Utilities
147
+ */
148
+ type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
149
+ IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
150
+ );
151
+
56
152
  /**
57
153
  Extract the element of an array that also works for array union.
58
154
 
@@ -62,13 +158,426 @@ It creates a type-safe way to access the element type of `unknown` type.
62
158
  */
63
159
  type ArrayElement<T> = T extends readonly unknown[] ? T[0] : never;
64
160
 
161
+ // Can eventually be replaced with the built-in once this library supports
162
+ // TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
163
+ type NoInfer<T> = T extends infer U ? U : never;
164
+
165
+ /**
166
+ Returns a boolean for whether the given type is `any`.
167
+
168
+ @link https://stackoverflow.com/a/49928360/1490091
169
+
170
+ Useful in type utilities, such as disallowing `any`s to be passed to a function.
171
+
172
+ @example
173
+ ```
174
+ import type {IsAny} from 'type-fest';
175
+
176
+ const typedObject = {a: 1, b: 2} as const;
177
+ const anyObject: any = {a: 1, b: 2};
178
+
179
+ function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
180
+ return obj[key];
181
+ }
182
+
183
+ const typedA = get(typedObject, 'a');
184
+ //=> 1
185
+
186
+ const anyA = get(anyObject, 'a');
187
+ //=> any
188
+ ```
189
+
190
+ @category Type Guard
191
+ @category Utilities
192
+ */
193
+ type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
194
+
195
+ /**
196
+ Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
197
+
198
+ @example
199
+ ```
200
+ import type {Simplify} from 'type-fest';
201
+
202
+ type PositionProps = {
203
+ top: number;
204
+ left: number;
205
+ };
206
+
207
+ type SizeProps = {
208
+ width: number;
209
+ height: number;
210
+ };
211
+
212
+ // In your editor, hovering over `Props` will show a flattened object with all the properties.
213
+ type Props = Simplify<PositionProps & SizeProps>;
214
+ ```
215
+
216
+ Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
217
+
218
+ If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
219
+
220
+ @example
221
+ ```
222
+ import type {Simplify} from 'type-fest';
223
+
224
+ interface SomeInterface {
225
+ foo: number;
226
+ bar?: string;
227
+ baz: number | undefined;
228
+ }
229
+
230
+ type SomeType = {
231
+ foo: number;
232
+ bar?: string;
233
+ baz: number | undefined;
234
+ };
235
+
236
+ const literal = {foo: 123, bar: 'hello', baz: 456};
237
+ const someType: SomeType = literal;
238
+ const someInterface: SomeInterface = literal;
239
+
240
+ function fn(object: Record<string, unknown>): void {}
241
+
242
+ fn(literal); // Good: literal object type is sealed
243
+ fn(someType); // Good: type is sealed
244
+ fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
245
+ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
246
+ ```
247
+
248
+ @link https://github.com/microsoft/TypeScript/issues/15300
249
+ @see SimplifyDeep
250
+ @category Object
251
+ */
252
+ type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
253
+
254
+ /**
255
+ Omit any index signatures from the given object type, leaving only explicitly defined properties.
256
+
257
+ This is the counterpart of `PickIndexSignature`.
258
+
259
+ Use-cases:
260
+ - Remove overly permissive signatures from third-party types.
261
+
262
+ This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
263
+
264
+ It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
265
+
266
+ (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
267
+
268
+ ```
269
+ const indexed: Record<string, unknown> = {}; // Allowed
270
+
271
+ const keyed: Record<'foo', unknown> = {}; // Error
272
+ // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
273
+ ```
274
+
275
+ Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
276
+
277
+ ```
278
+ type Indexed = {} extends Record<string, unknown>
279
+ ? '✅ `{}` is assignable to `Record<string, unknown>`'
280
+ : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
281
+ // => '✅ `{}` is assignable to `Record<string, unknown>`'
282
+
283
+ type Keyed = {} extends Record<'foo' | 'bar', unknown>
284
+ ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
285
+ : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
286
+ // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
287
+ ```
288
+
289
+ Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
290
+
291
+ ```
292
+ import type {OmitIndexSignature} from 'type-fest';
293
+
294
+ type OmitIndexSignature<ObjectType> = {
295
+ [KeyType in keyof ObjectType // Map each key of `ObjectType`...
296
+ ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
297
+ };
298
+ ```
299
+
300
+ ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
301
+
302
+ ```
303
+ import type {OmitIndexSignature} from 'type-fest';
304
+
305
+ type OmitIndexSignature<ObjectType> = {
306
+ [KeyType in keyof ObjectType
307
+ // Is `{}` assignable to `Record<KeyType, unknown>`?
308
+ as {} extends Record<KeyType, unknown>
309
+ ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
310
+ : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
311
+ ]: ObjectType[KeyType];
312
+ };
313
+ ```
314
+
315
+ If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
316
+
317
+ @example
318
+ ```
319
+ import type {OmitIndexSignature} from 'type-fest';
320
+
321
+ interface Example {
322
+ // These index signatures will be removed.
323
+ [x: string]: any
324
+ [x: number]: any
325
+ [x: symbol]: any
326
+ [x: `head-${string}`]: string
327
+ [x: `${string}-tail`]: string
328
+ [x: `head-${string}-tail`]: string
329
+ [x: `${bigint}`]: string
330
+ [x: `embedded-${number}`]: string
331
+
332
+ // These explicitly defined keys will remain.
333
+ foo: 'bar';
334
+ qux?: 'baz';
335
+ }
336
+
337
+ type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
338
+ // => { foo: 'bar'; qux?: 'baz' | undefined; }
339
+ ```
340
+
341
+ @see PickIndexSignature
342
+ @category Object
343
+ */
344
+ type OmitIndexSignature<ObjectType> = {
345
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
346
+ ? never
347
+ : KeyType]: ObjectType[KeyType];
348
+ };
349
+
350
+ /**
351
+ Pick only index signatures from the given object type, leaving out all explicitly defined properties.
352
+
353
+ This is the counterpart of `OmitIndexSignature`.
354
+
355
+ @example
356
+ ```
357
+ import type {PickIndexSignature} from 'type-fest';
358
+
359
+ declare const symbolKey: unique symbol;
360
+
361
+ type Example = {
362
+ // These index signatures will remain.
363
+ [x: string]: unknown;
364
+ [x: number]: unknown;
365
+ [x: symbol]: unknown;
366
+ [x: `head-${string}`]: string;
367
+ [x: `${string}-tail`]: string;
368
+ [x: `head-${string}-tail`]: string;
369
+ [x: `${bigint}`]: string;
370
+ [x: `embedded-${number}`]: string;
371
+
372
+ // These explicitly defined keys will be removed.
373
+ ['kebab-case-key']: string;
374
+ [symbolKey]: string;
375
+ foo: 'bar';
376
+ qux?: 'baz';
377
+ };
378
+
379
+ type ExampleIndexSignature = PickIndexSignature<Example>;
380
+ // {
381
+ // [x: string]: unknown;
382
+ // [x: number]: unknown;
383
+ // [x: symbol]: unknown;
384
+ // [x: `head-${string}`]: string;
385
+ // [x: `${string}-tail`]: string;
386
+ // [x: `head-${string}-tail`]: string;
387
+ // [x: `${bigint}`]: string;
388
+ // [x: `embedded-${number}`]: string;
389
+ // }
390
+ ```
391
+
392
+ @see OmitIndexSignature
393
+ @category Object
394
+ */
395
+ type PickIndexSignature<ObjectType> = {
396
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
397
+ ? KeyType
398
+ : never]: ObjectType[KeyType];
399
+ };
400
+
401
+ // Merges two objects without worrying about index signatures.
402
+ type SimpleMerge<Destination, Source> = {
403
+ [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
404
+ } & Source;
405
+
406
+ /**
407
+ Merge two types into a new type. Keys of the second type overrides keys of the first type.
408
+
409
+ @example
410
+ ```
411
+ import type {Merge} from 'type-fest';
412
+
413
+ interface Foo {
414
+ [x: string]: unknown;
415
+ [x: number]: unknown;
416
+ foo: string;
417
+ bar: symbol;
418
+ }
419
+
420
+ type Bar = {
421
+ [x: number]: number;
422
+ [x: symbol]: unknown;
423
+ bar: Date;
424
+ baz: boolean;
425
+ };
426
+
427
+ export type FooBar = Merge<Foo, Bar>;
428
+ // => {
429
+ // [x: string]: unknown;
430
+ // [x: number]: number;
431
+ // [x: symbol]: unknown;
432
+ // foo: string;
433
+ // bar: Date;
434
+ // baz: boolean;
435
+ // }
436
+ ```
437
+
438
+ @category Object
439
+ */
440
+ type Merge<Destination, Source> =
441
+ Simplify<
442
+ SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
443
+ & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
444
+ >;
445
+
446
+ /**
447
+ An if-else-like type that resolves depending on whether the given type is `any`.
448
+
449
+ @see {@link IsAny}
450
+
451
+ @example
452
+ ```
453
+ import type {IfAny} from 'type-fest';
454
+
455
+ type ShouldBeTrue = IfAny<any>;
456
+ //=> true
457
+
458
+ type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
459
+ //=> 'bar'
460
+ ```
461
+
462
+ @category Type Guard
463
+ @category Utilities
464
+ */
465
+ type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
466
+ IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
467
+ );
468
+
469
+ /**
470
+ Extract all optional keys from the given type.
471
+
472
+ This is useful when you want to create a new type that contains different type values for the optional keys only.
473
+
474
+ @example
475
+ ```
476
+ import type {OptionalKeysOf, Except} from 'type-fest';
477
+
478
+ interface User {
479
+ name: string;
480
+ surname: string;
481
+
482
+ luckyNumber?: number;
483
+ }
484
+
485
+ const REMOVE_FIELD = Symbol('remove field symbol');
486
+ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
487
+ [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
488
+ };
489
+
490
+ const update1: UpdateOperation<User> = {
491
+ name: 'Alice'
492
+ };
493
+
494
+ const update2: UpdateOperation<User> = {
495
+ name: 'Bob',
496
+ luckyNumber: REMOVE_FIELD
497
+ };
498
+ ```
499
+
500
+ @category Utilities
501
+ */
502
+ type OptionalKeysOf<BaseType extends object> = Exclude<{
503
+ [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
504
+ ? never
505
+ : Key
506
+ }[keyof BaseType], undefined>;
507
+
65
508
  /**
66
509
  Matches any primitive, `void`, `Date`, or `RegExp` value.
67
510
  */
68
511
  type BuiltIns = Primitive | void | Date | RegExp;
69
512
 
70
513
  /**
71
- @see PartialDeep
514
+ Merges user specified options with default options.
515
+
516
+ @example
517
+ ```
518
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
519
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
520
+ type SpecifiedOptions = {leavesOnly: true};
521
+
522
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
523
+ //=> {maxRecursionDepth: 10; leavesOnly: true}
524
+ ```
525
+
526
+ @example
527
+ ```
528
+ // Complains if default values are not provided for optional options
529
+
530
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
531
+ type DefaultPathsOptions = {maxRecursionDepth: 10};
532
+ type SpecifiedOptions = {};
533
+
534
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
535
+ // ~~~~~~~~~~~~~~~~~~~
536
+ // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
537
+ ```
538
+
539
+ @example
540
+ ```
541
+ // Complains if an option's default type does not conform to the expected type
542
+
543
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
544
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
545
+ type SpecifiedOptions = {};
546
+
547
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
548
+ // ~~~~~~~~~~~~~~~~~~~
549
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
550
+ ```
551
+
552
+ @example
553
+ ```
554
+ // Complains if an option's specified type does not conform to the expected type
555
+
556
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
557
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
558
+ type SpecifiedOptions = {leavesOnly: 'yes'};
559
+
560
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
561
+ // ~~~~~~~~~~~~~~~~
562
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
563
+ ```
564
+ */
565
+ type ApplyDefaultOptions<
566
+ Options extends object,
567
+ Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
568
+ SpecifiedOptions extends Options,
569
+ > =
570
+ IfAny<SpecifiedOptions, Defaults,
571
+ IfNever<SpecifiedOptions, Defaults,
572
+ Simplify<Merge<Defaults, {
573
+ [Key in keyof SpecifiedOptions
574
+ as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
575
+ ]: SpecifiedOptions[Key]
576
+ }> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
577
+ >>;
578
+
579
+ /**
580
+ @see {@link PartialDeep}
72
581
  */
73
582
  type PartialDeepOptions = {
74
583
  /**
@@ -77,6 +586,37 @@ type PartialDeepOptions = {
77
586
  @default false
78
587
  */
79
588
  readonly recurseIntoArrays?: boolean;
589
+
590
+ /**
591
+ Allows `undefined` values in non-tuple arrays.
592
+
593
+ - When set to `true`, elements of non-tuple arrays can be `undefined`.
594
+ - When set to `false`, only explicitly defined elements are allowed in non-tuple arrays, ensuring stricter type checking.
595
+
596
+ @default true
597
+
598
+ @example
599
+ You can prevent `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}` as the second type argument:
600
+
601
+ ```
602
+ import type {PartialDeep} from 'type-fest';
603
+
604
+ type Settings = {
605
+ languages: string[];
606
+ };
607
+
608
+ declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}>;
609
+
610
+ partialSettings.languages = [undefined]; // Error
611
+ partialSettings.languages = []; // Ok
612
+ ```
613
+ */
614
+ readonly allowUndefinedInNonTupleArrays?: boolean;
615
+ };
616
+
617
+ type DefaultPartialDeepOptions = {
618
+ recurseIntoArrays: false;
619
+ allowUndefinedInNonTupleArrays: true;
80
620
  };
81
621
 
82
622
  /**
@@ -92,12 +632,12 @@ import type {PartialDeep} from 'type-fest';
92
632
 
93
633
  const settings: Settings = {
94
634
  textEditor: {
95
- fontSize: 14;
96
- fontColor: '#000000';
97
- fontWeight: 400;
98
- }
99
- autocomplete: false;
100
- autosave: true;
635
+ fontSize: 14,
636
+ fontColor: '#000000',
637
+ fontWeight: 400
638
+ },
639
+ autocomplete: false,
640
+ autosave: true
101
641
  };
102
642
 
103
643
  const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
@@ -112,7 +652,7 @@ By default, this does not affect elements in array and tuple types. You can chan
112
652
  ```
113
653
  import type {PartialDeep} from 'type-fest';
114
654
 
115
- interface Settings {
655
+ type Settings = {
116
656
  languages: string[];
117
657
  }
118
658
 
@@ -121,12 +661,17 @@ const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
121
661
  };
122
662
  ```
123
663
 
664
+ @see {@link PartialDeepOptions}
665
+
124
666
  @category Object
125
667
  @category Array
126
668
  @category Set
127
669
  @category Map
128
670
  */
129
- type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
671
+ type PartialDeep<T, Options extends PartialDeepOptions = {}> =
672
+ _PartialDeep<T, ApplyDefaultOptions<PartialDeepOptions, DefaultPartialDeepOptions, Options>>;
673
+
674
+ type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
130
675
  ? T
131
676
  : T extends Map<infer KeyType, infer ValueType>
132
677
  ? PartialMapDeep<KeyType, ValueType, Options>
@@ -141,8 +686,8 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIn
141
686
  ? Options['recurseIntoArrays'] extends true
142
687
  ? ItemType[] extends T // Test for arrays (non-tuples) specifically
143
688
  ? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
144
- ? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
145
- : Array<PartialDeep<ItemType | undefined, Options>>
689
+ ? ReadonlyArray<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
690
+ : Array<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
146
691
  : PartialObjectDeep<T, Options> // Tuples behave properly
147
692
  : T // If they don't opt into array testing, just use the original type
148
693
  : PartialObjectDeep<T, Options>
@@ -151,28 +696,28 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIn
151
696
  /**
152
697
  Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
153
698
  */
154
- type PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & Map<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
699
+ type PartialMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & Map<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
155
700
 
156
701
  /**
157
702
  Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
158
703
  */
159
- type PartialSetDeep<T, Options extends PartialDeepOptions> = {} & Set<PartialDeep<T, Options>>;
704
+ type PartialSetDeep<T, Options extends Required<PartialDeepOptions>> = {} & Set<_PartialDeep<T, Options>>;
160
705
 
161
706
  /**
162
707
  Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
163
708
  */
164
- type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & ReadonlyMap<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
709
+ type PartialReadonlyMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & ReadonlyMap<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
165
710
 
166
711
  /**
167
712
  Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
168
713
  */
169
- type PartialReadonlySetDeep<T, Options extends PartialDeepOptions> = {} & ReadonlySet<PartialDeep<T, Options>>;
714
+ type PartialReadonlySetDeep<T, Options extends Required<PartialDeepOptions>> = {} & ReadonlySet<_PartialDeep<T, Options>>;
170
715
 
171
716
  /**
172
717
  Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
173
718
  */
174
- type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions> = {
175
- [KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
719
+ type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> = {
720
+ [KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
176
721
  };
177
722
 
178
723
  interface RegleSchema<TState extends Record<string, any>, TSchema extends Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}> {
@@ -183,13 +728,16 @@ interface RegleSchema<TState extends Record<string, any>, TSchema extends Record
183
728
  */
184
729
  r$: Raw<RegleSchemaStatus<TState, TSchema, TShortcuts, true>>;
185
730
  }
186
- interface RegleSingleFieldSchema<TState extends Maybe<PrimitiveTypes>, TSchema extends Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}> {
731
+ interface RegleSingleFieldSchema<TState extends Maybe<PrimitiveTypes>, TSchema extends unknown, TShortcuts extends RegleShortcutDefinition = {}> {
187
732
  /**
188
733
  * r$ is a reactive object containing the values, errors, dirty state and all the necessary validations properties you'll need to display informations.
189
734
  *
190
735
  * To see the list of properties: {@link https://reglejs.dev/core-concepts/validation-properties}
191
736
  */
192
- r$: Raw<RegleFieldStatus<TState, TSchema, TShortcuts>>;
737
+ r$: Raw<RegleSchemaFieldStatus<TState, TSchema, TShortcuts> & {
738
+ /** Sets all properties as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
739
+ $validate: () => Promise<RegleSchemaResult<TSchema>>;
740
+ }>;
193
741
  }
194
742
  type RegleSchemaResult<TSchema extends unknown> = {
195
743
  valid: false;
@@ -270,18 +818,11 @@ type RegleSchemaCollectionStatus<TSchema extends Record<string, any>, TState ext
270
818
  });
271
819
 
272
820
  interface useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never> {
273
- /**
274
- * Primitive parameter
275
- * */
276
- <TState extends Maybe<PrimitiveTypes>, TRules extends StandardSchemaV1<TState>>(state: MaybeRef<TState>, rulesFactory: MaybeRefOrGetter<TRules>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>>): RegleSingleFieldSchema<TState, TRules, TShortcuts>;
277
- /**
278
- * Object parameter
279
- * */
280
- <TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid, TValid = StandardSchemaV1.InferInput<TSchema> extends PartialDeep<UnwrapNestedRefs<TState>, {
821
+ <TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(state: MaybeRef<PartialDeep<TState, {
281
822
  recurseIntoArrays: true;
282
- }> ? {} : MismatchInfo<UnwrapNestedRefs<TState>, PartialDeep<StandardSchemaV1.InferInput<TSchema>, {
823
+ }>> | DeepReactiveState<PartialDeep<TState, {
283
824
  recurseIntoArrays: true;
284
- }>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, schema: MaybeRef<TSchema>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<UnwrapNestedRefs<TState>, {}, never>): RegleSchema<UnwrapNestedRefs<TState>, StandardSchemaV1.InferInput<TSchema>, TShortcuts>;
825
+ }>>, rulesFactory: MaybeRef<TSchema>): NonNullable<TState> extends PrimitiveTypes ? RegleSingleFieldSchema<NonNullable<TState>, StandardSchemaV1.InferInput<TSchema>, TShortcuts> : RegleSchema<UnwrapNestedRefs<NonNullable<TState>>, UnwrapNestedRefs<NonNullable<StandardSchemaV1.InferInput<TSchema>>>, TShortcuts>;
285
826
  }
286
827
  /**
287
828
  * useRegle serves as the foundation for validation logic.
@@ -323,12 +864,11 @@ declare const useRegleSchema: useRegleSchemaFn<RegleShortcutDefinition<any>>;
323
864
  declare function withDeps<TSchema extends StandardSchemaV1, TParams extends unknown[] = []>(schema: TSchema, depsArray: [...TParams]): TSchema;
324
865
 
325
866
  interface inferSchemaFn {
326
- <TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid, TValid = UnwrapNestedRefs<TState> extends PartialDeep<StandardSchemaV1.InferInput<TSchema>, {
867
+ <TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(state: MaybeRef<PartialDeep<TState, {
327
868
  recurseIntoArrays: true;
328
- }> ? {} : MismatchInfo<UnwrapNestedRefs<TState>, PartialDeep<StandardSchemaV1.InferInput<TSchema>, {
869
+ }>> | DeepReactiveState<PartialDeep<TState, {
329
870
  recurseIntoArrays: true;
330
- }>>>(state: MaybeRef<TState> | DeepReactiveState<TState> | undefined, rulesFactory: TSchema): NoInferLegacy<TSchema>;
331
- <TState extends PrimitiveTypes, TSchema extends StandardSchemaV1 & TValid, TValid = TState extends StandardSchemaV1.InferInput<TSchema> ? {} : MismatchInfo<TState, StandardSchemaV1.InferInput<TSchema>>>(state: MaybeRef<TState>, rulesFactory: TSchema): NoInferLegacy<TSchema>;
871
+ }>>, rulesFactory: MaybeRef<TSchema>): NoInferLegacy<TSchema>;
332
872
  }
333
873
  /**
334
874
  * Rule type helper to provide autocomplete and typecheck to your form rules or part of your form rules