@sinclair/typebox 0.28.2 → 0.28.4
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 +56 -16
- package/typebox.d.ts +65 -62
- package/typebox.js +59 -37
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -83,6 +83,7 @@ License MIT
|
|
|
83
83
|
- [Conditional](#types-conditional)
|
|
84
84
|
- [Template](#types-template-literal)
|
|
85
85
|
- [Indexed](#types-indexed)
|
|
86
|
+
- [Variadic](#types-variadic)
|
|
86
87
|
- [Guards](#types-guards)
|
|
87
88
|
- [Unsafe](#types-unsafe)
|
|
88
89
|
- [Strict](#types-strict)
|
|
@@ -256,7 +257,8 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
256
257
|
│ }) │ } │ properties: { │
|
|
257
258
|
│ │ │ x: { │
|
|
258
259
|
│ │ │ type: 'number' │
|
|
259
|
-
│ │ │ },
|
|
260
|
+
│ │ │ }, │
|
|
261
|
+
│ │ │ y: { │
|
|
260
262
|
│ │ │ type: 'number' │
|
|
261
263
|
│ │ │ } │
|
|
262
264
|
│ │ │ } │
|
|
@@ -277,14 +279,6 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
277
279
|
│ │ │ │
|
|
278
280
|
│ │ │ │
|
|
279
281
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
280
|
-
│ const T = Type.Index( │ type T = { │ const T = { │
|
|
281
|
-
│ Type.Object({ │ x: number, │ type: number │
|
|
282
|
-
│ x: Type.Number(), │ y: string │ } │
|
|
283
|
-
│ y: Type.String() │ }['x'] │ │
|
|
284
|
-
│ }), ['x'] │ │ │
|
|
285
|
-
│ ) │ │ │
|
|
286
|
-
│ │ │ │
|
|
287
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
288
282
|
│ enum Foo { │ enum Foo { │ const T = { │
|
|
289
283
|
│ A, │ A, │ anyOf: [{ │
|
|
290
284
|
│ B │ B │ type: 'number', │
|
|
@@ -468,6 +462,28 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
468
462
|
│ │ │ } │
|
|
469
463
|
│ │ │ │
|
|
470
464
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
465
|
+
│ const T = Type.Index( │ type T = { │ const T = { │
|
|
466
|
+
│ Type.Object({ │ x: number, │ type: 'number' │
|
|
467
|
+
│ x: Type.Number(), │ y: string │ } │
|
|
468
|
+
│ y: Type.String() │ }['x'] │ │
|
|
469
|
+
│ }), ['x'] │ │ │
|
|
470
|
+
│ ) │ │ │
|
|
471
|
+
│ │ │ │
|
|
472
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
473
|
+
│ const A = Type.Tuple([ │ type A = [0, 1] │ const T = { │
|
|
474
|
+
│ Type.Literal(0), │ type B = [2, 3] │ type: 'array', │
|
|
475
|
+
│ Type.Literal(1) │ type T = [...A, ...B] │ items: [ │
|
|
476
|
+
│ ]) │ │ { const: 0 }, │
|
|
477
|
+
│ const B = Type.Tuple([ │ │ { const: 1 }, │
|
|
478
|
+
| Type.Literal(2), │ │ { const: 2 }, │
|
|
479
|
+
| Type.Literal(3) │ │ { const: 3 } │
|
|
480
|
+
│ ]) │ │ ], │
|
|
481
|
+
│ const T = Type.Tuple([ │ │ additionalItems: false, │
|
|
482
|
+
| ...Type.Rest(A), │ │ minItems: 4, │
|
|
483
|
+
| ...Type.Test(B) │ │ maxItems: 4 │
|
|
484
|
+
│ ]) │ │ } │
|
|
485
|
+
│ │ │ │
|
|
486
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
471
487
|
│ const T = Type.Object({ │ type T = { │ const R = { │
|
|
472
488
|
│ x: Type.Number(), │ x: number, │ $ref: 'T' │
|
|
473
489
|
│ y: Type.Number() │ y: number │ } │
|
|
@@ -856,10 +872,34 @@ const A = Type.Index(T, ['x']) // type A = T['x']
|
|
|
856
872
|
|
|
857
873
|
const B = Type.Index(T, Type.KeyOf(T)) // type B = T[keyof T]
|
|
858
874
|
```
|
|
875
|
+
<a name='types-variadic'></a>
|
|
876
|
+
|
|
877
|
+
### Variadic Types
|
|
878
|
+
|
|
879
|
+
Variadic types are supported with `Type.Rest`. This type will extract interior types from a tuple and return them as a flat array. This array can then be passed to other types that accept arrays as arguments.
|
|
880
|
+
|
|
881
|
+
The following creates variadic functions using the Rest type.
|
|
882
|
+
|
|
883
|
+
```typescript
|
|
884
|
+
// TypeScript
|
|
859
885
|
|
|
886
|
+
type P = [number, number]
|
|
887
|
+
|
|
888
|
+
type F1 = (param: [...P]) => void
|
|
889
|
+
|
|
890
|
+
type F2 = (param: [...P, number]) => void
|
|
891
|
+
|
|
892
|
+
// TypeBox
|
|
893
|
+
|
|
894
|
+
const P = Type.Tuple([Type.Number(), Type.Number()])
|
|
895
|
+
|
|
896
|
+
const F1 = Type.Function(Type.Rest(P), Type.Void())
|
|
897
|
+
|
|
898
|
+
const F2 = Type.Function([...Type.Rest(P), Type.Number()], Type.Void())
|
|
899
|
+
```
|
|
860
900
|
<a name='types-unsafe'></a>
|
|
861
901
|
|
|
862
|
-
### Unsafe
|
|
902
|
+
### Unsafe Types
|
|
863
903
|
|
|
864
904
|
Use `Type.Unsafe` to create custom schematics with user defined inference rules.
|
|
865
905
|
|
|
@@ -904,7 +944,7 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
|
904
944
|
|
|
905
945
|
<a name='types-guards'></a>
|
|
906
946
|
|
|
907
|
-
###
|
|
947
|
+
### Type Gaurds
|
|
908
948
|
|
|
909
949
|
TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
|
|
910
950
|
|
|
@@ -1436,11 +1476,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1436
1476
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1437
1477
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1438
1478
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1439
|
-
│ typebox/compiler │ '127.
|
|
1440
|
-
│ typebox/errors │ '110.9 kb' │ '
|
|
1441
|
-
│ typebox/system │ ' 76.
|
|
1442
|
-
│ typebox/value │ '176.
|
|
1443
|
-
│ typebox │ ' 75.
|
|
1479
|
+
│ typebox/compiler │ '127.1 kb' │ ' 56.7 kb' │ '2.24 x' │
|
|
1480
|
+
│ typebox/errors │ '110.9 kb' │ ' 48.9 kb' │ '2.27 x' │
|
|
1481
|
+
│ typebox/system │ ' 76.3 kb' │ ' 31.2 kb' │ '2.44 x' │
|
|
1482
|
+
│ typebox/value │ '176.8 kb' │ ' 76.5 kb' │ '2.31 x' │
|
|
1483
|
+
│ typebox │ ' 75.2 kb' │ ' 30.8 kb' │ '2.44 x' │
|
|
1444
1484
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1445
1485
|
```
|
|
1446
1486
|
|
package/typebox.d.ts
CHANGED
|
@@ -20,15 +20,10 @@ export type Evaluate<T> = T extends infer O ? {
|
|
|
20
20
|
} : never;
|
|
21
21
|
export type Ensure<T> = T extends infer U ? U : never;
|
|
22
22
|
export type AssertProperties<T> = T extends TProperties ? T : TProperties;
|
|
23
|
-
export type
|
|
24
|
-
export type
|
|
25
|
-
export type
|
|
26
|
-
export type
|
|
27
|
-
export type AssertKeys<T> = Assert<T, Key[]>;
|
|
28
|
-
export type AssertKey<T> = Assert<T, Key>;
|
|
29
|
-
export type Key = keyof any;
|
|
30
|
-
export type IntersectType<T extends TSchema[]> = T extends [] ? TNever : T extends [TSchema] ? AssertSchema<T[0]> : TIntersect<T>;
|
|
31
|
-
export type UnionType<T extends TSchema[]> = T extends [] ? TNever : T extends [TSchema] ? AssertSchema<T[0]> : TUnion<T>;
|
|
23
|
+
export type AssertRest<T, E extends TSchema[] = TSchema[]> = T extends E ? T : [];
|
|
24
|
+
export type AssertType<T, E extends TSchema = TSchema> = T extends E ? T : TNever;
|
|
25
|
+
export type IntersectType<T extends TSchema[]> = T extends [] ? TNever : T extends [TSchema] ? AssertType<T[0]> : TIntersect<T>;
|
|
26
|
+
export type UnionType<T extends TSchema[]> = T extends [] ? TNever : T extends [TSchema] ? AssertType<T[0]> : TUnion<T>;
|
|
32
27
|
export type TModifier = TReadonlyOptional<TSchema> | TOptional<TSchema> | TReadonly<TSchema>;
|
|
33
28
|
export type TReadonly<T extends TSchema> = T & {
|
|
34
29
|
[Modifier]: 'Readonly';
|
|
@@ -39,6 +34,7 @@ export type TOptional<T extends TSchema> = T & {
|
|
|
39
34
|
export type TReadonlyOptional<T extends TSchema> = T & {
|
|
40
35
|
[Modifier]: 'ReadonlyOptional';
|
|
41
36
|
};
|
|
37
|
+
export type Key = string | number;
|
|
42
38
|
export interface SchemaOptions {
|
|
43
39
|
$schema?: string;
|
|
44
40
|
/** Id for this schema */
|
|
@@ -99,21 +95,21 @@ export interface TBoolean extends TSchema {
|
|
|
99
95
|
export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>;
|
|
100
96
|
export type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns'];
|
|
101
97
|
export type TCompositeIsOptional<T extends TSchema> = T extends TOptional<T> | TReadonlyOptional<T> ? true : false;
|
|
102
|
-
export type TCompositeOptional<T extends TSchema[]> = T extends [infer L, ...infer R] ? TCompositeIsOptional<
|
|
98
|
+
export type TCompositeOptional<T extends TSchema[]> = T extends [infer L, ...infer R] ? TCompositeIsOptional<AssertType<L>> extends false ? false : TCompositeOptional<AssertRest<R>> : true;
|
|
103
99
|
export type TCompositeKeyOfUnion1<T extends TObject> = keyof T['properties'];
|
|
104
100
|
export type TCompositeKeyOfUnion2<T extends TObject[]> = T extends [infer L, ...infer R] ? TCompositeKeyOfUnion1<Assert<L, TObject>> | TCompositeKeyOfUnion2<Assert<R, TObject[]>> : never;
|
|
105
101
|
export type TCompositeKeyOf<T extends TObject[]> = UnionToTuple<TCompositeKeyOfUnion2<T>>;
|
|
106
102
|
export type TCompositePropertiesWithKey1<T extends TObject, K extends Key> = K extends keyof T['properties'] ? [T['properties'][K]] : [];
|
|
107
|
-
export type TCompositePropertiesWithKey2<T extends TObject[], K extends Key> = T extends [infer L, ...infer R] ? [...TCompositePropertiesWithKey1<
|
|
108
|
-
export type TCompositeObjectProperty<T extends TObject[], K extends Key> = TCompositePropertiesWithKey2<T, K> extends infer S ? TCompositeOptional<
|
|
109
|
-
[_ in K]: TOptional<IntersectType<
|
|
103
|
+
export type TCompositePropertiesWithKey2<T extends TObject[], K extends Key> = T extends [infer L, ...infer R] ? [...TCompositePropertiesWithKey1<Assert<L, TObject>, K>, ...TCompositePropertiesWithKey2<Assert<R, TObject[]>, K>] : [];
|
|
104
|
+
export type TCompositeObjectProperty<T extends TObject[], K extends Key> = TCompositePropertiesWithKey2<T, K> extends infer S ? TCompositeOptional<AssertRest<S>> extends true ? {
|
|
105
|
+
[_ in K]: TOptional<IntersectType<AssertRest<S>>>;
|
|
110
106
|
} : {
|
|
111
|
-
[_ in K]: IntersectType<
|
|
107
|
+
[_ in K]: IntersectType<AssertRest<S>>;
|
|
112
108
|
} : {};
|
|
113
|
-
export type TCompositeObjectsWithKeys<T extends TObject[], K extends Key[]> = K extends [infer L, ...infer R] ? L extends Key ? TCompositeObjectProperty<T, L> & TCompositeObjectsWithKeys<T,
|
|
114
|
-
export type TComposite<T extends TObject[]> = Ensure<TObject<Evaluate<TCompositeObjectsWithKeys<T,
|
|
109
|
+
export type TCompositeObjectsWithKeys<T extends TObject[], K extends Key[]> = K extends [infer L, ...infer R] ? L extends Key ? TCompositeObjectProperty<T, L> & TCompositeObjectsWithKeys<T, Assert<R, Key[]>> : {} : {};
|
|
110
|
+
export type TComposite<T extends TObject[]> = Ensure<TObject<Evaluate<TCompositeObjectsWithKeys<T, Assert<TCompositeKeyOf<T>, Key[]>>>>>;
|
|
115
111
|
export type TConstructorParameterArray<T extends readonly TSchema[], P extends unknown[]> = [...{
|
|
116
|
-
[K in keyof T]: Static<
|
|
112
|
+
[K in keyof T]: Static<AssertType<T[K]>, P>;
|
|
117
113
|
}];
|
|
118
114
|
export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
|
|
119
115
|
[Kind]: 'Constructor';
|
|
@@ -144,25 +140,25 @@ export interface TEnum<T extends Record<string, string | number> = Record<string
|
|
|
144
140
|
static: T[keyof T];
|
|
145
141
|
anyOf: TLiteral<string | number>[];
|
|
146
142
|
}
|
|
147
|
-
export type TExtends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema> = (Static<L> extends Static<R> ? T : U) extends infer O ? UnionToTuple<O> extends [infer X, infer Y] ? TUnion<[
|
|
148
|
-
export type TExcludeTemplateLiteralResult<T extends string> = UnionType<
|
|
143
|
+
export type TExtends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema> = (Static<L> extends Static<R> ? T : U) extends infer O ? UnionToTuple<O> extends [infer X, infer Y] ? TUnion<[AssertType<X>, AssertType<Y>]> : AssertType<O> : never;
|
|
144
|
+
export type TExcludeTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{
|
|
149
145
|
[K in T]: TLiteral<K>;
|
|
150
146
|
}[T]>>>;
|
|
151
147
|
export type TExcludeTemplateLiteral<T extends TTemplateLiteral, U extends TSchema> = Exclude<Static<T>, Static<U>> extends infer S ? TExcludeTemplateLiteralResult<Assert<S, string>> : never;
|
|
152
|
-
export type TExcludeArray<T extends TSchema[], U extends TSchema> =
|
|
153
|
-
[K in keyof T]: Static<
|
|
154
|
-
}[number]>> extends infer R ? UnionType<
|
|
148
|
+
export type TExcludeArray<T extends TSchema[], U extends TSchema> = AssertRest<UnionToTuple<{
|
|
149
|
+
[K in keyof T]: Static<AssertType<T[K]>> extends Static<U> ? never : T[K];
|
|
150
|
+
}[number]>> extends infer R ? UnionType<AssertRest<R>> : never;
|
|
155
151
|
export type TExclude<T extends TSchema, U extends TSchema> = T extends TTemplateLiteral ? TExcludeTemplateLiteral<T, U> : T extends TUnion<infer S> ? TExcludeArray<S, U> : T extends U ? TNever : T;
|
|
156
|
-
export type TExtractTemplateLiteralResult<T extends string> = UnionType<
|
|
152
|
+
export type TExtractTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{
|
|
157
153
|
[K in T]: TLiteral<K>;
|
|
158
154
|
}[T]>>>;
|
|
159
155
|
export type TExtractTemplateLiteral<T extends TTemplateLiteral, U extends TSchema> = Extract<Static<T>, Static<U>> extends infer S ? TExtractTemplateLiteralResult<Assert<S, string>> : never;
|
|
160
|
-
export type TExtractArray<T extends TSchema[], U extends TSchema> =
|
|
161
|
-
[K in keyof T]: Static<
|
|
162
|
-
}[number]>> extends infer R ? UnionType<
|
|
156
|
+
export type TExtractArray<T extends TSchema[], U extends TSchema> = AssertRest<UnionToTuple<{
|
|
157
|
+
[K in keyof T]: Static<AssertType<T[K]>> extends Static<U> ? T[K] : never;
|
|
158
|
+
}[number]>> extends infer R ? UnionType<AssertRest<R>> : never;
|
|
163
159
|
export type TExtract<T extends TSchema, U extends TSchema> = T extends TTemplateLiteral ? TExtractTemplateLiteral<T, U> : T extends TUnion<infer S> ? TExtractArray<S, U> : T extends U ? T : T;
|
|
164
160
|
export type TFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
|
|
165
|
-
[K in keyof T]: Static<
|
|
161
|
+
[K in keyof T]: Static<AssertType<T[K]>, P>;
|
|
166
162
|
}];
|
|
167
163
|
export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
|
|
168
164
|
[Kind]: 'Function';
|
|
@@ -172,18 +168,20 @@ export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends T
|
|
|
172
168
|
parameters: T;
|
|
173
169
|
returns: U;
|
|
174
170
|
}
|
|
175
|
-
export type
|
|
176
|
-
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] : [
|
|
177
172
|
];
|
|
178
|
-
export type TIndexTuple<T extends TSchema[], K extends
|
|
173
|
+
export type TIndexTuple<T extends TSchema[], K extends Key> = K extends keyof T ? [T[K]] : [
|
|
179
174
|
];
|
|
180
|
-
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>] : [
|
|
181
176
|
];
|
|
182
|
-
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 : [
|
|
183
178
|
];
|
|
184
|
-
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
|
+
] : [
|
|
185
183
|
];
|
|
186
|
-
export type TIndex<T extends TSchema, K extends
|
|
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;
|
|
187
185
|
export interface TInteger extends TSchema, NumericOptions<number> {
|
|
188
186
|
[Kind]: 'Integer';
|
|
189
187
|
static: number;
|
|
@@ -196,7 +194,7 @@ export interface IntersectOptions extends SchemaOptions {
|
|
|
196
194
|
export interface TIntersect<T extends TSchema[] = TSchema[]> extends TSchema, IntersectOptions {
|
|
197
195
|
[Kind]: 'Intersect';
|
|
198
196
|
static: TupleToIntersect<{
|
|
199
|
-
[K in keyof T]: Static<
|
|
197
|
+
[K in keyof T]: Static<AssertType<T[K]>, this['params']>;
|
|
200
198
|
}>;
|
|
201
199
|
type?: 'object';
|
|
202
200
|
allOf: [...T];
|
|
@@ -205,11 +203,11 @@ export type TKeyOfProperties<T extends TSchema> = Static<T> extends infer S ? Un
|
|
|
205
203
|
[K in keyof S]: TLiteral<`${Assert<K, TLiteralValue>}`>;
|
|
206
204
|
}[keyof S]> : [];
|
|
207
205
|
export type TKeyOfIndicesArray<T extends TSchema[]> = UnionToTuple<keyof T & `${number}`>;
|
|
208
|
-
export type TKeyOfIndices<T extends TSchema[]> =
|
|
206
|
+
export type TKeyOfIndices<T extends TSchema[]> = AssertRest<TKeyOfIndicesArray<T> extends infer R ? {
|
|
209
207
|
[K in keyof R]: TLiteral<Assert<R[K], TLiteralValue>>;
|
|
210
208
|
} : []>;
|
|
211
209
|
export type TKeyOf<T extends TSchema = TSchema> = (T extends TRecursive<infer S> ? TKeyOfProperties<S> : T extends TIntersect ? TKeyOfProperties<T> : T extends TUnion ? TKeyOfProperties<T> : T extends TObject ? TKeyOfProperties<T> : T extends TTuple<infer K> ? TKeyOfIndices<K> : T extends TArray ? [TNumber] : T extends TRecord<infer K> ? [K] : [
|
|
212
|
-
]) extends infer R ? UnionType<
|
|
210
|
+
]) extends infer R ? UnionType<AssertRest<R>> : never;
|
|
213
211
|
export type TLiteralValue = string | number | boolean;
|
|
214
212
|
export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
|
|
215
213
|
[Kind]: 'Literal';
|
|
@@ -269,27 +267,27 @@ export interface TObject<T extends TProperties = TProperties> extends TSchema, O
|
|
|
269
267
|
properties: T;
|
|
270
268
|
required?: string[];
|
|
271
269
|
}
|
|
272
|
-
export type TOmitArray<T extends TSchema[], K extends keyof any> =
|
|
273
|
-
[K2 in keyof T]: TOmit<
|
|
270
|
+
export type TOmitArray<T extends TSchema[], K extends keyof any> = AssertRest<{
|
|
271
|
+
[K2 in keyof T]: TOmit<AssertType<T[K2]>, K>;
|
|
274
272
|
}>;
|
|
275
273
|
export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<AssertProperties<Omit<T, K>>>;
|
|
276
274
|
export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
277
275
|
export type TParameters<T extends TFunction> = Ensure<TTuple<T['parameters']>>;
|
|
278
|
-
export type TPartialObjectArray<T extends TObject[]> =
|
|
279
|
-
[K in keyof T]: TPartial<
|
|
276
|
+
export type TPartialObjectArray<T extends TObject[]> = AssertRest<{
|
|
277
|
+
[K in keyof T]: TPartial<AssertType<T[K], TObject>>;
|
|
280
278
|
}, TObject[]>;
|
|
281
|
-
export type TPartialArray<T extends TSchema[]> =
|
|
282
|
-
[K in keyof T]: TPartial<
|
|
279
|
+
export type TPartialArray<T extends TSchema[]> = AssertRest<{
|
|
280
|
+
[K in keyof T]: TPartial<AssertType<T[K]>>;
|
|
283
281
|
}>;
|
|
284
282
|
export type TPartialProperties<T extends TProperties> = Evaluate<AssertProperties<{
|
|
285
283
|
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T[K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T[K] extends TOptional<infer U> ? TOptional<U> : TOptional<T[K]>;
|
|
286
284
|
}>>;
|
|
287
285
|
export type TPartial<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : T extends TUnion<infer S> ? TUnion<TPartialArray<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T;
|
|
288
286
|
export type TPickArray<T extends TSchema[], K extends keyof any> = {
|
|
289
|
-
[K2 in keyof T]: TPick<
|
|
287
|
+
[K2 in keyof T]: TPick<AssertType<T[K2]>, K>;
|
|
290
288
|
};
|
|
291
289
|
export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
|
|
292
|
-
[K in keyof R]:
|
|
290
|
+
[K in keyof R]: AssertType<R[K]> extends TSchema ? R[K] : never;
|
|
293
291
|
}) : never;
|
|
294
292
|
export type TPick<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
|
|
295
293
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
@@ -303,7 +301,7 @@ export type RecordTemplateLiteralObjectType<K extends TTemplateLiteral, T extend
|
|
|
303
301
|
[_ in Static<K>]: T;
|
|
304
302
|
}>>>;
|
|
305
303
|
export type RecordTemplateLiteralType<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends true ? RecordTemplateLiteralObjectType<K, T> : TRecord<K, T>;
|
|
306
|
-
export type RecordUnionLiteralType<K extends TUnion
|
|
304
|
+
export type RecordUnionLiteralType<K extends TUnion, T extends TSchema> = Static<K> extends string ? Ensure<TObject<{
|
|
307
305
|
[X in Static<K>]: T;
|
|
308
306
|
}>> : never;
|
|
309
307
|
export type RecordLiteralType<K extends TLiteral<string | number>, T extends TSchema> = Ensure<TObject<{
|
|
@@ -336,9 +334,10 @@ export interface TRef<T extends TSchema = TSchema> extends TSchema {
|
|
|
336
334
|
static: Static<T, this['params']>;
|
|
337
335
|
$ref: string;
|
|
338
336
|
}
|
|
337
|
+
export type TRest<T extends TSchema> = T extends TTuple<infer R> ? Assert<R, TSchema[]> : Assert<[T], TSchema[]>;
|
|
339
338
|
export type TReturnType<T extends TFunction> = T['returns'];
|
|
340
|
-
export type TRequiredArray<T extends TSchema[]> =
|
|
341
|
-
[K in keyof T]: TRequired<
|
|
339
|
+
export type TRequiredArray<T extends TSchema[]> = AssertRest<{
|
|
340
|
+
[K in keyof T]: TRequired<AssertType<T[K]>>;
|
|
342
341
|
}>;
|
|
343
342
|
export type TRequiredProperties<T extends TProperties> = Evaluate<AssertProperties<{
|
|
344
343
|
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T[K] extends TReadonly<infer U> ? TReadonly<U> : T[K] extends TOptional<infer U> ? U : T[K];
|
|
@@ -373,14 +372,14 @@ export type TTemplateLiteralConst<T, Acc extends string> = T extends TUnion<infe
|
|
|
373
372
|
[K in keyof U]: TTemplateLiteralUnion<Assert<[U[K]], TTemplateLiteralKind[]>, Acc>;
|
|
374
373
|
}[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;
|
|
375
374
|
export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string = ''> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : Acc;
|
|
376
|
-
export type TTemplateLiteralKeyArray<T extends TTemplateLiteral> =
|
|
375
|
+
export type TTemplateLiteralKeyArray<T extends TTemplateLiteral> = Assert<UnionToTuple<Static<T>>, Key[]>;
|
|
377
376
|
export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema {
|
|
378
377
|
[Kind]: 'TemplateLiteral';
|
|
379
378
|
static: TTemplateLiteralUnion<T>;
|
|
380
379
|
type: 'string';
|
|
381
380
|
pattern: string;
|
|
382
381
|
}
|
|
383
|
-
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ?
|
|
382
|
+
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? AssertRest<R> : never;
|
|
384
383
|
export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
385
384
|
[Kind]: 'Tuple';
|
|
386
385
|
static: {
|
|
@@ -502,8 +501,8 @@ export declare namespace TypeGuard {
|
|
|
502
501
|
function TLiteralNumber(schema: unknown): schema is TLiteral<number>;
|
|
503
502
|
/** Returns true if the given schema is TLiteral<boolean> */
|
|
504
503
|
function TLiteralBoolean(schema: unknown): schema is TLiteral<boolean>;
|
|
505
|
-
/** Returns true if the given schema is TUnion<Literal<string>[]> */
|
|
506
|
-
function TLiteralUnion(schema: unknown): schema is TUnion<TLiteral
|
|
504
|
+
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
505
|
+
function TLiteralUnion(schema: unknown): schema is TUnion<TLiteral[]>;
|
|
507
506
|
/** Returns true if the given schema is TLiteral */
|
|
508
507
|
function TLiteral(schema: unknown): schema is TLiteral;
|
|
509
508
|
/** Returns true if the given schema is TNever */
|
|
@@ -571,7 +570,7 @@ export declare namespace TypeClone {
|
|
|
571
570
|
function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T;
|
|
572
571
|
}
|
|
573
572
|
export declare namespace IndexedAccessor {
|
|
574
|
-
function Resolve(schema: TSchema, keys: string[]): TSchema[];
|
|
573
|
+
function Resolve(schema: TSchema, keys: (string | number)[]): TSchema[];
|
|
575
574
|
}
|
|
576
575
|
export declare namespace ObjectMap {
|
|
577
576
|
function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T;
|
|
@@ -589,6 +588,10 @@ export declare namespace KeyArrayResolver {
|
|
|
589
588
|
/** Resolves an array of string[] keys from the given schema or array type. */
|
|
590
589
|
function Resolve(schema: TSchema | string[]): string[];
|
|
591
590
|
}
|
|
591
|
+
export declare namespace UnionResolver {
|
|
592
|
+
/** Returns a resolved union with interior unions flattened */
|
|
593
|
+
function Resolve(union: TUnion): TUnion;
|
|
594
|
+
}
|
|
592
595
|
export declare namespace TemplateLiteralPattern {
|
|
593
596
|
function Create(kinds: TTemplateLiteralKind[]): string;
|
|
594
597
|
}
|
|
@@ -654,15 +657,17 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
654
657
|
/** `[Standard]` Extracts from the left type any type that is assignable to the right */
|
|
655
658
|
Extract<L extends TSchema, R extends TSchema>(left: L, right: R, options?: SchemaOptions): TExtract<L, R>;
|
|
656
659
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
657
|
-
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex<T, K
|
|
660
|
+
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex<T, Assert<K, Key[]>>;
|
|
658
661
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
659
|
-
Index<T extends TSchema, K extends TUnion<TLiteral<
|
|
662
|
+
Index<T extends TSchema, K extends TUnion<TLiteral<Key>[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TLiteralUnion<K>>;
|
|
660
663
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
661
|
-
Index<T extends TSchema, K extends TLiteral<
|
|
664
|
+
Index<T extends TSchema, K extends TLiteral<Key>>(schema: T, key: K, options?: SchemaOptions): TIndex<T, [K['const']]>;
|
|
662
665
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
663
666
|
Index<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TIndex<T, TTemplateLiteralKeyArray<K>>;
|
|
664
667
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
665
|
-
Index<T extends
|
|
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']>;
|
|
666
671
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
667
672
|
Index<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TIndex<T, never>;
|
|
668
673
|
/** `[Standard]` Creates an Integer type */
|
|
@@ -709,7 +714,7 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
709
714
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
710
715
|
Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>;
|
|
711
716
|
/** `[Standard]` Creates a Record type */
|
|
712
|
-
Record<K extends TUnion
|
|
717
|
+
Record<K extends TUnion, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordUnionLiteralType<K, T>;
|
|
713
718
|
/** `[Standard]` Creates a Record type */
|
|
714
719
|
Record<K extends TLiteral<string | number>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordLiteralType<K, T>;
|
|
715
720
|
/** `[Standard]` Creates a Record type */
|
|
@@ -724,6 +729,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
724
729
|
Ref<T extends TSchema>(schema: T, options?: SchemaOptions): TRef<T>;
|
|
725
730
|
/** `[Standard]` Creates a mapped type where all properties are Required */
|
|
726
731
|
Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;
|
|
732
|
+
/** `[Standard]` Returns a schema array which allows types to compose with the JavaScript spread operator */
|
|
733
|
+
Rest<T extends TSchema>(schema: T): TRest<T>;
|
|
727
734
|
/** `[Standard]` Creates a String type */
|
|
728
735
|
String(options?: StringOptions): TString;
|
|
729
736
|
/** `[Standard]` Creates a template literal type */
|
|
@@ -749,14 +756,10 @@ export declare class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
749
756
|
/** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
|
|
750
757
|
ConstructorParameters<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TConstructorParameters<T>;
|
|
751
758
|
/** `[Extended]` Creates a Constructor type */
|
|
752
|
-
Constructor<T extends TTuple<TSchema[]>, U extends TSchema>(parameters: T, returns: U, options?: SchemaOptions): TConstructor<TTupleIntoArray<T>, U>;
|
|
753
|
-
/** `[Extended]` Creates a Constructor type */
|
|
754
759
|
Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U>;
|
|
755
760
|
/** `[Extended]` Creates a Date type */
|
|
756
761
|
Date(options?: DateOptions): TDate;
|
|
757
762
|
/** `[Extended]` Creates a Function type */
|
|
758
|
-
Function<T extends TTuple<TSchema[]>, U extends TSchema>(parameters: T, returns: U, options?: SchemaOptions): TFunction<TTupleIntoArray<T>, U>;
|
|
759
|
-
/** `[Extended]` Creates a Function type */
|
|
760
763
|
Function<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunction<T, U>;
|
|
761
764
|
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
762
765
|
InstanceType<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TInstanceType<T>;
|
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,9 +324,9 @@ 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>[]> */
|
|
327
|
+
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
328
328
|
function TLiteralUnion(schema) {
|
|
329
|
-
return TUnion(schema) && schema.anyOf.every((schema) =>
|
|
329
|
+
return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(schema));
|
|
330
330
|
}
|
|
331
331
|
TypeGuard.TLiteralUnion = TLiteralUnion;
|
|
332
332
|
/** Returns true if the given schema is TLiteral */
|
|
@@ -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 = {}));
|
|
@@ -1520,7 +1520,7 @@ var KeyArrayResolver;
|
|
|
1520
1520
|
if (globalThis.Array.isArray(schema))
|
|
1521
1521
|
return schema;
|
|
1522
1522
|
if (TypeGuard.TLiteralUnion(schema))
|
|
1523
|
-
return schema.anyOf.map((schema) => schema.const);
|
|
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;
|
|
@@ -1960,6 +1981,11 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1960
1981
|
if (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1961
1982
|
return TypeClone.Clone(schema.items, options);
|
|
1962
1983
|
}
|
|
1984
|
+
if (TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1985
|
+
const items = schema.items === undefined ? [] : schema.items;
|
|
1986
|
+
const cloned = items.map((schema) => TypeClone.Clone(schema, {}));
|
|
1987
|
+
return this.Union(cloned, options);
|
|
1988
|
+
}
|
|
1963
1989
|
const resolved = IndexedAccessor.Resolve(schema, keys);
|
|
1964
1990
|
const cloned = resolved.map((schema) => TypeClone.Clone(schema, {}));
|
|
1965
1991
|
return this.Union(cloned, options);
|
|
@@ -2110,20 +2136,21 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2110
2136
|
? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(schema, {}) }), {}), options))
|
|
2111
2137
|
: this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema, {}) } });
|
|
2112
2138
|
}
|
|
2113
|
-
else if (TypeGuard.
|
|
2114
|
-
|
|
2115
|
-
|
|
2139
|
+
else if (TypeGuard.TUnion(key)) {
|
|
2140
|
+
const union = UnionResolver.Resolve(key);
|
|
2141
|
+
if (TypeGuard.TLiteralUnion(union)) {
|
|
2142
|
+
const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
|
|
2116
2143
|
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
2117
2144
|
}
|
|
2118
2145
|
else
|
|
2119
|
-
throw Error('TypeBuilder: Record key
|
|
2146
|
+
throw Error('TypeBuilder: Record key of type union contains non-literal types');
|
|
2120
2147
|
}
|
|
2121
2148
|
else if (TypeGuard.TLiteral(key)) {
|
|
2122
2149
|
if (typeof key.const === 'string' || typeof key.const === 'number') {
|
|
2123
2150
|
return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options);
|
|
2124
2151
|
}
|
|
2125
2152
|
else
|
|
2126
|
-
throw Error('TypeBuilder: Record key
|
|
2153
|
+
throw Error('TypeBuilder: Record key of type literal is not of type string or number');
|
|
2127
2154
|
}
|
|
2128
2155
|
else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
|
|
2129
2156
|
const pattern = exports.PatternNumberExact;
|
|
@@ -2134,7 +2161,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2134
2161
|
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } });
|
|
2135
2162
|
}
|
|
2136
2163
|
else {
|
|
2137
|
-
throw Error(`StandardTypeBuilder:
|
|
2164
|
+
throw Error(`StandardTypeBuilder: Record key is an invalid type`);
|
|
2138
2165
|
}
|
|
2139
2166
|
}
|
|
2140
2167
|
/** `[Standard]` Creates a Recursive type */
|
|
@@ -2177,6 +2204,17 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2177
2204
|
return schema;
|
|
2178
2205
|
}, options);
|
|
2179
2206
|
}
|
|
2207
|
+
/** `[Standard]` Returns a schema array which allows types to compose with the JavaScript spread operator */
|
|
2208
|
+
Rest(schema) {
|
|
2209
|
+
if (TypeGuard.TTuple(schema)) {
|
|
2210
|
+
if (schema.items === undefined)
|
|
2211
|
+
return [];
|
|
2212
|
+
return schema.items.map((schema) => TypeClone.Clone(schema, {}));
|
|
2213
|
+
}
|
|
2214
|
+
else {
|
|
2215
|
+
return [TypeClone.Clone(schema, {})];
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2180
2218
|
/** `[Standard]` Creates a String type */
|
|
2181
2219
|
String(options = {}) {
|
|
2182
2220
|
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
|
|
@@ -2232,37 +2270,21 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2232
2270
|
ConstructorParameters(schema, options = {}) {
|
|
2233
2271
|
return this.Tuple([...schema.parameters], { ...options });
|
|
2234
2272
|
}
|
|
2235
|
-
|
|
2273
|
+
/** `[Extended]` Creates a Constructor type */
|
|
2274
|
+
Constructor(parameters, returns, options) {
|
|
2236
2275
|
const clonedReturns = TypeClone.Clone(returns, {});
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
2240
|
-
}
|
|
2241
|
-
else if (globalThis.Array.isArray(parameters)) {
|
|
2242
|
-
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2243
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
2244
|
-
}
|
|
2245
|
-
else {
|
|
2246
|
-
throw new Error('ExtendedTypeBuilder.Constructor: Invalid parameters');
|
|
2247
|
-
}
|
|
2276
|
+
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2277
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
2248
2278
|
}
|
|
2249
2279
|
/** `[Extended]` Creates a Date type */
|
|
2250
2280
|
Date(options = {}) {
|
|
2251
2281
|
return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
|
|
2252
2282
|
}
|
|
2253
|
-
|
|
2283
|
+
/** `[Extended]` Creates a Function type */
|
|
2284
|
+
Function(parameters, returns, options) {
|
|
2254
2285
|
const clonedReturns = TypeClone.Clone(returns, {});
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
|
|
2258
|
-
}
|
|
2259
|
-
else if (globalThis.Array.isArray(parameters)) {
|
|
2260
|
-
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2261
|
-
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
|
|
2262
|
-
}
|
|
2263
|
-
else {
|
|
2264
|
-
throw new Error('ExtendedTypeBuilder.Function: Invalid parameters');
|
|
2265
|
-
}
|
|
2286
|
+
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2287
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
|
|
2266
2288
|
}
|
|
2267
2289
|
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
2268
2290
|
InstanceType(schema, options = {}) {
|