@sinclair/typebox 0.28.4 → 0.28.6
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 +36 -15
- package/typebox.d.ts +30 -37
- package/typebox.js +32 -21
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
|
|
12
12
|
[](https://badge.fury.io/js/%40sinclair%2Ftypebox)
|
|
13
13
|
[](https://www.npmjs.com/package/%40sinclair%2Ftypebox)
|
|
14
|
+
[](https://packagephobia.com/result?p=@sinclair/typebox)
|
|
15
|
+
[](https://opensource.org/licenses/MIT)
|
|
14
16
|
[](https://github.com/sinclairzx81/typebox/actions)
|
|
15
17
|
|
|
16
18
|
</div>
|
|
@@ -83,7 +85,7 @@ License MIT
|
|
|
83
85
|
- [Conditional](#types-conditional)
|
|
84
86
|
- [Template](#types-template-literal)
|
|
85
87
|
- [Indexed](#types-indexed)
|
|
86
|
-
- [
|
|
88
|
+
- [Rest](#types-rest)
|
|
87
89
|
- [Guards](#types-guards)
|
|
88
90
|
- [Unsafe](#types-unsafe)
|
|
89
91
|
- [Strict](#types-strict)
|
|
@@ -872,30 +874,43 @@ const A = Type.Index(T, ['x']) // type A = T['x']
|
|
|
872
874
|
|
|
873
875
|
const B = Type.Index(T, Type.KeyOf(T)) // type B = T[keyof T]
|
|
874
876
|
```
|
|
875
|
-
<a name='types-
|
|
877
|
+
<a name='types-rest'></a>
|
|
876
878
|
|
|
877
|
-
###
|
|
879
|
+
### Rest Types
|
|
878
880
|
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
The following creates variadic functions using the Rest type.
|
|
881
|
+
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
882
|
|
|
883
883
|
```typescript
|
|
884
884
|
// TypeScript
|
|
885
885
|
|
|
886
|
-
type
|
|
886
|
+
type T = [number, number] // type T = [number, number]
|
|
887
887
|
|
|
888
|
-
type
|
|
888
|
+
type C = [...T, number] // type C = [number, number, number]
|
|
889
889
|
|
|
890
|
-
type
|
|
890
|
+
type F = (...param: C) => void // type F = (
|
|
891
|
+
// param0: number,
|
|
892
|
+
// param1: number,
|
|
893
|
+
// param2: number,
|
|
894
|
+
// ) => void
|
|
891
895
|
|
|
892
896
|
// TypeBox
|
|
893
897
|
|
|
894
|
-
const
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
898
|
+
const T = Type.Tuple([ // const T: TTuple<[
|
|
899
|
+
Type.Number(), // TNumber,
|
|
900
|
+
Type.Number() // TNumber,
|
|
901
|
+
]) // ]>
|
|
902
|
+
|
|
903
|
+
const C = Type.Tuple([ // const C: TTuple<[
|
|
904
|
+
...Type.Rest(T), // TNumber,
|
|
905
|
+
Type.Number() // TNumber,
|
|
906
|
+
]) // TNumber
|
|
907
|
+
// ]>
|
|
908
|
+
|
|
909
|
+
const F = Type.Function(Type.Rest(C), Type.Void()) // const F: TFunction<[
|
|
910
|
+
// TNumber,
|
|
911
|
+
// TNumber,
|
|
912
|
+
// TNumber
|
|
913
|
+
// ], TVoid>
|
|
899
914
|
```
|
|
900
915
|
<a name='types-unsafe'></a>
|
|
901
916
|
|
|
@@ -1356,9 +1371,15 @@ const B = Value.Check(T, 'hello') // const B = true
|
|
|
1356
1371
|
|
|
1357
1372
|
### Policies
|
|
1358
1373
|
|
|
1359
|
-
TypeBox validates using JSON Schema assertion policies by default. It is possible to override these policies
|
|
1374
|
+
TypeBox validates using standard JSON Schema assertion policies by default. It is possible to override some of these policies to have TypeBox assert inline with TypeScript static assertion rules. The following policy overrides are available.
|
|
1360
1375
|
|
|
1361
1376
|
```typescript
|
|
1377
|
+
// Disallow undefined values for optional properties (default is false)
|
|
1378
|
+
//
|
|
1379
|
+
// const A: { x?: number } = { x: undefined } - disallowed when enabled
|
|
1380
|
+
|
|
1381
|
+
TypeSystem.ExactOptionalPropertyTypes = true
|
|
1382
|
+
|
|
1362
1383
|
// Allow arrays to validate as object types (default is false)
|
|
1363
1384
|
//
|
|
1364
1385
|
// const A: {} = [] - allowed in TS
|
package/typebox.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ export type TupleToUnion<T extends any[]> = {
|
|
|
14
14
|
export type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never;
|
|
15
15
|
export type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never;
|
|
16
16
|
export type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L];
|
|
17
|
+
export type Discard<T extends unknown[], D extends unknown> = T extends [infer L, ...infer R] ? (L extends D ? Discard<R, D> : [L, ...Discard<R, D>]) : [];
|
|
18
|
+
export type Flat<T> = T extends [] ? [] : T extends [infer L] ? [...Flat<L>] : T extends [infer L, ...infer R] ? [...Flat<L>, ...Flat<R>] : [T];
|
|
17
19
|
export type Assert<T, E> = T extends E ? T : never;
|
|
18
20
|
export type Evaluate<T> = T extends infer O ? {
|
|
19
21
|
[K in keyof O]: O[K];
|
|
@@ -168,20 +170,21 @@ export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends T
|
|
|
168
170
|
parameters: T;
|
|
169
171
|
returns: U;
|
|
170
172
|
}
|
|
171
|
-
export type
|
|
173
|
+
export type TIndexRest<T extends TSchema[], K extends Key> = T extends [infer L, ...infer R] ? [TIndexType<AssertType<L>, K>, ...TIndexRest<AssertRest<R>, K>] : [];
|
|
174
|
+
export type TIndexProperty<T extends TProperties, K extends Key> = K extends keyof T ? [T[K]] : [];
|
|
175
|
+
export type TIndexTuple<T extends TSchema[], K extends Key> = K extends keyof T ? [T[K]] : [];
|
|
176
|
+
export type TIndexType<T extends TSchema, K extends Key> = T extends TRecursive<infer S> ? TIndexType<S, K> : T extends TIntersect<infer S> ? IntersectType<AssertRest<Discard<Flat<TIndexRest<S, K>>, TNever>>> : T extends TUnion<infer S> ? UnionType<AssertRest<Flat<TIndexRest<S, K>>>> : T extends TObject<infer S> ? UnionType<AssertRest<Flat<TIndexProperty<S, K>>>> : T extends TTuple<infer S> ? UnionType<AssertRest<Flat<TIndexTuple<S, K>>>> : [
|
|
172
177
|
];
|
|
173
|
-
export type
|
|
178
|
+
export type TIndexRestMany<T extends TSchema, K extends Key[]> = K extends [infer L, ...infer R] ? [TIndexType<T, Assert<L, Key>>, ...TIndexRestMany<T, Assert<R, Key[]>>] : [
|
|
174
179
|
];
|
|
175
|
-
export type
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
] : [
|
|
183
|
-
];
|
|
184
|
-
export type TIndex<T extends TSchema, K extends Key[]> = TIndexKeys<T, K> extends infer R ? T extends TRecursive<infer S> ? TIndex<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;
|
|
180
|
+
export type TIndexReduce<T extends TSchema, K extends Key[]> = T extends TRecursive<infer S> ? TIndexReduce<S, K> : T extends TIntersect ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TUnion ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TObject ? UnionType<Flat<TIndexRestMany<T, K>>> : T extends TTuple ? UnionType<Flat<TIndexRestMany<T, K>>> : TNever;
|
|
181
|
+
export type TIndex<T extends TSchema, K extends TSchema> = [
|
|
182
|
+
T,
|
|
183
|
+
K
|
|
184
|
+
] extends [TTuple, TNumber] ? UnionType<Assert<T['items'], TSchema[]>> : [
|
|
185
|
+
T,
|
|
186
|
+
K
|
|
187
|
+
] extends [TArray, TNumber] ? AssertType<T['items']> : K extends TTemplateLiteral ? TIndexReduce<T, TTemplateLiteralKeyRest<K>> : K extends TUnion<TLiteral<Key>[]> ? TIndexReduce<T, TUnionLiteralKeyRest<K>> : K extends TLiteral<Key> ? TIndexReduce<T, [K['const']]> : TNever;
|
|
185
188
|
export interface TInteger extends TSchema, NumericOptions<number> {
|
|
186
189
|
[Kind]: 'Integer';
|
|
187
190
|
static: number;
|
|
@@ -372,7 +375,7 @@ export type TTemplateLiteralConst<T, Acc extends string> = T extends TUnion<infe
|
|
|
372
375
|
[K in keyof U]: TTemplateLiteralUnion<Assert<[U[K]], TTemplateLiteralKind[]>, Acc>;
|
|
373
376
|
}[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;
|
|
374
377
|
export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string = ''> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : Acc;
|
|
375
|
-
export type
|
|
378
|
+
export type TTemplateLiteralKeyRest<T extends TTemplateLiteral> = Assert<UnionToTuple<Static<T>>, Key[]>;
|
|
376
379
|
export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema {
|
|
377
380
|
[Kind]: 'TemplateLiteral';
|
|
378
381
|
static: TTemplateLiteralUnion<T>;
|
|
@@ -380,11 +383,10 @@ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLi
|
|
|
380
383
|
pattern: string;
|
|
381
384
|
}
|
|
382
385
|
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? AssertRest<R> : never;
|
|
386
|
+
export type TTupleInfer<T extends TSchema[], P extends unknown[]> = T extends [infer L, ...infer R] ? [Static<AssertType<L>, P>, ...TTupleInfer<AssertRest<R>, P>] : [];
|
|
383
387
|
export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
384
388
|
[Kind]: 'Tuple';
|
|
385
|
-
static:
|
|
386
|
-
[K in keyof T]: T[K] extends TSchema ? Static<T[K], this['params']> : T[K];
|
|
387
|
-
};
|
|
389
|
+
static: TTupleInfer<T, this['params']>;
|
|
388
390
|
type: 'array';
|
|
389
391
|
items?: T;
|
|
390
392
|
additionalItems?: false;
|
|
@@ -399,7 +401,8 @@ export interface TUndefined extends TSchema {
|
|
|
399
401
|
}
|
|
400
402
|
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>[]>>] : [
|
|
401
403
|
];
|
|
402
|
-
export type
|
|
404
|
+
export type TUnionLiteralKeyRest<T extends TUnion<TLiteral<string | number>[]>> = T extends TUnion<infer S> ? TLiteralUnionReduce<Assert<S, TLiteral<string | number>[]>> : [
|
|
405
|
+
];
|
|
403
406
|
export type TUnionTemplateLiteral<T extends TTemplateLiteral, S extends string = Static<T>> = Ensure<UnionType<Assert<UnionToTuple<{
|
|
404
407
|
[K in S]: TLiteral<K>;
|
|
405
408
|
}[S]>, TLiteral[]>>>;
|
|
@@ -437,7 +440,7 @@ export interface TVoid extends TSchema {
|
|
|
437
440
|
type: 'null';
|
|
438
441
|
typeOf: 'Void';
|
|
439
442
|
}
|
|
440
|
-
/**
|
|
443
|
+
/** Infers a static type from a TypeBox type */
|
|
441
444
|
export type Static<T extends TSchema, P extends unknown[] = []> = (T & {
|
|
442
445
|
params: P;
|
|
443
446
|
})['static'];
|
|
@@ -501,8 +504,6 @@ export declare namespace TypeGuard {
|
|
|
501
504
|
function TLiteralNumber(schema: unknown): schema is TLiteral<number>;
|
|
502
505
|
/** Returns true if the given schema is TLiteral<boolean> */
|
|
503
506
|
function TLiteralBoolean(schema: unknown): schema is TLiteral<boolean>;
|
|
504
|
-
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
505
|
-
function TLiteralUnion(schema: unknown): schema is TUnion<TLiteral[]>;
|
|
506
507
|
/** Returns true if the given schema is TLiteral */
|
|
507
508
|
function TLiteral(schema: unknown): schema is TLiteral;
|
|
508
509
|
/** Returns true if the given schema is TNever */
|
|
@@ -533,6 +534,8 @@ export declare namespace TypeGuard {
|
|
|
533
534
|
function TTuple(schema: unknown): schema is TTuple;
|
|
534
535
|
/** Returns true if the given schema is TUndefined */
|
|
535
536
|
function TUndefined(schema: unknown): schema is TUndefined;
|
|
537
|
+
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
538
|
+
function TUnionLiteral(schema: unknown): schema is TUnion<TLiteral[]>;
|
|
536
539
|
/** Returns true if the given schema is TUnion */
|
|
537
540
|
function TUnion(schema: unknown): schema is TUnion;
|
|
538
541
|
/** Returns true if the given schema is TUint8Array */
|
|
@@ -570,7 +573,7 @@ export declare namespace TypeClone {
|
|
|
570
573
|
function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T;
|
|
571
574
|
}
|
|
572
575
|
export declare namespace IndexedAccessor {
|
|
573
|
-
function Resolve(schema: TSchema, keys:
|
|
576
|
+
function Resolve(schema: TSchema, keys: Key[], options?: SchemaOptions): TSchema;
|
|
574
577
|
}
|
|
575
578
|
export declare namespace ObjectMap {
|
|
576
579
|
function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T;
|
|
@@ -657,19 +660,9 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
657
660
|
/** `[Standard]` Extracts from the left type any type that is assignable to the right */
|
|
658
661
|
Extract<L extends TSchema, R extends TSchema>(left: L, right: R, options?: SchemaOptions): TExtract<L, R>;
|
|
659
662
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
660
|
-
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions):
|
|
661
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
662
|
-
Index<T extends TSchema, K extends TUnion<TLiteral<Key>[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TLiteralUnion<K>>;
|
|
663
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
664
|
-
Index<T extends TSchema, K extends TLiteral<Key>>(schema: T, key: K, options?: SchemaOptions): TIndex<T, [K['const']]>;
|
|
665
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
666
|
-
Index<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TIndex<T, TTemplateLiteralKeyArray<K>>;
|
|
667
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
668
|
-
Index<T extends TTuple, K extends TNumber>(schema: T, key: K, options?: SchemaOptions): UnionType<Assert<T['items'], TSchema[]>>;
|
|
669
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
670
|
-
Index<T extends TArray, K extends TNumber>(schema: T, key: K, options?: SchemaOptions): AssertType<T['items']>;
|
|
663
|
+
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndexReduce<T, Assert<K, Key[]>>;
|
|
671
664
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
672
|
-
Index<T extends TSchema, K extends
|
|
665
|
+
Index<T extends TSchema, K extends TSchema>(schema: T, key: K, options?: SchemaOptions): TIndex<T, K>;
|
|
673
666
|
/** `[Standard]` Creates an Integer type */
|
|
674
667
|
Integer(options?: NumericOptions<number>): TInteger;
|
|
675
668
|
/** `[Standard]` Creates a Intersect type */
|
|
@@ -694,11 +687,11 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
694
687
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
695
688
|
Omit<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TOmit<T, K[number]>;
|
|
696
689
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
697
|
-
Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T,
|
|
690
|
+
Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T, TUnionLiteralKeyRest<K>[number]>;
|
|
698
691
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
699
692
|
Omit<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TOmit<T, K['const']>;
|
|
700
693
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
701
|
-
Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T,
|
|
694
|
+
Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T, TTemplateLiteralKeyRest<K>[number]>;
|
|
702
695
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
703
696
|
Omit<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TOmit<T, never>;
|
|
704
697
|
/** `[Standard]` Creates a mapped type where all properties are Optional */
|
|
@@ -706,11 +699,11 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
706
699
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
707
700
|
Pick<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TPick<T, K[number]>;
|
|
708
701
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
709
|
-
Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T,
|
|
702
|
+
Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T, TUnionLiteralKeyRest<K>[number]>;
|
|
710
703
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
711
704
|
Pick<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TPick<T, K['const']>;
|
|
712
705
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
713
|
-
Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T,
|
|
706
|
+
Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T, TTemplateLiteralKeyRest<K>[number]>;
|
|
714
707
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
715
708
|
Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>;
|
|
716
709
|
/** `[Standard]` Creates a Record type */
|
package/typebox.js
CHANGED
|
@@ -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 | number>[]> */
|
|
328
|
-
function TLiteralUnion(schema) {
|
|
329
|
-
return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(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
|
|
@@ -1405,18 +1405,28 @@ var TypeClone;
|
|
|
1405
1405
|
var IndexedAccessor;
|
|
1406
1406
|
(function (IndexedAccessor) {
|
|
1407
1407
|
function Intersect(schema, key) {
|
|
1408
|
-
|
|
1408
|
+
const schemas = schema.allOf.reduce((acc, schema) => {
|
|
1409
|
+
const indexed = Visit(schema, key);
|
|
1410
|
+
return indexed[exports.Kind] === 'Never' ? acc : [...acc, indexed];
|
|
1411
|
+
}, []);
|
|
1412
|
+
return exports.Type.Intersect(schemas);
|
|
1409
1413
|
}
|
|
1410
1414
|
function Union(schema, key) {
|
|
1411
|
-
|
|
1415
|
+
const schemas = schema.anyOf.map((schema) => Visit(schema, key));
|
|
1416
|
+
return exports.Type.Union(schemas);
|
|
1412
1417
|
}
|
|
1413
1418
|
function Object(schema, key) {
|
|
1414
|
-
const
|
|
1415
|
-
return
|
|
1419
|
+
const property = schema.properties[key];
|
|
1420
|
+
return property === undefined ? exports.Type.Never() : exports.Type.Union([property]);
|
|
1416
1421
|
}
|
|
1417
1422
|
function Tuple(schema, key) {
|
|
1418
|
-
const items = schema.items
|
|
1419
|
-
|
|
1423
|
+
const items = schema.items;
|
|
1424
|
+
if (items === undefined)
|
|
1425
|
+
return exports.Type.Never();
|
|
1426
|
+
const element = items[key]; //
|
|
1427
|
+
if (element === undefined)
|
|
1428
|
+
return exports.Type.Never();
|
|
1429
|
+
return element;
|
|
1420
1430
|
}
|
|
1421
1431
|
function Visit(schema, key) {
|
|
1422
1432
|
if (schema[exports.Kind] === 'Intersect')
|
|
@@ -1427,10 +1437,10 @@ var IndexedAccessor;
|
|
|
1427
1437
|
return Object(schema, key);
|
|
1428
1438
|
if (schema[exports.Kind] === 'Tuple')
|
|
1429
1439
|
return Tuple(schema, key);
|
|
1430
|
-
return
|
|
1440
|
+
return exports.Type.Never();
|
|
1431
1441
|
}
|
|
1432
|
-
function Resolve(schema, keys) {
|
|
1433
|
-
return keys.
|
|
1442
|
+
function Resolve(schema, keys, options = {}) {
|
|
1443
|
+
return exports.Type.Union(keys.map((key) => Visit(schema, key.toString())));
|
|
1434
1444
|
}
|
|
1435
1445
|
IndexedAccessor.Resolve = Resolve;
|
|
1436
1446
|
})(IndexedAccessor = exports.IndexedAccessor || (exports.IndexedAccessor = {}));
|
|
@@ -1519,7 +1529,7 @@ var KeyArrayResolver;
|
|
|
1519
1529
|
function Resolve(schema) {
|
|
1520
1530
|
if (globalThis.Array.isArray(schema))
|
|
1521
1531
|
return schema;
|
|
1522
|
-
if (TypeGuard.
|
|
1532
|
+
if (TypeGuard.TUnionLiteral(schema))
|
|
1523
1533
|
return schema.anyOf.map((schema) => schema.const.toString());
|
|
1524
1534
|
if (TypeGuard.TLiteral(schema))
|
|
1525
1535
|
return [schema.const];
|
|
@@ -1977,18 +1987,19 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1977
1987
|
}
|
|
1978
1988
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
1979
1989
|
Index(schema, unresolved, options = {}) {
|
|
1980
|
-
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
1981
1990
|
if (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1982
1991
|
return TypeClone.Clone(schema.items, options);
|
|
1983
1992
|
}
|
|
1984
|
-
if (TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1993
|
+
else if (TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1985
1994
|
const items = schema.items === undefined ? [] : schema.items;
|
|
1986
1995
|
const cloned = items.map((schema) => TypeClone.Clone(schema, {}));
|
|
1987
1996
|
return this.Union(cloned, options);
|
|
1988
1997
|
}
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1998
|
+
else {
|
|
1999
|
+
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2000
|
+
const clone = TypeClone.Clone(schema, {});
|
|
2001
|
+
return IndexedAccessor.Resolve(clone, keys, options);
|
|
2002
|
+
}
|
|
1992
2003
|
}
|
|
1993
2004
|
/** `[Standard]` Creates an Integer type */
|
|
1994
2005
|
Integer(options = {}) {
|
|
@@ -2138,7 +2149,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2138
2149
|
}
|
|
2139
2150
|
else if (TypeGuard.TUnion(key)) {
|
|
2140
2151
|
const union = UnionResolver.Resolve(key);
|
|
2141
|
-
if (TypeGuard.
|
|
2152
|
+
if (TypeGuard.TUnionLiteral(union)) {
|
|
2142
2153
|
const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
|
|
2143
2154
|
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
2144
2155
|
}
|