@sinclair/typebox 0.28.1 → 0.28.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/compiler.js +1 -1
- package/package.json +1 -1
- package/readme.md +56 -16
- package/typebox.d.ts +46 -49
- package/typebox.js +24 -24
package/compiler/compiler.js
CHANGED
|
@@ -342,7 +342,7 @@ var TypeCompiler;
|
|
|
342
342
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
343
343
|
const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
344
344
|
const local = PushLocal(`new RegExp(/${patternKey}/)`);
|
|
345
|
-
const check1 = CreateExpression(patternSchema, references, value);
|
|
345
|
+
const check1 = CreateExpression(patternSchema, references, 'value');
|
|
346
346
|
const check2 = Types.TypeGuard.TSchema(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true';
|
|
347
347
|
const expression = `(${local}.test(key) ? ${check1} : ${check2})`;
|
|
348
348
|
yield `(Object.entries(${value}).every(([key, value]) => ${expression}))`;
|
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 │ '
|
|
1440
|
-
│ typebox/errors │ '110.
|
|
1441
|
-
│ typebox/system │ '
|
|
1442
|
-
│ typebox/value │ '176.
|
|
1443
|
-
│ typebox │ '
|
|
1479
|
+
│ typebox/compiler │ '126.7 kb' │ ' 56.6 kb' │ '2.24 x' │
|
|
1480
|
+
│ typebox/errors │ '110.4 kb' │ ' 48.8 kb' │ '2.26 x' │
|
|
1481
|
+
│ typebox/system │ ' 75.9 kb' │ ' 31.1 kb' │ '2.44 x' │
|
|
1482
|
+
│ typebox/value │ '176.4 kb' │ ' 76.4 kb' │ '2.31 x' │
|
|
1483
|
+
│ typebox │ ' 74.8 kb' │ ' 30.7 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 = keyof any;
|
|
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';
|
|
@@ -177,13 +173,13 @@ export type TIndexProperty<T extends TProperties, K extends IndexKey> = K extend
|
|
|
177
173
|
];
|
|
178
174
|
export type TIndexTuple<T extends TSchema[], K extends IndexKey> = K extends keyof T ? [T[K]] : [
|
|
179
175
|
];
|
|
180
|
-
export type TIndexComposite<T extends TSchema[], K extends IndexKey> = T extends [infer L, ...infer R] ? [...TIndexKey<
|
|
176
|
+
export type TIndexComposite<T extends TSchema[], K extends IndexKey> = T extends [infer L, ...infer R] ? [...TIndexKey<AssertType<L>, K>, ...TIndexComposite<AssertRest<R>, K>] : [
|
|
181
177
|
];
|
|
182
178
|
export type TIndexKey<T extends TSchema, K extends IndexKey> = 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
179
|
];
|
|
184
180
|
export type TIndexKeys<T extends TSchema, K extends IndexKey[]> = K extends [infer L, ...infer R] ? [...TIndexKey<T, Assert<L, IndexKey>>, ...TIndexKeys<T, Assert<R, IndexKey[]>>] : [
|
|
185
181
|
];
|
|
186
|
-
export type TIndex<T extends TSchema, K extends IndexKey[]> = TIndexKeys<T, K> extends infer R ? T extends TRecursive<infer S> ? TIndex<S, K> : T extends TTuple ? UnionType<
|
|
182
|
+
export type TIndex<T extends TSchema, K extends IndexKey[]> = 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
183
|
export interface TInteger extends TSchema, NumericOptions<number> {
|
|
188
184
|
[Kind]: 'Integer';
|
|
189
185
|
static: number;
|
|
@@ -196,7 +192,7 @@ export interface IntersectOptions extends SchemaOptions {
|
|
|
196
192
|
export interface TIntersect<T extends TSchema[] = TSchema[]> extends TSchema, IntersectOptions {
|
|
197
193
|
[Kind]: 'Intersect';
|
|
198
194
|
static: TupleToIntersect<{
|
|
199
|
-
[K in keyof T]: Static<
|
|
195
|
+
[K in keyof T]: Static<AssertType<T[K]>, this['params']>;
|
|
200
196
|
}>;
|
|
201
197
|
type?: 'object';
|
|
202
198
|
allOf: [...T];
|
|
@@ -205,11 +201,11 @@ export type TKeyOfProperties<T extends TSchema> = Static<T> extends infer S ? Un
|
|
|
205
201
|
[K in keyof S]: TLiteral<`${Assert<K, TLiteralValue>}`>;
|
|
206
202
|
}[keyof S]> : [];
|
|
207
203
|
export type TKeyOfIndicesArray<T extends TSchema[]> = UnionToTuple<keyof T & `${number}`>;
|
|
208
|
-
export type TKeyOfIndices<T extends TSchema[]> =
|
|
204
|
+
export type TKeyOfIndices<T extends TSchema[]> = AssertRest<TKeyOfIndicesArray<T> extends infer R ? {
|
|
209
205
|
[K in keyof R]: TLiteral<Assert<R[K], TLiteralValue>>;
|
|
210
206
|
} : []>;
|
|
211
207
|
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<
|
|
208
|
+
]) extends infer R ? UnionType<AssertRest<R>> : never;
|
|
213
209
|
export type TLiteralValue = string | number | boolean;
|
|
214
210
|
export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
|
|
215
211
|
[Kind]: 'Literal';
|
|
@@ -269,27 +265,27 @@ export interface TObject<T extends TProperties = TProperties> extends TSchema, O
|
|
|
269
265
|
properties: T;
|
|
270
266
|
required?: string[];
|
|
271
267
|
}
|
|
272
|
-
export type TOmitArray<T extends TSchema[], K extends keyof any> =
|
|
273
|
-
[K2 in keyof T]: TOmit<
|
|
268
|
+
export type TOmitArray<T extends TSchema[], K extends keyof any> = AssertRest<{
|
|
269
|
+
[K2 in keyof T]: TOmit<AssertType<T[K2]>, K>;
|
|
274
270
|
}>;
|
|
275
271
|
export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<AssertProperties<Omit<T, K>>>;
|
|
276
272
|
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
273
|
export type TParameters<T extends TFunction> = Ensure<TTuple<T['parameters']>>;
|
|
278
|
-
export type TPartialObjectArray<T extends TObject[]> =
|
|
279
|
-
[K in keyof T]: TPartial<
|
|
274
|
+
export type TPartialObjectArray<T extends TObject[]> = AssertRest<{
|
|
275
|
+
[K in keyof T]: TPartial<AssertType<T[K], TObject>>;
|
|
280
276
|
}, TObject[]>;
|
|
281
|
-
export type TPartialArray<T extends TSchema[]> =
|
|
282
|
-
[K in keyof T]: TPartial<
|
|
277
|
+
export type TPartialArray<T extends TSchema[]> = AssertRest<{
|
|
278
|
+
[K in keyof T]: TPartial<AssertType<T[K]>>;
|
|
283
279
|
}>;
|
|
284
280
|
export type TPartialProperties<T extends TProperties> = Evaluate<AssertProperties<{
|
|
285
281
|
[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
282
|
}>>;
|
|
287
283
|
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
284
|
export type TPickArray<T extends TSchema[], K extends keyof any> = {
|
|
289
|
-
[K2 in keyof T]: TPick<
|
|
285
|
+
[K2 in keyof T]: TPick<AssertType<T[K2]>, K>;
|
|
290
286
|
};
|
|
291
287
|
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]:
|
|
288
|
+
[K in keyof R]: AssertType<R[K]> extends TSchema ? R[K] : never;
|
|
293
289
|
}) : never;
|
|
294
290
|
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
291
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
@@ -336,9 +332,10 @@ export interface TRef<T extends TSchema = TSchema> extends TSchema {
|
|
|
336
332
|
static: Static<T, this['params']>;
|
|
337
333
|
$ref: string;
|
|
338
334
|
}
|
|
335
|
+
export type TRest<T extends TSchema> = T extends TTuple<infer R> ? Assert<R, TSchema[]> : Assert<[T], TSchema[]>;
|
|
339
336
|
export type TReturnType<T extends TFunction> = T['returns'];
|
|
340
|
-
export type TRequiredArray<T extends TSchema[]> =
|
|
341
|
-
[K in keyof T]: TRequired<
|
|
337
|
+
export type TRequiredArray<T extends TSchema[]> = AssertRest<{
|
|
338
|
+
[K in keyof T]: TRequired<AssertType<T[K]>>;
|
|
342
339
|
}>;
|
|
343
340
|
export type TRequiredProperties<T extends TProperties> = Evaluate<AssertProperties<{
|
|
344
341
|
[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 +370,14 @@ export type TTemplateLiteralConst<T, Acc extends string> = T extends TUnion<infe
|
|
|
373
370
|
[K in keyof U]: TTemplateLiteralUnion<Assert<[U[K]], TTemplateLiteralKind[]>, Acc>;
|
|
374
371
|
}[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
372
|
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> =
|
|
373
|
+
export type TTemplateLiteralKeyArray<T extends TTemplateLiteral> = Assert<UnionToTuple<Static<T>>, Key[]>;
|
|
377
374
|
export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema {
|
|
378
375
|
[Kind]: 'TemplateLiteral';
|
|
379
376
|
static: TTemplateLiteralUnion<T>;
|
|
380
377
|
type: 'string';
|
|
381
378
|
pattern: string;
|
|
382
379
|
}
|
|
383
|
-
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ?
|
|
380
|
+
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? AssertRest<R> : never;
|
|
384
381
|
export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
385
382
|
[Kind]: 'Tuple';
|
|
386
383
|
static: {
|
|
@@ -662,7 +659,9 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
662
659
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
663
660
|
Index<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TIndex<T, TTemplateLiteralKeyArray<K>>;
|
|
664
661
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
665
|
-
Index<T extends
|
|
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']>;
|
|
666
665
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
667
666
|
Index<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TIndex<T, never>;
|
|
668
667
|
/** `[Standard]` Creates an Integer type */
|
|
@@ -724,6 +723,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
724
723
|
Ref<T extends TSchema>(schema: T, options?: SchemaOptions): TRef<T>;
|
|
725
724
|
/** `[Standard]` Creates a mapped type where all properties are Required */
|
|
726
725
|
Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;
|
|
726
|
+
/** `[Standard]` Returns a schema array which allows types to compose with the JavaScript spread operator */
|
|
727
|
+
Rest<T extends TSchema>(schema: T): TRest<T>;
|
|
727
728
|
/** `[Standard]` Creates a String type */
|
|
728
729
|
String(options?: StringOptions): TString;
|
|
729
730
|
/** `[Standard]` Creates a template literal type */
|
|
@@ -749,14 +750,10 @@ export declare class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
749
750
|
/** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
|
|
750
751
|
ConstructorParameters<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TConstructorParameters<T>;
|
|
751
752
|
/** `[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
753
|
Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U>;
|
|
755
754
|
/** `[Extended]` Creates a Date type */
|
|
756
755
|
Date(options?: DateOptions): TDate;
|
|
757
756
|
/** `[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
757
|
Function<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunction<T, U>;
|
|
761
758
|
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
762
759
|
InstanceType<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TInstanceType<T>;
|
package/typebox.js
CHANGED
|
@@ -1960,6 +1960,11 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1960
1960
|
if (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1961
1961
|
return TypeClone.Clone(schema.items, options);
|
|
1962
1962
|
}
|
|
1963
|
+
if (TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1964
|
+
const items = schema.items === undefined ? [] : schema.items;
|
|
1965
|
+
const cloned = items.map((schema) => TypeClone.Clone(schema, {}));
|
|
1966
|
+
return this.Union(cloned, options);
|
|
1967
|
+
}
|
|
1963
1968
|
const resolved = IndexedAccessor.Resolve(schema, keys);
|
|
1964
1969
|
const cloned = resolved.map((schema) => TypeClone.Clone(schema, {}));
|
|
1965
1970
|
return this.Union(cloned, options);
|
|
@@ -2177,6 +2182,17 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2177
2182
|
return schema;
|
|
2178
2183
|
}, options);
|
|
2179
2184
|
}
|
|
2185
|
+
/** `[Standard]` Returns a schema array which allows types to compose with the JavaScript spread operator */
|
|
2186
|
+
Rest(schema) {
|
|
2187
|
+
if (TypeGuard.TTuple(schema)) {
|
|
2188
|
+
if (schema.items === undefined)
|
|
2189
|
+
return [];
|
|
2190
|
+
return schema.items.map((schema) => TypeClone.Clone(schema, {}));
|
|
2191
|
+
}
|
|
2192
|
+
else {
|
|
2193
|
+
return [TypeClone.Clone(schema, {})];
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2180
2196
|
/** `[Standard]` Creates a String type */
|
|
2181
2197
|
String(options = {}) {
|
|
2182
2198
|
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
|
|
@@ -2232,37 +2248,21 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2232
2248
|
ConstructorParameters(schema, options = {}) {
|
|
2233
2249
|
return this.Tuple([...schema.parameters], { ...options });
|
|
2234
2250
|
}
|
|
2235
|
-
|
|
2251
|
+
/** `[Extended]` Creates a Constructor type */
|
|
2252
|
+
Constructor(parameters, returns, options) {
|
|
2236
2253
|
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
|
-
}
|
|
2254
|
+
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2255
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
2248
2256
|
}
|
|
2249
2257
|
/** `[Extended]` Creates a Date type */
|
|
2250
2258
|
Date(options = {}) {
|
|
2251
2259
|
return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
|
|
2252
2260
|
}
|
|
2253
|
-
|
|
2261
|
+
/** `[Extended]` Creates a Function type */
|
|
2262
|
+
Function(parameters, returns, options) {
|
|
2254
2263
|
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
|
-
}
|
|
2264
|
+
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
2265
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
|
|
2266
2266
|
}
|
|
2267
2267
|
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
2268
2268
|
InstanceType(schema, options = {}) {
|