@regle/core 0.5.1 → 0.5.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.
- package/README.md +0 -4
- package/dist/regle-core.browser.js +57278 -0
- package/dist/regle-core.browser.min.js +72 -0
- package/dist/regle-core.cjs +116 -113
- package/dist/regle-core.d.cts +672 -5
- package/dist/regle-core.d.ts +672 -5
- package/dist/regle-core.min.cjs +2 -2
- package/dist/regle-core.min.mjs +2 -2
- package/dist/regle-core.mjs +117 -115
- package/package.json +2 -1
package/dist/regle-core.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MaybeRef, Ref, UnwrapRef, UnwrapNestedRefs, MaybeRefOrGetter } from 'vue';
|
|
1
|
+
import { MaybeRef, Ref, UnwrapRef, UnwrapNestedRefs, MaybeRefOrGetter, ComputedRef } from 'vue';
|
|
2
2
|
|
|
3
3
|
type Prettify<T> = T extends infer R ? {
|
|
4
4
|
[K in keyof R]: R[K];
|
|
@@ -78,6 +78,23 @@ Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
|
78
78
|
*/
|
|
79
79
|
type BuiltIns = Primitive | void | Date | RegExp;
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
Test if the given function has multiple call signatures.
|
|
83
|
+
|
|
84
|
+
Needed to handle the case of a single call signature with properties.
|
|
85
|
+
|
|
86
|
+
Multiple call signatures cannot currently be supported due to a TypeScript limitation.
|
|
87
|
+
@see https://github.com/microsoft/TypeScript/issues/29732
|
|
88
|
+
*/
|
|
89
|
+
type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
|
|
90
|
+
T extends {(...arguments_: infer A): unknown; (...arguments_: infer B): unknown}
|
|
91
|
+
? B extends A
|
|
92
|
+
? A extends B
|
|
93
|
+
? false
|
|
94
|
+
: true
|
|
95
|
+
: true
|
|
96
|
+
: false;
|
|
97
|
+
|
|
81
98
|
/**
|
|
82
99
|
@see PartialDeep
|
|
83
100
|
*/
|
|
@@ -186,6 +203,83 @@ type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOpt
|
|
|
186
203
|
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
|
|
187
204
|
};
|
|
188
205
|
|
|
206
|
+
type ExcludeUndefined<T> = Exclude<T, undefined>;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
Create a type from another type with all keys and nested keys set to required.
|
|
210
|
+
|
|
211
|
+
Use-cases:
|
|
212
|
+
- Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
|
|
213
|
+
- Modeling the resulting type after a deep merge with a set of defaults.
|
|
214
|
+
|
|
215
|
+
@example
|
|
216
|
+
```
|
|
217
|
+
import type {RequiredDeep} from 'type-fest';
|
|
218
|
+
|
|
219
|
+
type Settings = {
|
|
220
|
+
textEditor?: {
|
|
221
|
+
fontSize?: number | undefined;
|
|
222
|
+
fontColor?: string | undefined;
|
|
223
|
+
fontWeight?: number | undefined;
|
|
224
|
+
}
|
|
225
|
+
autocomplete?: boolean | undefined;
|
|
226
|
+
autosave?: boolean | undefined;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
type RequiredSettings = RequiredDeep<Settings>;
|
|
230
|
+
// type RequiredSettings = {
|
|
231
|
+
// textEditor: {
|
|
232
|
+
// fontSize: number;
|
|
233
|
+
// fontColor: string;
|
|
234
|
+
// fontWeight: number;
|
|
235
|
+
// }
|
|
236
|
+
// autocomplete: boolean;
|
|
237
|
+
// autosave: boolean;
|
|
238
|
+
// }
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
|
|
242
|
+
|
|
243
|
+
@category Utilities
|
|
244
|
+
@category Object
|
|
245
|
+
@category Array
|
|
246
|
+
@category Set
|
|
247
|
+
@category Map
|
|
248
|
+
*/
|
|
249
|
+
type RequiredDeep<T, E extends ExcludeUndefined<T> = ExcludeUndefined<T>> = E extends BuiltIns
|
|
250
|
+
? E
|
|
251
|
+
: E extends Map<infer KeyType, infer ValueType>
|
|
252
|
+
? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
|
|
253
|
+
: E extends Set<infer ItemType>
|
|
254
|
+
? Set<RequiredDeep<ItemType>>
|
|
255
|
+
: E extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
256
|
+
? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
|
|
257
|
+
: E extends ReadonlySet<infer ItemType>
|
|
258
|
+
? ReadonlySet<RequiredDeep<ItemType>>
|
|
259
|
+
: E extends WeakMap<infer KeyType, infer ValueType>
|
|
260
|
+
? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
|
|
261
|
+
: E extends WeakSet<infer ItemType>
|
|
262
|
+
? WeakSet<RequiredDeep<ItemType>>
|
|
263
|
+
: E extends Promise<infer ValueType>
|
|
264
|
+
? Promise<RequiredDeep<ValueType>>
|
|
265
|
+
: E extends (...arguments_: any[]) => unknown
|
|
266
|
+
? {} extends RequiredObjectDeep<E>
|
|
267
|
+
? E
|
|
268
|
+
: HasMultipleCallSignatures<E> extends true
|
|
269
|
+
? E
|
|
270
|
+
: ((...arguments_: Parameters<E>) => ReturnType<E>) & RequiredObjectDeep<E>
|
|
271
|
+
: E extends object
|
|
272
|
+
? E extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
273
|
+
? ItemType[] extends E // Test for arrays (non-tuples) specifically
|
|
274
|
+
? Array<RequiredDeep<ItemType>> // Recreate relevant array type to prevent eager evaluation of circular reference
|
|
275
|
+
: RequiredObjectDeep<E> // Tuples behave properly
|
|
276
|
+
: RequiredObjectDeep<E>
|
|
277
|
+
: unknown;
|
|
278
|
+
|
|
279
|
+
type RequiredObjectDeep<ObjectType extends object> = {
|
|
280
|
+
[KeyType in keyof ObjectType]-?: RequiredDeep<ObjectType[KeyType]>
|
|
281
|
+
};
|
|
282
|
+
|
|
189
283
|
/**
|
|
190
284
|
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
191
285
|
|
|
@@ -231,7 +325,7 @@ type Intersection = UnionToIntersection<Union>;
|
|
|
231
325
|
|
|
232
326
|
@category Type
|
|
233
327
|
*/
|
|
234
|
-
type UnionToIntersection<Union> = (
|
|
328
|
+
type UnionToIntersection$1<Union> = (
|
|
235
329
|
// `extends unknown` is always going to be the case and is used to convert the
|
|
236
330
|
// `Union` into a [distributive conditional
|
|
237
331
|
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
@@ -289,6 +383,7 @@ interface RegleValidationGroupOutput {
|
|
|
289
383
|
type FieldRegleBehaviourOptions = AddDollarToOptions<RegleBehaviourOptions> & {
|
|
290
384
|
$debounce?: number;
|
|
291
385
|
};
|
|
386
|
+
type ResolvedRegleBehaviourOptions = DeepMaybeRef<RequiredDeep<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>, Record<string, any>, Record<string, any[]>>;
|
|
292
387
|
type ShortcutCommonFn<T extends Record<string, any>> = {
|
|
293
388
|
[x: string]: (element: OmitByType<T, Function>) => unknown;
|
|
294
389
|
};
|
|
@@ -307,7 +402,7 @@ interface Regle<TState extends Record<string, any> = EmptyObject, TRules extends
|
|
|
307
402
|
type isDeepExact<T, U> = {
|
|
308
403
|
[K in keyof T]-?: CheckDeepExact<NonNullable<T[K]>, K extends keyof U ? NonNullable<U[K]> : never>;
|
|
309
404
|
}[keyof T] extends true ? true : false;
|
|
310
|
-
type CheckDeepExact<T, U> = [U] extends [never] ? false : T extends RegleCollectionRuleDecl ? U extends RegleCollectionRuleDecl ? isDeepExact<NonNullable<T['$each']>, UnionToIntersection<NonNullable<U['$each']>>> : T extends RegleRuleDecl ? true : T extends ReglePartialRuleTree<any> ? isDeepExact<T, U> : false : T extends RegleRuleDecl ? true : T extends ReglePartialRuleTree<any> ? isDeepExact<T, U> : false;
|
|
405
|
+
type CheckDeepExact<T, U> = [U] extends [never] ? false : T extends RegleCollectionRuleDecl ? U extends RegleCollectionRuleDecl ? isDeepExact<NonNullable<T['$each']>, UnionToIntersection$1<NonNullable<U['$each']>>> : T extends RegleRuleDecl ? true : T extends ReglePartialRuleTree<any> ? isDeepExact<T, U> : false : T extends RegleRuleDecl ? true : T extends ReglePartialRuleTree<any> ? isDeepExact<T, U> : false;
|
|
311
406
|
type DeepReactiveState<T extends Record<string, any>> = {
|
|
312
407
|
[K in keyof T]: MaybeRef<T[K]>;
|
|
313
408
|
};
|
|
@@ -327,7 +422,7 @@ type IsPropertyOutputRequired<TState, TRule extends RegleFormPropertyType<any, a
|
|
|
327
422
|
] extends [TState] ? unknown : NonNullable<TState> extends Array<any> ? TRule extends RegleCollectionRuleDecl<any, any> ? ArrayHaveAtLeastOneRequiredField<NonNullable<TState>, TRule> extends false ? false : true : false : TRule extends ReglePartialRuleTree<any, any> ? ExtendOnlyRealRecord<TState> extends true ? ObjectHaveAtLeastOneRequiredField<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, TRule> extends false ? false : true : TRule extends RegleRuleDecl<any, any> ? FieldHaveRequiredRule<TRule> extends false ? false : true : false : false;
|
|
328
423
|
type SafeFieldProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleRuleDecl<any, any> ? unknown extends TRule['required'] ? Maybe<TState> : TRule['required'] extends undefined ? never : TRule['required'] extends RegleRuleDefinition<any, infer Params, any, any, any> ? Params extends never[] ? Maybe<TState> : Maybe<TState> : Maybe<TState> : Maybe<TState>;
|
|
329
424
|
|
|
330
|
-
type useRegleFn<TCustomRules extends Partial<AllRulesDeclarations>, TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid, TValidationGroups extends Record<string, RegleValidationGroupEntry[]>, TValid = isDeepExact<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>> extends true ? {} :
|
|
425
|
+
type useRegleFn<TCustomRules extends Partial<AllRulesDeclarations>, TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid, TValidationGroups extends Record<string, RegleValidationGroupEntry[]>, TValid = isDeepExact<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>> extends true ? {} : MismatchInfo<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, rulesFactory: MaybeRefOrGetter<TRules>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Unwrap<TState>, TRules, TValidationGroups>) => Regle<Unwrap<TState>, TRules, TValidationGroups, TShortcuts>;
|
|
331
426
|
declare const useRegle: useRegleFn<Partial<AllRulesDeclarations>, RegleShortcutDefinition<any>>;
|
|
332
427
|
|
|
333
428
|
interface inferRulesFn<TCustomRules extends Partial<AllRulesDeclarations>> {
|
|
@@ -336,6 +431,17 @@ interface inferRulesFn<TCustomRules extends Partial<AllRulesDeclarations>> {
|
|
|
336
431
|
}
|
|
337
432
|
declare const inferRules: inferRulesFn<Partial<AllRulesDeclarations>>;
|
|
338
433
|
|
|
434
|
+
declare function useRootStorage({ initialState, options, scopeRules, state, customRules, shortcuts, }: {
|
|
435
|
+
scopeRules: ComputedRef<$InternalReglePartialRuleTree>;
|
|
436
|
+
state: Ref<Record<string, any>>;
|
|
437
|
+
options: ResolvedRegleBehaviourOptions;
|
|
438
|
+
initialState: Record<string, any>;
|
|
439
|
+
customRules?: () => CustomRulesDeclarationTree;
|
|
440
|
+
shortcuts: RegleShortcutDefinition | undefined;
|
|
441
|
+
}): {
|
|
442
|
+
regle: $InternalRegleStatus | undefined;
|
|
443
|
+
};
|
|
444
|
+
|
|
339
445
|
type InferRegleRoot<T extends (...args: any[]) => Regle<any, any, any, any>> = T extends (...args: any[]) => infer U ? U extends Regle<any, any, any, any> ? Prettify<U['r$']> : never : never;
|
|
340
446
|
type InferRegleRules<T extends useRegleFn<any, any>> = T extends useRegleFn<infer U, any> ? UnwrapRuleTree<Partial<U> & Partial<DefaultValidators>> : {};
|
|
341
447
|
type InferRegleShortcuts<T extends useRegleFn<any, any>> = T extends useRegleFn<any, infer U> ? U : {};
|
|
@@ -346,6 +452,451 @@ type RegleEnforceCustomRequiredRules<T extends Partial<AllRulesDeclarations> | u
|
|
|
346
452
|
[K in TRules]-?: T extends useRegleFn<any> ? K extends keyof InferRegleRules<T> ? NonNullable<InferRegleRules<T>[K]> : never : K extends keyof T ? NonNullable<T[K]> : never;
|
|
347
453
|
};
|
|
348
454
|
|
|
455
|
+
/**
|
|
456
|
+
* Negates a boolean type.
|
|
457
|
+
*/
|
|
458
|
+
type Not<T extends boolean> = T extends true ? false : true;
|
|
459
|
+
/**
|
|
460
|
+
* Checks if all the boolean types in the {@linkcode Types} array are `true`.
|
|
461
|
+
*/
|
|
462
|
+
type And<Types extends boolean[]> = Types[number] extends true ? true : false;
|
|
463
|
+
/**
|
|
464
|
+
* @internal
|
|
465
|
+
*/
|
|
466
|
+
declare const secret: unique symbol;
|
|
467
|
+
/**
|
|
468
|
+
* @internal
|
|
469
|
+
*/
|
|
470
|
+
type Secret = typeof secret;
|
|
471
|
+
/**
|
|
472
|
+
* Checks if the given type is `never`.
|
|
473
|
+
*/
|
|
474
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
475
|
+
/**
|
|
476
|
+
* Checks if the given type is `any`.
|
|
477
|
+
*/
|
|
478
|
+
type IsAny<T> = [T] extends [Secret] ? Not<IsNever<T>> : false;
|
|
479
|
+
/**
|
|
480
|
+
* Determines if the given type is `unknown`.
|
|
481
|
+
*/
|
|
482
|
+
type IsUnknown<T> = [unknown] extends [T] ? Not<IsAny<T>> : false;
|
|
483
|
+
/**
|
|
484
|
+
* Subjective "useful" keys from a type. For objects it's just `keyof` but for
|
|
485
|
+
* tuples/arrays it's the number keys.
|
|
486
|
+
*
|
|
487
|
+
* @example
|
|
488
|
+
* ```ts
|
|
489
|
+
* UsefulKeys<{ a: 1; b: 2 }> // 'a' | 'b'
|
|
490
|
+
*
|
|
491
|
+
* UsefulKeys<['a', 'b']> // '0' | '1'
|
|
492
|
+
*
|
|
493
|
+
* UsefulKeys<string[]> // number
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
496
|
+
type UsefulKeys<T> = T extends any[] ? {
|
|
497
|
+
[K in keyof T]: K;
|
|
498
|
+
}[number] : keyof T;
|
|
499
|
+
/**
|
|
500
|
+
* Extracts the keys from a type that are required (not optional).
|
|
501
|
+
*/
|
|
502
|
+
type RequiredKeys<T> = Extract<{
|
|
503
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
|
|
504
|
+
}[keyof T], keyof T>;
|
|
505
|
+
/**
|
|
506
|
+
* Gets the keys of an object type that are optional.
|
|
507
|
+
*/
|
|
508
|
+
type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>;
|
|
509
|
+
/**
|
|
510
|
+
* Extracts the keys from a type that are not `readonly`.
|
|
511
|
+
*/
|
|
512
|
+
type ReadonlyKeys<T> = Extract<{
|
|
513
|
+
[K in keyof T]-?: ReadonlyEquivalent<{
|
|
514
|
+
[_K in K]: T[K];
|
|
515
|
+
}, {
|
|
516
|
+
-readonly [_K in K]: T[K];
|
|
517
|
+
}> extends true ? never : K;
|
|
518
|
+
}[keyof T], keyof T>;
|
|
519
|
+
/**
|
|
520
|
+
* Determines if two types, are equivalent in a `readonly` manner.
|
|
521
|
+
*
|
|
522
|
+
* @internal
|
|
523
|
+
*/
|
|
524
|
+
type ReadonlyEquivalent<X, Y> = Extends<(<T>() => T extends X ? true : false), (<T>() => T extends Y ? true : false)>;
|
|
525
|
+
/**
|
|
526
|
+
* Checks if one type extends another. Note: this is not quite the same as `Left extends Right` because:
|
|
527
|
+
* 1. If either type is `never`, the result is `true` iff the other type is also `never`.
|
|
528
|
+
* 2. Types are wrapped in a 1-tuple so that union types are not distributed - instead we consider `string | number` to _not_ extend `number`. If we used `Left extends Right` directly you would get `Extends<string | number, number>` => `false | true` => `boolean`.
|
|
529
|
+
*/
|
|
530
|
+
type Extends<Left, Right> = IsNever<Left> extends true ? IsNever<Right> : [Left] extends [Right] ? true : false;
|
|
531
|
+
/**
|
|
532
|
+
* Checks if two types are strictly equal using
|
|
533
|
+
* the TypeScript internal identical-to operator.
|
|
534
|
+
*
|
|
535
|
+
* @see {@link https://github.com/microsoft/TypeScript/issues/55188#issuecomment-1656328122 | much history}
|
|
536
|
+
*/
|
|
537
|
+
type StrictEqualUsingTSInternalIdenticalToOperator<L, R> = (<T>() => T extends (L & T) | T ? true : false) extends <T>() => T extends (R & T) | T ? true : false ? IsNever<L> extends IsNever<R> ? true : false : false;
|
|
538
|
+
/**
|
|
539
|
+
* Checks that {@linkcode Left} and {@linkcode Right} extend each other.
|
|
540
|
+
* Not quite the same as an equality check since `any` can make it resolve
|
|
541
|
+
* to `true`. So should only be used when {@linkcode Left} and
|
|
542
|
+
* {@linkcode Right} are known to avoid `any`.
|
|
543
|
+
*/
|
|
544
|
+
type MutuallyExtends<Left, Right> = And<[Extends<Left, Right>, Extends<Right, Left>]>;
|
|
545
|
+
/**
|
|
546
|
+
* Convert a union to an intersection.
|
|
547
|
+
* `A | B | C` -\> `A & B & C`
|
|
548
|
+
*/
|
|
549
|
+
type UnionToIntersection<Union> = (Union extends any ? (distributedUnion: Union) => void : never) extends (mergedIntersection: infer Intersection) => void ? Intersection : never;
|
|
550
|
+
/**
|
|
551
|
+
* Get the last element of a union.
|
|
552
|
+
* First, converts to a union of `() => T` functions,
|
|
553
|
+
* then uses {@linkcode UnionToIntersection} to get the last one.
|
|
554
|
+
*/
|
|
555
|
+
type LastOf<Union> = UnionToIntersection<Union extends any ? () => Union : never> extends () => infer R ? R : never;
|
|
556
|
+
/**
|
|
557
|
+
* Intermediate type for {@linkcode UnionToTuple} which pushes the
|
|
558
|
+
* "last" union member to the end of a tuple, and recursively prepends
|
|
559
|
+
* the remainder of the union.
|
|
560
|
+
*/
|
|
561
|
+
type TuplifyUnion<Union, LastElement = LastOf<Union>> = IsNever<Union> extends true ? [] : [...TuplifyUnion<Exclude<Union, LastElement>>, LastElement];
|
|
562
|
+
/**
|
|
563
|
+
* Convert a union like `1 | 2 | 3` to a tuple like `[1, 2, 3]`.
|
|
564
|
+
*/
|
|
565
|
+
type UnionToTuple<Union> = TuplifyUnion<Union>;
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The simple(ish) way to get overload info from a function
|
|
569
|
+
* {@linkcode FunctionType}. Recent versions of TypeScript will match any
|
|
570
|
+
* function against a generic 10-overload type, filling in slots with
|
|
571
|
+
* duplicates of the function. So, we can just match against a single type
|
|
572
|
+
* and get all the overloads.
|
|
573
|
+
*
|
|
574
|
+
* For older versions of TypeScript, we'll need to painstakingly do
|
|
575
|
+
* ten separate matches.
|
|
576
|
+
*/
|
|
577
|
+
type TSPost53OverloadsInfoUnion<FunctionType> = FunctionType extends {
|
|
578
|
+
(...args: infer A1): infer R1;
|
|
579
|
+
(...args: infer A2): infer R2;
|
|
580
|
+
(...args: infer A3): infer R3;
|
|
581
|
+
(...args: infer A4): infer R4;
|
|
582
|
+
(...args: infer A5): infer R5;
|
|
583
|
+
(...args: infer A6): infer R6;
|
|
584
|
+
(...args: infer A7): infer R7;
|
|
585
|
+
(...args: infer A8): infer R8;
|
|
586
|
+
(...args: infer A9): infer R9;
|
|
587
|
+
(...args: infer A10): infer R10;
|
|
588
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) | ((...p: A9) => R9) | ((...p: A10) => R10) : never;
|
|
589
|
+
/**
|
|
590
|
+
* A function with `unknown` parameters and return type.
|
|
591
|
+
*/
|
|
592
|
+
type UnknownFunction = (...args: unknown[]) => unknown;
|
|
593
|
+
/**
|
|
594
|
+
* `true` iff {@linkcode FunctionType} is
|
|
595
|
+
* equivalent to `(...args: unknown[]) => unknown`,
|
|
596
|
+
* which is what an overload variant looks like for a non-existent overload.
|
|
597
|
+
* This is useful because older versions of TypeScript end up with
|
|
598
|
+
* 9 "useless" overloads and one real one for parameterless/generic functions.
|
|
599
|
+
*
|
|
600
|
+
* @see {@link https://github.com/microsoft/TypeScript/issues/28867 | Related}
|
|
601
|
+
*/
|
|
602
|
+
type IsUselessOverloadInfo<FunctionType> = StrictEqualUsingTSInternalIdenticalToOperator<FunctionType, UnknownFunction>;
|
|
603
|
+
/**
|
|
604
|
+
* Old versions of TypeScript can sometimes seem to refuse to separate out
|
|
605
|
+
* union members unless you put them each in a pointless tuple and add an
|
|
606
|
+
* extra `infer X` expression. There may be a better way to work around this
|
|
607
|
+
* problem, but since it's not a problem in newer versions of TypeScript,
|
|
608
|
+
* it's not a priority right now.
|
|
609
|
+
*/
|
|
610
|
+
type Tuplify<Union> = Union extends infer X ? [X] : never;
|
|
611
|
+
/**
|
|
612
|
+
* For older versions of TypeScript, we need two separate workarounds
|
|
613
|
+
* to get overload info. First, we need need to use
|
|
614
|
+
* {@linkcode DecreasingOverloadsInfoUnion} to get the overload info for
|
|
615
|
+
* functions with 1-10 overloads. Then, we need to filter out the
|
|
616
|
+
* "useless" overloads that are present in older versions of TypeScript,
|
|
617
|
+
* for parameterless functions. To do this we use
|
|
618
|
+
* {@linkcode IsUselessOverloadInfo} to remove useless overloads.
|
|
619
|
+
*
|
|
620
|
+
* @see {@link https://github.com/microsoft/TypeScript/issues/28867 | Related}
|
|
621
|
+
*/
|
|
622
|
+
type TSPre53OverloadsInfoUnion<FunctionType> = Tuplify<DecreasingOverloadsInfoUnion<FunctionType>> extends infer Tup ? Tup extends [infer Fn] ? IsUselessOverloadInfo<Fn> extends true ? never : Fn : never : never;
|
|
623
|
+
/**
|
|
624
|
+
* For versions of TypeScript below 5.3, we need to check for 10 overloads,
|
|
625
|
+
* then 9, then 8, etc., to get a union of the overload variants.
|
|
626
|
+
*/
|
|
627
|
+
type DecreasingOverloadsInfoUnion<F> = F extends {
|
|
628
|
+
(...args: infer A1): infer R1;
|
|
629
|
+
(...args: infer A2): infer R2;
|
|
630
|
+
(...args: infer A3): infer R3;
|
|
631
|
+
(...args: infer A4): infer R4;
|
|
632
|
+
(...args: infer A5): infer R5;
|
|
633
|
+
(...args: infer A6): infer R6;
|
|
634
|
+
(...args: infer A7): infer R7;
|
|
635
|
+
(...args: infer A8): infer R8;
|
|
636
|
+
(...args: infer A9): infer R9;
|
|
637
|
+
(...args: infer A10): infer R10;
|
|
638
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) | ((...p: A9) => R9) | ((...p: A10) => R10) : F extends {
|
|
639
|
+
(...args: infer A1): infer R1;
|
|
640
|
+
(...args: infer A2): infer R2;
|
|
641
|
+
(...args: infer A3): infer R3;
|
|
642
|
+
(...args: infer A4): infer R4;
|
|
643
|
+
(...args: infer A5): infer R5;
|
|
644
|
+
(...args: infer A6): infer R6;
|
|
645
|
+
(...args: infer A7): infer R7;
|
|
646
|
+
(...args: infer A8): infer R8;
|
|
647
|
+
(...args: infer A9): infer R9;
|
|
648
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) | ((...p: A9) => R9) : F extends {
|
|
649
|
+
(...args: infer A1): infer R1;
|
|
650
|
+
(...args: infer A2): infer R2;
|
|
651
|
+
(...args: infer A3): infer R3;
|
|
652
|
+
(...args: infer A4): infer R4;
|
|
653
|
+
(...args: infer A5): infer R5;
|
|
654
|
+
(...args: infer A6): infer R6;
|
|
655
|
+
(...args: infer A7): infer R7;
|
|
656
|
+
(...args: infer A8): infer R8;
|
|
657
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) | ((...p: A8) => R8) : F extends {
|
|
658
|
+
(...args: infer A1): infer R1;
|
|
659
|
+
(...args: infer A2): infer R2;
|
|
660
|
+
(...args: infer A3): infer R3;
|
|
661
|
+
(...args: infer A4): infer R4;
|
|
662
|
+
(...args: infer A5): infer R5;
|
|
663
|
+
(...args: infer A6): infer R6;
|
|
664
|
+
(...args: infer A7): infer R7;
|
|
665
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) | ((...p: A7) => R7) : F extends {
|
|
666
|
+
(...args: infer A1): infer R1;
|
|
667
|
+
(...args: infer A2): infer R2;
|
|
668
|
+
(...args: infer A3): infer R3;
|
|
669
|
+
(...args: infer A4): infer R4;
|
|
670
|
+
(...args: infer A5): infer R5;
|
|
671
|
+
(...args: infer A6): infer R6;
|
|
672
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) | ((...p: A6) => R6) : F extends {
|
|
673
|
+
(...args: infer A1): infer R1;
|
|
674
|
+
(...args: infer A2): infer R2;
|
|
675
|
+
(...args: infer A3): infer R3;
|
|
676
|
+
(...args: infer A4): infer R4;
|
|
677
|
+
(...args: infer A5): infer R5;
|
|
678
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) | ((...p: A5) => R5) : F extends {
|
|
679
|
+
(...args: infer A1): infer R1;
|
|
680
|
+
(...args: infer A2): infer R2;
|
|
681
|
+
(...args: infer A3): infer R3;
|
|
682
|
+
(...args: infer A4): infer R4;
|
|
683
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) | ((...p: A4) => R4) : F extends {
|
|
684
|
+
(...args: infer A1): infer R1;
|
|
685
|
+
(...args: infer A2): infer R2;
|
|
686
|
+
(...args: infer A3): infer R3;
|
|
687
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) | ((...p: A3) => R3) : F extends {
|
|
688
|
+
(...args: infer A1): infer R1;
|
|
689
|
+
(...args: infer A2): infer R2;
|
|
690
|
+
} ? ((...p: A1) => R1) | ((...p: A2) => R2) : F extends (...args: infer A1) => infer R1 ? ((...p: A1) => R1) : never;
|
|
691
|
+
/**
|
|
692
|
+
* Get a union of overload variants for a function {@linkcode FunctionType}.
|
|
693
|
+
* Does a check for whether we can do the one-shot
|
|
694
|
+
* 10-overload matcher (which works for ts\>5.3), and if not,
|
|
695
|
+
* falls back to the more complicated utility.
|
|
696
|
+
*/
|
|
697
|
+
type OverloadsInfoUnion<FunctionType> = IsNever<TSPost53OverloadsInfoUnion<(a: 1) => 2>> extends true ? TSPre53OverloadsInfoUnion<FunctionType> : TSPost53OverloadsInfoUnion<FunctionType>;
|
|
698
|
+
/**
|
|
699
|
+
* The simple(ish) way to get overload info from a constructor
|
|
700
|
+
* {@linkcode ConstructorType}. Recent versions of TypeScript will match any
|
|
701
|
+
* constructor against a generic 10-overload type, filling in slots with
|
|
702
|
+
* duplicates of the constructor. So, we can just match against a single type
|
|
703
|
+
* and get all the overloads.
|
|
704
|
+
*
|
|
705
|
+
* For older versions of TypeScript,
|
|
706
|
+
* we'll need to painstakingly do ten separate matches.
|
|
707
|
+
*/
|
|
708
|
+
type TSPost53ConstructorOverloadsInfoUnion<ConstructorType> = ConstructorType extends {
|
|
709
|
+
new (...args: infer A1): infer R1;
|
|
710
|
+
new (...args: infer A2): infer R2;
|
|
711
|
+
new (...args: infer A3): infer R3;
|
|
712
|
+
new (...args: infer A4): infer R4;
|
|
713
|
+
new (...args: infer A5): infer R5;
|
|
714
|
+
new (...args: infer A6): infer R6;
|
|
715
|
+
new (...args: infer A7): infer R7;
|
|
716
|
+
new (...args: infer A8): infer R8;
|
|
717
|
+
new (...args: infer A9): infer R9;
|
|
718
|
+
new (...args: infer A10): infer R10;
|
|
719
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) | (new (...p: A9) => R9) | (new (...p: A10) => R10) : never;
|
|
720
|
+
/**
|
|
721
|
+
* A constructor function with `unknown` parameters and return type.
|
|
722
|
+
*/
|
|
723
|
+
type UnknownConstructor = new (...args: unknown[]) => unknown;
|
|
724
|
+
/**
|
|
725
|
+
* Same as {@linkcode IsUselessOverloadInfo}, but for constructors.
|
|
726
|
+
*/
|
|
727
|
+
type IsUselessConstructorOverloadInfo<FunctionType> = StrictEqualUsingTSInternalIdenticalToOperator<FunctionType, UnknownConstructor>;
|
|
728
|
+
/**
|
|
729
|
+
* For older versions of TypeScript, we need two separate workarounds to
|
|
730
|
+
* get constructor overload info. First, we need need to use
|
|
731
|
+
* {@linkcode DecreasingConstructorOverloadsInfoUnion} to get the overload
|
|
732
|
+
* info for constructors with 1-10 overloads. Then, we need to filter out the
|
|
733
|
+
* "useless" overloads that are present in older versions of TypeScript,
|
|
734
|
+
* for parameterless constructors. To do this we use
|
|
735
|
+
* {@linkcode IsUselessConstructorOverloadInfo} to remove useless overloads.
|
|
736
|
+
*
|
|
737
|
+
* @see {@link https://github.com/microsoft/TypeScript/issues/28867 | Related}
|
|
738
|
+
*/
|
|
739
|
+
type TSPre53ConstructorOverloadsInfoUnion<ConstructorType> = Tuplify<DecreasingConstructorOverloadsInfoUnion<ConstructorType>> extends infer Tup ? Tup extends [infer Ctor] ? IsUselessConstructorOverloadInfo<Ctor> extends true ? never : Ctor : never : never;
|
|
740
|
+
/**
|
|
741
|
+
* For versions of TypeScript below 5.3, we need to check for 10 overloads,
|
|
742
|
+
* then 9, then 8, etc., to get a union of the overload variants.
|
|
743
|
+
*/
|
|
744
|
+
type DecreasingConstructorOverloadsInfoUnion<ConstructorType> = ConstructorType extends {
|
|
745
|
+
new (...args: infer A1): infer R1;
|
|
746
|
+
new (...args: infer A2): infer R2;
|
|
747
|
+
new (...args: infer A3): infer R3;
|
|
748
|
+
new (...args: infer A4): infer R4;
|
|
749
|
+
new (...args: infer A5): infer R5;
|
|
750
|
+
new (...args: infer A6): infer R6;
|
|
751
|
+
new (...args: infer A7): infer R7;
|
|
752
|
+
new (...args: infer A8): infer R8;
|
|
753
|
+
new (...args: infer A9): infer R9;
|
|
754
|
+
new (...args: infer A10): infer R10;
|
|
755
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) | (new (...p: A9) => R9) | (new (...p: A10) => R10) : ConstructorType extends {
|
|
756
|
+
new (...args: infer A1): infer R1;
|
|
757
|
+
new (...args: infer A2): infer R2;
|
|
758
|
+
new (...args: infer A3): infer R3;
|
|
759
|
+
new (...args: infer A4): infer R4;
|
|
760
|
+
new (...args: infer A5): infer R5;
|
|
761
|
+
new (...args: infer A6): infer R6;
|
|
762
|
+
new (...args: infer A7): infer R7;
|
|
763
|
+
new (...args: infer A8): infer R8;
|
|
764
|
+
new (...args: infer A9): infer R9;
|
|
765
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) | (new (...p: A9) => R9) : ConstructorType extends {
|
|
766
|
+
new (...args: infer A1): infer R1;
|
|
767
|
+
new (...args: infer A2): infer R2;
|
|
768
|
+
new (...args: infer A3): infer R3;
|
|
769
|
+
new (...args: infer A4): infer R4;
|
|
770
|
+
new (...args: infer A5): infer R5;
|
|
771
|
+
new (...args: infer A6): infer R6;
|
|
772
|
+
new (...args: infer A7): infer R7;
|
|
773
|
+
new (...args: infer A8): infer R8;
|
|
774
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) | (new (...p: A8) => R8) : ConstructorType extends {
|
|
775
|
+
new (...args: infer A1): infer R1;
|
|
776
|
+
new (...args: infer A2): infer R2;
|
|
777
|
+
new (...args: infer A3): infer R3;
|
|
778
|
+
new (...args: infer A4): infer R4;
|
|
779
|
+
new (...args: infer A5): infer R5;
|
|
780
|
+
new (...args: infer A6): infer R6;
|
|
781
|
+
new (...args: infer A7): infer R7;
|
|
782
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) | (new (...p: A7) => R7) : ConstructorType extends {
|
|
783
|
+
new (...args: infer A1): infer R1;
|
|
784
|
+
new (...args: infer A2): infer R2;
|
|
785
|
+
new (...args: infer A3): infer R3;
|
|
786
|
+
new (...args: infer A4): infer R4;
|
|
787
|
+
new (...args: infer A5): infer R5;
|
|
788
|
+
new (...args: infer A6): infer R6;
|
|
789
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) | (new (...p: A6) => R6) : ConstructorType extends {
|
|
790
|
+
new (...args: infer A1): infer R1;
|
|
791
|
+
new (...args: infer A2): infer R2;
|
|
792
|
+
new (...args: infer A3): infer R3;
|
|
793
|
+
new (...args: infer A4): infer R4;
|
|
794
|
+
new (...args: infer A5): infer R5;
|
|
795
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) | (new (...p: A5) => R5) : ConstructorType extends {
|
|
796
|
+
new (...args: infer A1): infer R1;
|
|
797
|
+
new (...args: infer A2): infer R2;
|
|
798
|
+
new (...args: infer A3): infer R3;
|
|
799
|
+
new (...args: infer A4): infer R4;
|
|
800
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) | (new (...p: A4) => R4) : ConstructorType extends {
|
|
801
|
+
new (...args: infer A1): infer R1;
|
|
802
|
+
new (...args: infer A2): infer R2;
|
|
803
|
+
new (...args: infer A3): infer R3;
|
|
804
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) | (new (...p: A3) => R3) : ConstructorType extends {
|
|
805
|
+
new (...args: infer A1): infer R1;
|
|
806
|
+
new (...args: infer A2): infer R2;
|
|
807
|
+
} ? (new (...p: A1) => R1) | (new (...p: A2) => R2) : ConstructorType extends new (...args: infer A1) => infer R1 ? (new (...p: A1) => R1) : never;
|
|
808
|
+
/**
|
|
809
|
+
* Get a union of overload variants for a constructor
|
|
810
|
+
* {@linkcode ConstructorType}. Does a check for whether we can do the
|
|
811
|
+
* one-shot 10-overload matcher (which works for ts\>5.3), and if not,
|
|
812
|
+
* falls back to the more complicated utility.
|
|
813
|
+
*/
|
|
814
|
+
type ConstructorOverloadsUnion<ConstructorType> = IsNever<TSPost53ConstructorOverloadsInfoUnion<new (a: 1) => any>> extends true ? TSPre53ConstructorOverloadsInfoUnion<ConstructorType> : TSPost53ConstructorOverloadsInfoUnion<ConstructorType>;
|
|
815
|
+
/**
|
|
816
|
+
* Allows inferring any constructor using the `infer` keyword.
|
|
817
|
+
*/
|
|
818
|
+
type InferConstructor<ConstructorType extends new (...args: any) => any> = ConstructorType;
|
|
819
|
+
/**
|
|
820
|
+
* A union type of the parameters allowed for any overload
|
|
821
|
+
* of constructor {@linkcode ConstructorType}.
|
|
822
|
+
*/
|
|
823
|
+
type ConstructorOverloadParameters<ConstructorType> = ConstructorOverloadsUnion<ConstructorType> extends InferConstructor<infer Ctor> ? ConstructorParameters<Ctor> : never;
|
|
824
|
+
/**
|
|
825
|
+
* Calculates the number of overloads for a given function type.
|
|
826
|
+
*/
|
|
827
|
+
type NumOverloads<FunctionType> = UnionToTuple<OverloadsInfoUnion<FunctionType>>['length'];
|
|
828
|
+
|
|
829
|
+
/**
|
|
830
|
+
* Represents a deeply branded type.
|
|
831
|
+
*
|
|
832
|
+
* Recursively walk a type and replace it with a branded type related to the
|
|
833
|
+
* original. This is useful for equality-checking stricter than
|
|
834
|
+
* `A extends B ? B extends A ? true : false : false`, because it detects the
|
|
835
|
+
* difference between a few edge-case types that vanilla TypeScript
|
|
836
|
+
* doesn't by default:
|
|
837
|
+
* - `any` vs `unknown`
|
|
838
|
+
* - `{ readonly a: string }` vs `{ a: string }`
|
|
839
|
+
* - `{ a?: string }` vs `{ a: string | undefined }`
|
|
840
|
+
*
|
|
841
|
+
* __Note__: not very performant for complex types - this should only be used
|
|
842
|
+
* when you know you need it. If doing an equality check, it's almost always
|
|
843
|
+
* better to use {@linkcode StrictEqualUsingTSInternalIdenticalToOperator}.
|
|
844
|
+
*/
|
|
845
|
+
type DeepBrand<T> = IsNever<T> extends true ? {
|
|
846
|
+
type: 'never';
|
|
847
|
+
} : IsAny<T> extends true ? {
|
|
848
|
+
type: 'any';
|
|
849
|
+
} : IsUnknown<T> extends true ? {
|
|
850
|
+
type: 'unknown';
|
|
851
|
+
} : T extends string | number | boolean | symbol | bigint | null | undefined | void ? {
|
|
852
|
+
type: 'primitive';
|
|
853
|
+
value: T;
|
|
854
|
+
} : T extends new (...args: any[]) => any ? {
|
|
855
|
+
type: 'constructor';
|
|
856
|
+
params: ConstructorOverloadParameters<T>;
|
|
857
|
+
instance: DeepBrand<InstanceType<Extract<T, new (...args: any) => any>>>;
|
|
858
|
+
} : T extends (...args: infer P) => infer R ? NumOverloads<T> extends 1 ? {
|
|
859
|
+
type: 'function';
|
|
860
|
+
params: DeepBrand<P>;
|
|
861
|
+
return: DeepBrand<R>;
|
|
862
|
+
this: DeepBrand<ThisParameterType<T>>;
|
|
863
|
+
props: DeepBrand<Omit<T, keyof Function>>;
|
|
864
|
+
} : UnionToTuple<OverloadsInfoUnion<T>> extends infer OverloadsTuple ? {
|
|
865
|
+
type: 'overloads';
|
|
866
|
+
overloads: {
|
|
867
|
+
[K in keyof OverloadsTuple]: DeepBrand<OverloadsTuple[K]>;
|
|
868
|
+
};
|
|
869
|
+
} : never : T extends any[] ? {
|
|
870
|
+
type: 'array';
|
|
871
|
+
items: {
|
|
872
|
+
[K in keyof T]: T[K];
|
|
873
|
+
};
|
|
874
|
+
} : {
|
|
875
|
+
type: 'object';
|
|
876
|
+
properties: {
|
|
877
|
+
[K in keyof T]: DeepBrand<T[K]>;
|
|
878
|
+
};
|
|
879
|
+
readonly: ReadonlyKeys<T>;
|
|
880
|
+
required: RequiredKeys<T>;
|
|
881
|
+
optional: OptionalKeys<T>;
|
|
882
|
+
constructorParams: DeepBrand<ConstructorOverloadParameters<T>>;
|
|
883
|
+
};
|
|
884
|
+
/**
|
|
885
|
+
* Checks if two types are strictly equal using branding.
|
|
886
|
+
*/
|
|
887
|
+
type StrictEqualUsingBranding<Left, Right> = MutuallyExtends<DeepBrand<Left>, DeepBrand<Right>>;
|
|
888
|
+
|
|
889
|
+
/**
|
|
890
|
+
* Borrowed from vitest
|
|
891
|
+
*/
|
|
892
|
+
type PrintType<T> = IsUnknown<T> extends true ? 'unknown' : IsNever<T> extends true ? 'never' : IsAny<T> extends true ? never : boolean extends T ? 'boolean' : T extends boolean ? `literal boolean: ${T}` : string extends T ? 'string' : T extends string ? `literal string: ${T}` : number extends T ? 'number' : T extends number ? `literal number: ${T}` : bigint extends T ? 'bigint' : T extends bigint ? `literal bigint: ${T}` : T extends null ? 'null' : T extends undefined ? 'undefined' : T extends (...args: any[]) => any ? 'function' : '...';
|
|
893
|
+
/**
|
|
894
|
+
* Borrowed from vitest
|
|
895
|
+
*/
|
|
896
|
+
type MismatchInfo<Actual, Expected> = And<[Extends<PrintType<Actual>, '...'>, Not<IsAny<Actual>>]> extends true ? And<[Extends<any[], Actual>, Extends<any[], Expected>]> extends true ? Array<MismatchInfo<Extract<Actual, any[]>[number], Extract<Expected, any[]>[number]>> : {
|
|
897
|
+
[K in UsefulKeys<Actual> | UsefulKeys<Expected>]: MismatchInfo<K extends keyof Actual ? Actual[K] : never, K extends keyof Expected ? Expected[K] : never>;
|
|
898
|
+
} : StrictEqualUsingBranding<Actual, Expected> extends true ? Actual : `[Regle error] The parent property does not match the form schema`;
|
|
899
|
+
|
|
349
900
|
type ParamDecl<T = any> = MaybeRef<Maybe<T>> | (() => Maybe<T>);
|
|
350
901
|
type CreateFn<T extends any[]> = (...args: T) => any;
|
|
351
902
|
/**
|
|
@@ -460,6 +1011,7 @@ interface RegleRuleInit<TValue extends any, TParams extends [...any[]] = [], TRe
|
|
|
460
1011
|
active?: RegleInitPropertyGetter<TValue, boolean, TParams, TMetadata>;
|
|
461
1012
|
tooltip?: RegleInitPropertyGetter<TValue, string | string[], TParams, TMetadata>;
|
|
462
1013
|
type?: string;
|
|
1014
|
+
async?: TAsync;
|
|
463
1015
|
}
|
|
464
1016
|
/**
|
|
465
1017
|
* @argument
|
|
@@ -472,6 +1024,10 @@ interface RegleRuleCore<TValue extends any, TParams extends any[] = [], TAsync e
|
|
|
472
1024
|
tooltip?: RegleInitPropertyGetter<TValue, string | string[], TParams, TMetadata>;
|
|
473
1025
|
type?: string;
|
|
474
1026
|
}
|
|
1027
|
+
type RegleRuleTypeReturn<TValue, TParams extends [...any[]]> = {
|
|
1028
|
+
value: TValue;
|
|
1029
|
+
params: [...TParams];
|
|
1030
|
+
};
|
|
475
1031
|
|
|
476
1032
|
type DefaultValidators = {
|
|
477
1033
|
alpha: RegleRuleDefinition<string>;
|
|
@@ -547,10 +1103,22 @@ type RegleRuleTree<TForm extends Record<string, any>, TCustomRules extends Parti
|
|
|
547
1103
|
type RegleComputedRules<TForm extends MaybeRef<Record<string, any>> | DeepReactiveState<Record<string, any>>, TCustomRules extends Partial<AllRulesDeclarations> | Regle<any, any> = Partial<AllRulesDeclarations>, TState = Unwrap<TForm>, TCustom = TCustomRules extends Regle<any, infer R> ? R extends ReglePartialRuleTree<any, infer C> ? C : Partial<AllRulesDeclarations> : TCustomRules> = {
|
|
548
1104
|
[TKey in keyof TState]?: RegleFormPropertyType<TState[TKey], TCustom extends Partial<AllRulesDeclarations> ? TCustom : {}>;
|
|
549
1105
|
};
|
|
1106
|
+
/**
|
|
1107
|
+
* @internal
|
|
1108
|
+
* @reference {@link ReglePartialRuleTree}
|
|
1109
|
+
*/
|
|
1110
|
+
type $InternalReglePartialRuleTree = {
|
|
1111
|
+
[x: string]: $InternalFormPropertyTypes;
|
|
1112
|
+
};
|
|
550
1113
|
/**
|
|
551
1114
|
* @public
|
|
552
1115
|
*/
|
|
553
1116
|
type RegleFormPropertyType<TValue = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = [NonNullable<TValue>] extends [never] ? RegleRuleDecl<TValue, TCustomRules> : NonNullable<TValue> extends Array<any> ? RegleCollectionRuleDecl<TValue, TCustomRules> : NonNullable<TValue> extends Date ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends File ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends Ref<infer V> ? RegleFormPropertyType<V, TCustomRules> : NonNullable<TValue> extends Record<string, any> ? ReglePartialRuleTree<NonNullable<TValue>, TCustomRules> : RegleRuleDecl<NonNullable<TValue>, TCustomRules>;
|
|
1117
|
+
/**
|
|
1118
|
+
* @internal
|
|
1119
|
+
* @reference {@link RegleFormPropertyType}
|
|
1120
|
+
*/
|
|
1121
|
+
type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollectionRuleDecl | $InternalReglePartialRuleTree | FieldRegleBehaviourOptions;
|
|
554
1122
|
/**
|
|
555
1123
|
* @public
|
|
556
1124
|
* Rule tree for a form property
|
|
@@ -558,6 +1126,11 @@ type RegleFormPropertyType<TValue = any, TCustomRules extends Partial<AllRulesDe
|
|
|
558
1126
|
type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = FieldRegleBehaviourOptions & {
|
|
559
1127
|
[TKey in keyof TCustomRules]?: NonNullable<TCustomRules[TKey]> extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, [...TParams, ...args: [...any[]]], boolean> : NonNullable<TCustomRules[TKey]> extends RegleRuleDefinition<any, any, any, any> ? FormRuleDeclaration<TValue, any> : FormRuleDeclaration<TValue, any> | FieldRegleBehaviourOptions[keyof FieldRegleBehaviourOptions];
|
|
560
1128
|
};
|
|
1129
|
+
/**
|
|
1130
|
+
* @internal
|
|
1131
|
+
* @reference {@link RegleRuleDecl}
|
|
1132
|
+
*/
|
|
1133
|
+
type $InternalRegleRuleDecl = FieldRegleBehaviourOptions & Record<string, FormRuleDeclaration<any, any>>;
|
|
561
1134
|
/**
|
|
562
1135
|
* @public
|
|
563
1136
|
*/
|
|
@@ -572,6 +1145,13 @@ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRul
|
|
|
572
1145
|
} & RegleRuleDecl<NonNullable<TValue>, TCustomRules>) | ({
|
|
573
1146
|
$each?: MaybeGetter<RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>, ArrayElement<TValue>, RegleCollectionRuleDeclKeyProperty>;
|
|
574
1147
|
} & FieldRegleBehaviourOptions);
|
|
1148
|
+
/**
|
|
1149
|
+
* @internal
|
|
1150
|
+
* @reference {@link RegleCollectionRuleDecl}
|
|
1151
|
+
*/
|
|
1152
|
+
type $InternalRegleCollectionRuleDecl = $InternalRegleRuleDecl & {
|
|
1153
|
+
$each?: MaybeGetter<$InternalFormPropertyTypes & RegleCollectionRuleDeclKeyProperty, any>;
|
|
1154
|
+
};
|
|
575
1155
|
/**
|
|
576
1156
|
* @public
|
|
577
1157
|
*/
|
|
@@ -597,6 +1177,18 @@ type RegleExternalCollectionErrors<TState extends Record<string, any>> = {
|
|
|
597
1177
|
readonly $self?: string[];
|
|
598
1178
|
readonly $each?: RegleValidationErrors<TState, true>[];
|
|
599
1179
|
};
|
|
1180
|
+
/** @internal */
|
|
1181
|
+
type $InternalRegleCollectionErrors = {
|
|
1182
|
+
readonly $self?: string[];
|
|
1183
|
+
readonly $each?: $InternalRegleErrors[];
|
|
1184
|
+
};
|
|
1185
|
+
type $InternalRegleErrorTree = {
|
|
1186
|
+
[x: string]: $InternalRegleErrors;
|
|
1187
|
+
};
|
|
1188
|
+
/**
|
|
1189
|
+
* @internal
|
|
1190
|
+
*/
|
|
1191
|
+
type $InternalRegleErrors = $InternalRegleCollectionErrors | string[] | $InternalRegleErrorTree;
|
|
600
1192
|
type PartialFormState<TState extends Record<string, any>> = [unknown] extends [TState] ? {} : Prettify<{
|
|
601
1193
|
[K in keyof TState as ExtendOnlyRealRecord<TState[K]> extends true ? never : TState[K] extends Array<any> ? never : K]?: Maybe<TState[K]>;
|
|
602
1194
|
} & {
|
|
@@ -609,6 +1201,10 @@ type RegleResult<Data extends Record<string, any> | any[] | unknown, TRules exte
|
|
|
609
1201
|
result: true;
|
|
610
1202
|
data: Data extends Array<infer U extends Record<string, any>> ? DeepSafeFormState<U, TRules>[] : Data extends Date | File ? SafeFieldProperty<Data, TRules> : Data extends Record<string, any> ? DeepSafeFormState<Data, TRules> : SafeFieldProperty<Data, TRules>;
|
|
611
1203
|
};
|
|
1204
|
+
type $InternalRegleResult = {
|
|
1205
|
+
result: boolean;
|
|
1206
|
+
data: any;
|
|
1207
|
+
};
|
|
612
1208
|
|
|
613
1209
|
/**
|
|
614
1210
|
* @public
|
|
@@ -634,10 +1230,28 @@ type RegleStatus<TState extends Record<string, any> | undefined = Record<string,
|
|
|
634
1230
|
} & ([TShortcuts['nested']] extends [never] ? {} : {
|
|
635
1231
|
[K in keyof TShortcuts['nested']]: ReturnType<NonNullable<TShortcuts['nested']>[K]>;
|
|
636
1232
|
});
|
|
1233
|
+
/**
|
|
1234
|
+
* @internal
|
|
1235
|
+
* @reference {@link RegleStatus}
|
|
1236
|
+
*/
|
|
1237
|
+
interface $InternalRegleStatus extends RegleCommonStatus {
|
|
1238
|
+
$fields: {
|
|
1239
|
+
[x: string]: $InternalRegleStatusType;
|
|
1240
|
+
};
|
|
1241
|
+
readonly $errors: Record<string, $InternalRegleErrors>;
|
|
1242
|
+
readonly $silentErrors: Record<string, $InternalRegleErrors>;
|
|
1243
|
+
$extractDirtyFields: (filterNullishValues?: boolean) => Record<string, any>;
|
|
1244
|
+
$validate: () => Promise<$InternalRegleResult>;
|
|
1245
|
+
}
|
|
637
1246
|
/**
|
|
638
1247
|
* @public
|
|
639
1248
|
*/
|
|
640
1249
|
type InferRegleStatusType<TRule extends RegleCollectionRuleDecl | RegleRuleDecl | ReglePartialRuleTree<any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string, TShortcuts extends RegleShortcutDefinition = {}> = NonNullable<TState[TKey]> extends Array<Record<string, any> | any> ? TRule extends RegleCollectionRuleDefinition<any, any> ? ExtractFromGetter<TRule['$each']> extends ReglePartialRuleTree<any> ? RegleCollectionStatus<TState[TKey], ExtractFromGetter<TRule['$each']>, TRule, TShortcuts> : RegleFieldStatus<TState[TKey], TRule, TShortcuts> : RegleCollectionStatus<TState[TKey], {}, TRule, TShortcuts> : TRule extends ReglePartialRuleTree<any> ? NonNullable<TState[TKey]> extends Array<any> ? RegleCommonStatus<TState[TKey]> : NonNullable<TState[TKey]> extends Date | File ? RegleFieldStatus<TState[TKey], TRule, TShortcuts> : NonNullable<TState[TKey]> extends Record<PropertyKey, any> ? RegleStatus<TState[TKey], TRule, TShortcuts> : RegleFieldStatus<TState[TKey], TRule, TShortcuts> : NonNullable<TState[TKey]> extends Date | File ? RegleFieldStatus<TState[TKey], TRule, TShortcuts> : NonNullable<TState[TKey]> extends Record<PropertyKey, any> ? RegleStatus<TState[TKey], ReglePartialRuleTree<TState[TKey]>, TShortcuts> : RegleFieldStatus<TState[TKey], TRule, TShortcuts>;
|
|
1250
|
+
/**
|
|
1251
|
+
* @internal
|
|
1252
|
+
* @reference {@link InferRegleStatusType}
|
|
1253
|
+
*/
|
|
1254
|
+
type $InternalRegleStatusType = $InternalRegleCollectionStatus | $InternalRegleStatus | $InternalRegleFieldStatus;
|
|
641
1255
|
/**
|
|
642
1256
|
* @public
|
|
643
1257
|
*/
|
|
@@ -656,6 +1270,20 @@ type RegleFieldStatus<TState extends any = any, TRules extends RegleFormProperty
|
|
|
656
1270
|
} & ([TShortcuts['fields']] extends [never] ? {} : {
|
|
657
1271
|
[K in keyof TShortcuts['fields']]: ReturnType<NonNullable<TShortcuts['fields']>[K]>;
|
|
658
1272
|
});
|
|
1273
|
+
/**
|
|
1274
|
+
* @internal
|
|
1275
|
+
* @reference {@link RegleFieldStatus}
|
|
1276
|
+
*/
|
|
1277
|
+
interface $InternalRegleFieldStatus extends RegleCommonStatus {
|
|
1278
|
+
$value: any;
|
|
1279
|
+
$silentValue: any;
|
|
1280
|
+
readonly $rules: Record<string, $InternalRegleRuleStatus>;
|
|
1281
|
+
readonly $externalErrors?: string[];
|
|
1282
|
+
readonly $errors: string[];
|
|
1283
|
+
readonly $silentErrors: string[];
|
|
1284
|
+
$extractDirtyFields: (filterNullishValues?: boolean) => any;
|
|
1285
|
+
$validate: () => Promise<$InternalRegleResult>;
|
|
1286
|
+
}
|
|
659
1287
|
/**
|
|
660
1288
|
* @public
|
|
661
1289
|
*/
|
|
@@ -698,6 +1326,29 @@ type RegleRuleStatus<TValue = any, TParams extends any[] = any[], TMetadata exte
|
|
|
698
1326
|
} : {
|
|
699
1327
|
readonly $params: TParams;
|
|
700
1328
|
});
|
|
1329
|
+
/**
|
|
1330
|
+
* @internal
|
|
1331
|
+
* @reference {@link RegleRuleStatus}
|
|
1332
|
+
*/
|
|
1333
|
+
interface $InternalRegleRuleStatus {
|
|
1334
|
+
$type: string;
|
|
1335
|
+
$message: string | string[];
|
|
1336
|
+
$tooltip: string | string[];
|
|
1337
|
+
$active: boolean;
|
|
1338
|
+
$valid: boolean;
|
|
1339
|
+
$pending: boolean;
|
|
1340
|
+
$path: string;
|
|
1341
|
+
$externalErrors?: string[];
|
|
1342
|
+
$params?: any[];
|
|
1343
|
+
$metadata: any;
|
|
1344
|
+
$haveAsync: boolean;
|
|
1345
|
+
$validating: boolean;
|
|
1346
|
+
$validator(value: any, ...args: any[]): RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>;
|
|
1347
|
+
$validate(): Promise<boolean>;
|
|
1348
|
+
$unwatch(): void;
|
|
1349
|
+
$watch(): void;
|
|
1350
|
+
$reset(): void;
|
|
1351
|
+
}
|
|
701
1352
|
/**
|
|
702
1353
|
* @public
|
|
703
1354
|
*/
|
|
@@ -713,6 +1364,22 @@ type RegleCollectionStatus<TState extends any[] = any[], TRules extends ReglePar
|
|
|
713
1364
|
} & ([TShortcuts['collections']] extends [never] ? {} : {
|
|
714
1365
|
[K in keyof TShortcuts['collections']]: ReturnType<NonNullable<TShortcuts['collections']>[K]>;
|
|
715
1366
|
});
|
|
1367
|
+
/**
|
|
1368
|
+
* @internal
|
|
1369
|
+
* @reference {@link RegleCollectionStatus}
|
|
1370
|
+
*/
|
|
1371
|
+
interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields' | '$errors' | '$silentErrors'> {
|
|
1372
|
+
readonly $field: $InternalRegleFieldStatus;
|
|
1373
|
+
readonly $each: Array<$InternalRegleStatusType>;
|
|
1374
|
+
readonly $errors: $InternalRegleCollectionErrors;
|
|
1375
|
+
readonly $silentErrors: $InternalRegleCollectionErrors;
|
|
1376
|
+
readonly $externalErrors?: string[];
|
|
1377
|
+
$extractDirtyFields: (filterNullishValues?: boolean) => any[];
|
|
1378
|
+
$validate: () => Promise<$InternalRegleResult>;
|
|
1379
|
+
/** Track each array state */
|
|
1380
|
+
$unwatch(): void;
|
|
1381
|
+
$watch(): void;
|
|
1382
|
+
}
|
|
716
1383
|
|
|
717
1384
|
/**
|
|
718
1385
|
* @description
|
|
@@ -762,4 +1429,4 @@ declare function defineRegleConfig<TShortcuts extends RegleShortcutDefinition<TC
|
|
|
762
1429
|
inferRules: inferRulesFn<TCustomRules>;
|
|
763
1430
|
};
|
|
764
1431
|
|
|
765
|
-
export { type AllRulesDeclarations, type DeepMaybeRef, type FormRuleDeclaration, type InferRegleRoot, type InferRegleRule, type InferRegleRules, type InferRegleShortcuts, type InlineRuleDeclaration, InternalRuleType, type LocalRegleBehaviourOptions, type Maybe, type ParamDecl, type Regle, type RegleBehaviourOptions, type RegleCollectionErrors, type RegleCollectionRuleDecl, type RegleCollectionRuleDefinition, type RegleCollectionStatus, type RegleCommonStatus, type RegleComputedRules, type RegleEnforceCustomRequiredRules, type RegleEnforceRequiredRules, type RegleErrorTree, type RegleExternalErrorTree, type RegleFieldStatus, type RegleFormPropertyType, type RegleInternalRuleDefs, type ReglePartialRuleTree, type RegleResult, type RegleRoot, type RegleRuleCore, type RegleRuleDecl, type RegleRuleDefinition, type RegleRuleDefinitionProcessor, type RegleRuleDefinitionWithMetadataProcessor, type RegleRuleInit, type RegleRuleMetadataConsumer, type RegleRuleMetadataDefinition, type RegleRuleRaw, type RegleRuleStatus, type RegleRuleWithParamsDefinition, type RegleStatus, type RegleUniversalParams, type RegleValidationErrors, type RegleValidationGroupEntry, type RegleValidationGroupOutput, type RegleRuleTree as RegleValidationTree, type Unwrap, type UnwrapRegleUniversalParams, type UnwrapRuleWithParams, createRule, defineRegleConfig, inferRules, unwrapRuleParameters, useRegle };
|
|
1432
|
+
export { type $InternalRegleStatus, type AllRulesDeclarations, type DeepMaybeRef, type FormRuleDeclaration, type InferRegleRoot, type InferRegleRule, type InferRegleRules, type InferRegleShortcuts, type InferRegleStatusType, type InlineRuleDeclaration, InternalRuleType, type LocalRegleBehaviourOptions, type Maybe, type NoInferLegacy, type ParamDecl, type PrimitiveTypes, type Regle, type RegleBehaviourOptions, type RegleCollectionErrors, type RegleCollectionRuleDecl, type RegleCollectionRuleDefinition, type RegleCollectionStatus, type RegleCommonStatus, type RegleComputedRules, type RegleEnforceCustomRequiredRules, type RegleEnforceRequiredRules, type RegleErrorTree, type RegleExternalCollectionErrors, type RegleExternalErrorTree, type RegleFieldStatus, type RegleFormPropertyType, type RegleInternalRuleDefs, type ReglePartialRuleTree, type RegleResult, type RegleRoot, type RegleRuleCore, type RegleRuleDecl, type RegleRuleDefinition, type RegleRuleDefinitionProcessor, type RegleRuleDefinitionWithMetadataProcessor, type RegleRuleInit, type RegleRuleMetadataConsumer, type RegleRuleMetadataDefinition, type RegleRuleMetadataExtended, type RegleRuleRaw, type RegleRuleStatus, type RegleRuleTypeReturn, type RegleRuleWithParamsDefinition, type RegleShortcutDefinition, type RegleStatus, type RegleUniversalParams, type RegleValidationErrors, type RegleValidationGroupEntry, type RegleValidationGroupOutput, type RegleRuleTree as RegleValidationTree, type ResolvedRegleBehaviourOptions, type Unwrap, type UnwrapRegleUniversalParams, type UnwrapRuleWithParams, createRule, defineRegleConfig, inferRules, unwrapRuleParameters, useRegle, useRootStorage };
|