@sinclair/typebox 0.28.3 → 0.28.5
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/package.json +1 -1
- package/readme.md +32 -19
- package/typebox.d.ts +37 -34
- package/typebox.js +39 -17
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -83,7 +83,7 @@ License MIT
|
|
|
83
83
|
- [Conditional](#types-conditional)
|
|
84
84
|
- [Template](#types-template-literal)
|
|
85
85
|
- [Indexed](#types-indexed)
|
|
86
|
-
- [
|
|
86
|
+
- [Rest](#types-rest)
|
|
87
87
|
- [Guards](#types-guards)
|
|
88
88
|
- [Unsafe](#types-unsafe)
|
|
89
89
|
- [Strict](#types-strict)
|
|
@@ -872,30 +872,43 @@ const A = Type.Index(T, ['x']) // type A = T['x']
|
|
|
872
872
|
|
|
873
873
|
const B = Type.Index(T, Type.KeyOf(T)) // type B = T[keyof T]
|
|
874
874
|
```
|
|
875
|
-
<a name='types-
|
|
875
|
+
<a name='types-rest'></a>
|
|
876
876
|
|
|
877
|
-
###
|
|
877
|
+
### Rest Types
|
|
878
878
|
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
The following creates variadic functions using the Rest type.
|
|
879
|
+
Rest parameters are supported with `Type.Rest`. This function is used to extract interior arrays from tuples to allow them to compose with the JavaScript spread operator `...`. This type can be used for tuple concatination and variadic function composition.
|
|
882
880
|
|
|
883
881
|
```typescript
|
|
884
882
|
// TypeScript
|
|
885
883
|
|
|
886
|
-
type
|
|
884
|
+
type T = [number, number] // type T = [number, number]
|
|
887
885
|
|
|
888
|
-
type
|
|
886
|
+
type C = [...T, number] // type C = [number, number, number]
|
|
889
887
|
|
|
890
|
-
type
|
|
888
|
+
type F = (...param: C) => void // type F = (
|
|
889
|
+
// param0: number,
|
|
890
|
+
// param1: number,
|
|
891
|
+
// param2: number,
|
|
892
|
+
// ) => void
|
|
891
893
|
|
|
892
894
|
// TypeBox
|
|
893
895
|
|
|
894
|
-
const
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
896
|
+
const T = Type.Tuple([ // const T: TTuple<[
|
|
897
|
+
Type.Number(), // TNumber,
|
|
898
|
+
Type.Number() // TNumber,
|
|
899
|
+
]) // ]>
|
|
900
|
+
|
|
901
|
+
const C = Type.Tuple([ // const C: TTuple<[
|
|
902
|
+
...Type.Rest(T), // TNumber,
|
|
903
|
+
Type.Number() // TNumber,
|
|
904
|
+
]) // TNumber
|
|
905
|
+
// ]>
|
|
906
|
+
|
|
907
|
+
const F = Type.Function(Type.Rest(C), Type.Void()) // const F: TFunction<[
|
|
908
|
+
// TNumber,
|
|
909
|
+
// TNumber,
|
|
910
|
+
// TNumber
|
|
911
|
+
// ], TVoid>
|
|
899
912
|
```
|
|
900
913
|
<a name='types-unsafe'></a>
|
|
901
914
|
|
|
@@ -1476,11 +1489,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1476
1489
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1477
1490
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1478
1491
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1479
|
-
│ typebox/compiler │ '
|
|
1480
|
-
│ typebox/errors │ '110.
|
|
1481
|
-
│ typebox/system │ '
|
|
1482
|
-
│ typebox/value │ '176.
|
|
1483
|
-
│ typebox │ '
|
|
1492
|
+
│ typebox/compiler │ '127.1 kb' │ ' 56.7 kb' │ '2.24 x' │
|
|
1493
|
+
│ typebox/errors │ '110.9 kb' │ ' 48.9 kb' │ '2.27 x' │
|
|
1494
|
+
│ typebox/system │ ' 76.3 kb' │ ' 31.2 kb' │ '2.44 x' │
|
|
1495
|
+
│ typebox/value │ '176.8 kb' │ ' 76.5 kb' │ '2.31 x' │
|
|
1496
|
+
│ typebox │ ' 75.2 kb' │ ' 30.8 kb' │ '2.44 x' │
|
|
1484
1497
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1485
1498
|
```
|
|
1486
1499
|
|
package/typebox.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ export type TOptional<T extends TSchema> = T & {
|
|
|
34
34
|
export type TReadonlyOptional<T extends TSchema> = T & {
|
|
35
35
|
[Modifier]: 'ReadonlyOptional';
|
|
36
36
|
};
|
|
37
|
-
export type Key =
|
|
37
|
+
export type Key = string | number;
|
|
38
38
|
export interface SchemaOptions {
|
|
39
39
|
$schema?: string;
|
|
40
40
|
/** Id for this schema */
|
|
@@ -168,18 +168,27 @@ export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends T
|
|
|
168
168
|
parameters: T;
|
|
169
169
|
returns: U;
|
|
170
170
|
}
|
|
171
|
-
export type
|
|
172
|
-
export type TIndexProperty<T extends TProperties, K extends IndexKey> = K extends keyof T ? [T[K]] : [
|
|
171
|
+
export type TIndexProperty<T extends TProperties, K extends Key> = T[K] extends infer R ? [R] : [
|
|
173
172
|
];
|
|
174
|
-
export type TIndexTuple<T extends TSchema[], K extends
|
|
173
|
+
export type TIndexTuple<T extends TSchema[], K extends Key> = K extends keyof T ? [T[K]] : [
|
|
175
174
|
];
|
|
176
|
-
export type TIndexComposite<T extends TSchema[], K extends
|
|
175
|
+
export type TIndexComposite<T extends TSchema[], K extends Key> = T extends [infer L, ...infer R] ? [...TIndexKey<AssertType<L>, K>, ...TIndexComposite<AssertRest<R>, K>] : [
|
|
177
176
|
];
|
|
178
|
-
export type TIndexKey<T extends TSchema, K extends
|
|
177
|
+
export type TIndexKey<T extends TSchema, K extends Key> = T extends TRecursive<infer S> ? TIndexKey<S, K> : T extends TIntersect<infer S> ? TIndexComposite<S, K> : T extends TUnion<infer S> ? TIndexComposite<S, K> : T extends TObject<infer S> ? TIndexProperty<S, K> : T extends TTuple<infer S> ? TIndexTuple<S, K> : T extends TArray<infer S> ? S : [
|
|
179
178
|
];
|
|
180
|
-
export type TIndexKeys<T extends TSchema, K extends
|
|
179
|
+
export type TIndexKeys<T extends TSchema, K extends Key[]> = K extends [infer L, ...infer R] ? [
|
|
180
|
+
...TIndexKey<T, Assert<L, Key>>,
|
|
181
|
+
...TIndexKeys<T, Assert<R, Key[]>>
|
|
182
|
+
] : [
|
|
181
183
|
];
|
|
182
|
-
export type
|
|
184
|
+
export type TIndexFromKeyTuple<T extends TSchema, K extends Key[]> = TIndexKeys<T, K> extends infer R ? T extends TRecursive<infer S> ? TIndexFromKeyTuple<S, K> : T extends TTuple ? UnionType<AssertRest<R>> : T extends TIntersect ? UnionType<AssertRest<R>> : T extends TUnion ? UnionType<AssertRest<R>> : T extends TObject ? UnionType<AssertRest<R>> : T extends TArray ? UnionType<AssertRest<R>> : TNever : TNever;
|
|
185
|
+
export type TIndex<T extends TSchema, K extends TSchema> = [
|
|
186
|
+
T,
|
|
187
|
+
K
|
|
188
|
+
] extends [TTuple, TNumber] ? UnionType<Assert<T['items'], TSchema[]>> : [
|
|
189
|
+
T,
|
|
190
|
+
K
|
|
191
|
+
] extends [TArray, TNumber] ? AssertType<T['items']> : K extends TTemplateLiteral ? TIndexFromKeyTuple<T, TTemplateLiteralKeyTuple<K>> : K extends TUnion<TLiteral<Key>[]> ? TIndexFromKeyTuple<T, TUnionLiteral<K>> : K extends TLiteral<Key> ? TIndexFromKeyTuple<T, [K['const']]> : TNever;
|
|
183
192
|
export interface TInteger extends TSchema, NumericOptions<number> {
|
|
184
193
|
[Kind]: 'Integer';
|
|
185
194
|
static: number;
|
|
@@ -299,7 +308,7 @@ export type RecordTemplateLiteralObjectType<K extends TTemplateLiteral, T extend
|
|
|
299
308
|
[_ in Static<K>]: T;
|
|
300
309
|
}>>>;
|
|
301
310
|
export type RecordTemplateLiteralType<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends true ? RecordTemplateLiteralObjectType<K, T> : TRecord<K, T>;
|
|
302
|
-
export type RecordUnionLiteralType<K extends TUnion
|
|
311
|
+
export type RecordUnionLiteralType<K extends TUnion, T extends TSchema> = Static<K> extends string ? Ensure<TObject<{
|
|
303
312
|
[X in Static<K>]: T;
|
|
304
313
|
}>> : never;
|
|
305
314
|
export type RecordLiteralType<K extends TLiteral<string | number>, T extends TSchema> = Ensure<TObject<{
|
|
@@ -370,7 +379,7 @@ export type TTemplateLiteralConst<T, Acc extends string> = T extends TUnion<infe
|
|
|
370
379
|
[K in keyof U]: TTemplateLiteralUnion<Assert<[U[K]], TTemplateLiteralKind[]>, Acc>;
|
|
371
380
|
}[number] : T extends TTemplateLiteral ? `${Static<T>}` : T extends TLiteral<infer U> ? `${U}` : T extends TString ? `${string}` : T extends TNumber ? `${number}` : T extends TBigInt ? `${bigint}` : T extends TBoolean ? `${boolean}` : never;
|
|
372
381
|
export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string = ''> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : Acc;
|
|
373
|
-
export type
|
|
382
|
+
export type TTemplateLiteralKeyTuple<T extends TTemplateLiteral> = Assert<UnionToTuple<Static<T>>, Key[]>;
|
|
374
383
|
export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema {
|
|
375
384
|
[Kind]: 'TemplateLiteral';
|
|
376
385
|
static: TTemplateLiteralUnion<T>;
|
|
@@ -378,11 +387,10 @@ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLi
|
|
|
378
387
|
pattern: string;
|
|
379
388
|
}
|
|
380
389
|
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? AssertRest<R> : never;
|
|
390
|
+
export type TTupleInfer<T extends TSchema[], P extends unknown[]> = T extends [infer L, ...infer R] ? [Static<AssertType<L>, P>, ...TTupleInfer<AssertRest<R>, P>] : [];
|
|
381
391
|
export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
382
392
|
[Kind]: 'Tuple';
|
|
383
|
-
static:
|
|
384
|
-
[K in keyof T]: T[K] extends TSchema ? Static<T[K], this['params']> : T[K];
|
|
385
|
-
};
|
|
393
|
+
static: TTupleInfer<T, this['params']>;
|
|
386
394
|
type: 'array';
|
|
387
395
|
items?: T;
|
|
388
396
|
additionalItems?: false;
|
|
@@ -397,7 +405,8 @@ export interface TUndefined extends TSchema {
|
|
|
397
405
|
}
|
|
398
406
|
export type TLiteralUnionReduce<T extends TLiteral<string | number>[]> = T extends [infer L, ...infer R] ? [Assert<L, TLiteral<string | number>>['const'], ...TLiteralUnionReduce<Assert<R, TLiteral<string | number>[]>>] : [
|
|
399
407
|
];
|
|
400
|
-
export type
|
|
408
|
+
export type TUnionLiteral<T extends TUnion<TLiteral<string | number>[]>> = T extends TUnion<infer S> ? TLiteralUnionReduce<Assert<S, TLiteral<string | number>[]>> : [
|
|
409
|
+
];
|
|
401
410
|
export type TUnionTemplateLiteral<T extends TTemplateLiteral, S extends string = Static<T>> = Ensure<UnionType<Assert<UnionToTuple<{
|
|
402
411
|
[K in S]: TLiteral<K>;
|
|
403
412
|
}[S]>, TLiteral[]>>>;
|
|
@@ -499,8 +508,6 @@ export declare namespace TypeGuard {
|
|
|
499
508
|
function TLiteralNumber(schema: unknown): schema is TLiteral<number>;
|
|
500
509
|
/** Returns true if the given schema is TLiteral<boolean> */
|
|
501
510
|
function TLiteralBoolean(schema: unknown): schema is TLiteral<boolean>;
|
|
502
|
-
/** Returns true if the given schema is TUnion<Literal<string>[]> */
|
|
503
|
-
function TLiteralUnion(schema: unknown): schema is TUnion<TLiteral<string>[]>;
|
|
504
511
|
/** Returns true if the given schema is TLiteral */
|
|
505
512
|
function TLiteral(schema: unknown): schema is TLiteral;
|
|
506
513
|
/** Returns true if the given schema is TNever */
|
|
@@ -531,6 +538,8 @@ export declare namespace TypeGuard {
|
|
|
531
538
|
function TTuple(schema: unknown): schema is TTuple;
|
|
532
539
|
/** Returns true if the given schema is TUndefined */
|
|
533
540
|
function TUndefined(schema: unknown): schema is TUndefined;
|
|
541
|
+
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
542
|
+
function TUnionLiteral(schema: unknown): schema is TUnion<TLiteral[]>;
|
|
534
543
|
/** Returns true if the given schema is TUnion */
|
|
535
544
|
function TUnion(schema: unknown): schema is TUnion;
|
|
536
545
|
/** Returns true if the given schema is TUint8Array */
|
|
@@ -568,7 +577,7 @@ export declare namespace TypeClone {
|
|
|
568
577
|
function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T;
|
|
569
578
|
}
|
|
570
579
|
export declare namespace IndexedAccessor {
|
|
571
|
-
function Resolve(schema: TSchema, keys: string[]): TSchema[];
|
|
580
|
+
function Resolve(schema: TSchema, keys: (string | number)[]): TSchema[];
|
|
572
581
|
}
|
|
573
582
|
export declare namespace ObjectMap {
|
|
574
583
|
function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T;
|
|
@@ -586,6 +595,10 @@ export declare namespace KeyArrayResolver {
|
|
|
586
595
|
/** Resolves an array of string[] keys from the given schema or array type. */
|
|
587
596
|
function Resolve(schema: TSchema | string[]): string[];
|
|
588
597
|
}
|
|
598
|
+
export declare namespace UnionResolver {
|
|
599
|
+
/** Returns a resolved union with interior unions flattened */
|
|
600
|
+
function Resolve(union: TUnion): TUnion;
|
|
601
|
+
}
|
|
589
602
|
export declare namespace TemplateLiteralPattern {
|
|
590
603
|
function Create(kinds: TTemplateLiteralKind[]): string;
|
|
591
604
|
}
|
|
@@ -651,19 +664,9 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
651
664
|
/** `[Standard]` Extracts from the left type any type that is assignable to the right */
|
|
652
665
|
Extract<L extends TSchema, R extends TSchema>(left: L, right: R, options?: SchemaOptions): TExtract<L, R>;
|
|
653
666
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
654
|
-
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions):
|
|
655
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
656
|
-
Index<T extends TSchema, K extends TUnion<TLiteral<string | number>[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TLiteralUnion<K>>;
|
|
657
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
658
|
-
Index<T extends TSchema, K extends TLiteral<string | number>>(schema: T, key: K, options?: SchemaOptions): TIndex<T, [K['const']]>;
|
|
659
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
660
|
-
Index<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TIndex<T, TTemplateLiteralKeyArray<K>>;
|
|
661
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
662
|
-
Index<T extends TTuple, K extends TNumber>(schema: T, key: K, options?: SchemaOptions): UnionType<Assert<T['items'], TSchema[]>>;
|
|
663
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
664
|
-
Index<T extends TArray, K extends TNumber>(schema: T, key: K, options?: SchemaOptions): AssertType<T['items']>;
|
|
667
|
+
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndexFromKeyTuple<T, Assert<K, Key[]>>;
|
|
665
668
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
666
|
-
Index<T extends TSchema, K extends
|
|
669
|
+
Index<T extends TSchema, K extends TSchema>(schema: T, key: K, options?: SchemaOptions): TIndex<T, K>;
|
|
667
670
|
/** `[Standard]` Creates an Integer type */
|
|
668
671
|
Integer(options?: NumericOptions<number>): TInteger;
|
|
669
672
|
/** `[Standard]` Creates a Intersect type */
|
|
@@ -688,11 +691,11 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
688
691
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
689
692
|
Omit<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TOmit<T, K[number]>;
|
|
690
693
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
691
|
-
Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T,
|
|
694
|
+
Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T, TUnionLiteral<K>[number]>;
|
|
692
695
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
693
696
|
Omit<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TOmit<T, K['const']>;
|
|
694
697
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
695
|
-
Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T,
|
|
698
|
+
Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T, TTemplateLiteralKeyTuple<K>[number]>;
|
|
696
699
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
697
700
|
Omit<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TOmit<T, never>;
|
|
698
701
|
/** `[Standard]` Creates a mapped type where all properties are Optional */
|
|
@@ -700,15 +703,15 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
700
703
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
701
704
|
Pick<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TPick<T, K[number]>;
|
|
702
705
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
703
|
-
Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T,
|
|
706
|
+
Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T, TUnionLiteral<K>[number]>;
|
|
704
707
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
705
708
|
Pick<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TPick<T, K['const']>;
|
|
706
709
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
707
|
-
Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T,
|
|
710
|
+
Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T, TTemplateLiteralKeyTuple<K>[number]>;
|
|
708
711
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
709
712
|
Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>;
|
|
710
713
|
/** `[Standard]` Creates a Record type */
|
|
711
|
-
Record<K extends TUnion
|
|
714
|
+
Record<K extends TUnion, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordUnionLiteralType<K, T>;
|
|
712
715
|
/** `[Standard]` Creates a Record type */
|
|
713
716
|
Record<K extends TLiteral<string | number>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordLiteralType<K, T>;
|
|
714
717
|
/** `[Standard]` Creates a Record type */
|
package/typebox.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Modifier = void 0;
|
|
30
|
+
exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Modifier = void 0;
|
|
31
31
|
// --------------------------------------------------------------------------
|
|
32
32
|
// Symbols
|
|
33
33
|
// --------------------------------------------------------------------------
|
|
@@ -324,11 +324,6 @@ var TypeGuard;
|
|
|
324
324
|
return TKind(schema) && schema[exports.Kind] === 'Literal' && IsOptionalString(schema.$id) && typeof schema.const === 'boolean';
|
|
325
325
|
}
|
|
326
326
|
TypeGuard.TLiteralBoolean = TLiteralBoolean;
|
|
327
|
-
/** Returns true if the given schema is TUnion<Literal<string>[]> */
|
|
328
|
-
function TLiteralUnion(schema) {
|
|
329
|
-
return TUnion(schema) && schema.anyOf.every((schema) => TLiteral(schema));
|
|
330
|
-
}
|
|
331
|
-
TypeGuard.TLiteralUnion = TLiteralUnion;
|
|
332
327
|
/** Returns true if the given schema is TLiteral */
|
|
333
328
|
function TLiteral(schema) {
|
|
334
329
|
return TLiteralString(schema) || TLiteralNumber(schema) || TLiteralBoolean(schema);
|
|
@@ -515,6 +510,11 @@ var TypeGuard;
|
|
|
515
510
|
IsOptionalString(schema.$id));
|
|
516
511
|
}
|
|
517
512
|
TypeGuard.TUndefined = TUndefined;
|
|
513
|
+
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
514
|
+
function TUnionLiteral(schema) {
|
|
515
|
+
return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(schema));
|
|
516
|
+
}
|
|
517
|
+
TypeGuard.TUnionLiteral = TUnionLiteral;
|
|
518
518
|
/** Returns true if the given schema is TUnion */
|
|
519
519
|
function TUnion(schema) {
|
|
520
520
|
// prettier-ignore
|
|
@@ -1411,12 +1411,12 @@ var IndexedAccessor;
|
|
|
1411
1411
|
return schema.anyOf.reduce((acc, schema) => [...acc, ...Visit(schema, key)], []);
|
|
1412
1412
|
}
|
|
1413
1413
|
function Object(schema, key) {
|
|
1414
|
-
const keys = globalThis.Object.getOwnPropertyNames(schema.properties).filter((
|
|
1414
|
+
const keys = globalThis.Object.getOwnPropertyNames(schema.properties).filter((key_) => key_ === key);
|
|
1415
1415
|
return keys.map((key) => schema.properties[key]);
|
|
1416
1416
|
}
|
|
1417
1417
|
function Tuple(schema, key) {
|
|
1418
1418
|
const items = schema.items === undefined ? [] : schema.items;
|
|
1419
|
-
return items.filter((_, index) => index.toString() === key
|
|
1419
|
+
return items.filter((_, index) => index.toString() === key);
|
|
1420
1420
|
}
|
|
1421
1421
|
function Visit(schema, key) {
|
|
1422
1422
|
if (schema[exports.Kind] === 'Intersect')
|
|
@@ -1430,7 +1430,7 @@ var IndexedAccessor;
|
|
|
1430
1430
|
return [];
|
|
1431
1431
|
}
|
|
1432
1432
|
function Resolve(schema, keys) {
|
|
1433
|
-
return keys.reduce((acc, key) => [...acc, ...Visit(schema, key)], []);
|
|
1433
|
+
return keys.reduce((acc, key) => [...acc, ...Visit(schema, key.toString())], []);
|
|
1434
1434
|
}
|
|
1435
1435
|
IndexedAccessor.Resolve = Resolve;
|
|
1436
1436
|
})(IndexedAccessor = exports.IndexedAccessor || (exports.IndexedAccessor = {}));
|
|
@@ -1519,8 +1519,8 @@ var KeyArrayResolver;
|
|
|
1519
1519
|
function Resolve(schema) {
|
|
1520
1520
|
if (globalThis.Array.isArray(schema))
|
|
1521
1521
|
return schema;
|
|
1522
|
-
if (TypeGuard.
|
|
1523
|
-
return schema.anyOf.map((schema) => schema.const);
|
|
1522
|
+
if (TypeGuard.TUnionLiteral(schema))
|
|
1523
|
+
return schema.anyOf.map((schema) => schema.const.toString());
|
|
1524
1524
|
if (TypeGuard.TLiteral(schema))
|
|
1525
1525
|
return [schema.const];
|
|
1526
1526
|
if (TypeGuard.TTemplateLiteral(schema)) {
|
|
@@ -1534,6 +1534,27 @@ var KeyArrayResolver;
|
|
|
1534
1534
|
KeyArrayResolver.Resolve = Resolve;
|
|
1535
1535
|
})(KeyArrayResolver = exports.KeyArrayResolver || (exports.KeyArrayResolver = {}));
|
|
1536
1536
|
// --------------------------------------------------------------------------
|
|
1537
|
+
// UnionResolver
|
|
1538
|
+
// --------------------------------------------------------------------------
|
|
1539
|
+
var UnionResolver;
|
|
1540
|
+
(function (UnionResolver) {
|
|
1541
|
+
function* Union(union) {
|
|
1542
|
+
for (const schema of union.anyOf) {
|
|
1543
|
+
if (schema[exports.Kind] === 'Union') {
|
|
1544
|
+
yield* Union(schema);
|
|
1545
|
+
}
|
|
1546
|
+
else {
|
|
1547
|
+
yield schema;
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
/** Returns a resolved union with interior unions flattened */
|
|
1552
|
+
function Resolve(union) {
|
|
1553
|
+
return exports.Type.Union([...Union(union)], { ...union });
|
|
1554
|
+
}
|
|
1555
|
+
UnionResolver.Resolve = Resolve;
|
|
1556
|
+
})(UnionResolver = exports.UnionResolver || (exports.UnionResolver = {}));
|
|
1557
|
+
// --------------------------------------------------------------------------
|
|
1537
1558
|
// TemplateLiteralPattern
|
|
1538
1559
|
// --------------------------------------------------------------------------
|
|
1539
1560
|
var TemplateLiteralPattern;
|
|
@@ -2115,20 +2136,21 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2115
2136
|
? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(schema, {}) }), {}), options))
|
|
2116
2137
|
: this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema, {}) } });
|
|
2117
2138
|
}
|
|
2118
|
-
else if (TypeGuard.
|
|
2119
|
-
|
|
2120
|
-
|
|
2139
|
+
else if (TypeGuard.TUnion(key)) {
|
|
2140
|
+
const union = UnionResolver.Resolve(key);
|
|
2141
|
+
if (TypeGuard.TUnionLiteral(union)) {
|
|
2142
|
+
const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
|
|
2121
2143
|
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
2122
2144
|
}
|
|
2123
2145
|
else
|
|
2124
|
-
throw Error('TypeBuilder: Record key
|
|
2146
|
+
throw Error('TypeBuilder: Record key of type union contains non-literal types');
|
|
2125
2147
|
}
|
|
2126
2148
|
else if (TypeGuard.TLiteral(key)) {
|
|
2127
2149
|
if (typeof key.const === 'string' || typeof key.const === 'number') {
|
|
2128
2150
|
return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options);
|
|
2129
2151
|
}
|
|
2130
2152
|
else
|
|
2131
|
-
throw Error('TypeBuilder: Record key
|
|
2153
|
+
throw Error('TypeBuilder: Record key of type literal is not of type string or number');
|
|
2132
2154
|
}
|
|
2133
2155
|
else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
|
|
2134
2156
|
const pattern = exports.PatternNumberExact;
|
|
@@ -2139,7 +2161,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2139
2161
|
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } });
|
|
2140
2162
|
}
|
|
2141
2163
|
else {
|
|
2142
|
-
throw Error(`StandardTypeBuilder:
|
|
2164
|
+
throw Error(`StandardTypeBuilder: Record key is an invalid type`);
|
|
2143
2165
|
}
|
|
2144
2166
|
}
|
|
2145
2167
|
/** `[Standard]` Creates a Recursive type */
|