@sinclair/typebox 0.26.0-dev.4 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/readme.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  <p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
6
6
 
7
- <img src="./typebox.png" />
7
+ <img src="https://github.com/sinclairzx81/typebox/blob/master/typebox.png?raw=true" />
8
8
 
9
9
  <br />
10
10
  <br />
@@ -62,9 +62,9 @@ type T = Static<typeof T> // type T = {
62
62
 
63
63
  ## Overview
64
64
 
65
- TypeBox is a runtime type builder that constructs in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
65
+ TypeBox is a runtime type builder that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type assertion rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
66
66
 
67
- This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST and RPC services to help validate data received over the wire.
67
+ This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
68
68
 
69
69
  License MIT
70
70
 
@@ -189,13 +189,13 @@ function receive(value: T) { // ... as a Static Type
189
189
 
190
190
  ## Types
191
191
 
192
- TypeBox types are small JSON schema fragments which can compose into more complex types though function composition. Each fragment is strictly defined to match to the static type checking rules of TypeScript. TypeBox provides a Standard type set which produces standards compliant JSON schema as well as an Extended type set used to define native JavaScript constructs.
192
+ TypeBox types are JSON schema fragments that can be composed into more complex types. Each fragment is structured such that a JSON schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox provides a set of Standard types which are used create JSON schema compliant schematics as well as an Extended type set used to create schematics for constructs native to JavaScript.
193
193
 
194
194
  <a name='types-standard'></a>
195
195
 
196
196
  ### Standard Types
197
197
 
198
- The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema specification.
198
+ The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 6 specification.
199
199
 
200
200
  ```typescript
201
201
  ┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
@@ -457,17 +457,6 @@ The following table lists the Standard TypeBox types. These types are fully comp
457
457
  │ │ │ │
458
458
  │ │ │ │
459
459
  │ │ │ │
460
- ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
461
- │ const A = Type.Object({ │ type T = { │ const T = { │
462
- │ x: Type.Number(), │ x: number, │ $id: 'T' │
463
- │ y: Type.Number() │ y: number │ type: 'object', │
464
- │ }, { $id: 'T' }) | } │ required: ['x', 'y'], │
465
- │ │ │ properties: { │
466
- │ const T = Type.Deref( │ │ x: { type: 'number' }, │
467
- │ Type.Ref(A) │ │ y: { type: 'number' }, │
468
- │ ) │ │ } │
469
- │ │ │ } │
470
- │ │ │ │
471
460
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
472
461
  ```
473
462
 
@@ -475,9 +464,7 @@ The following table lists the Standard TypeBox types. These types are fully comp
475
464
 
476
465
  ### Extended Types
477
466
 
478
- TypeBox provides several extended types that can be used to produce schematics for common JavaScript constructs. These types cannot be used with standard JSON schema validators; but are useful to help frame schematics for RPC interfaces that may receive JSON validated data. Extended types are prefixed with the `[Extended]` doc comment for convenience.
479
-
480
- The following lists the supported types
467
+ TypeBox provides several extended types that can be used to produce schematics for common JavaScript constructs. These types can not be used with standard JSON schema validators; but are useful to help frame schematics for RPC interfaces that may receive JSON validated data. Extended types are prefixed with the `[Extended]` doc comment for convenience. The following table lists the supported types.
481
468
 
482
469
  ```typescript
483
470
  ┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
@@ -570,7 +557,7 @@ The following lists the supported types
570
557
 
571
558
  ### Modifiers
572
559
 
573
- TypeBox provides property modifier types that allow properties to be mapped with TypeScript `readonly` and `optional`. The following table shows the modifier types and how they map between TypeScript and JSON Schema.
560
+ TypeBox provides modifiers that allow schema properties to be statically inferred as `readonly` or `optional`. The following table shows the supported modifiers and how they map between TypeScript and JSON Schema.
574
561
 
575
562
  ```typescript
576
563
  ┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
@@ -614,7 +601,7 @@ TypeBox provides property modifier types that allow properties to be mapped with
614
601
 
615
602
  ### Options
616
603
 
617
- You can pass JSON Schema compliant options on the last argument of any type. Option property hints are provided for convenience.
604
+ You can pass JSON Schema options on the last argument of any type. Option hints specific to each type are provided for convenience.
618
605
 
619
606
  ```typescript
620
607
  // String must be an email
@@ -644,7 +631,7 @@ const T = Type.Array(Type.Integer(), { // const T = {
644
631
 
645
632
  ### Generic Types
646
633
 
647
- Generic types can be created with generic functions. The following creates a generic `Vector<T>` type.
634
+ Generic types can be created with generic functions constrained to type `TSchema`. The following creates a generic `Vector<T>` type.
648
635
 
649
636
  ```typescript
650
637
  import { Type, Static, TSchema } from '@sinclair/typebox'
@@ -661,6 +648,12 @@ const NumberVector = Vector(Type.Number()) // const NumberVector = {
661
648
  // }
662
649
  // }
663
650
 
651
+ type NumberVector = Static<typeof NumberVector> // type NumberVector = {
652
+ // x: number,
653
+ // y: number,
654
+ // z: number
655
+ // }
656
+
664
657
  const BooleanVector = Vector(Type.Boolean()) // const BooleanVector = {
665
658
  // type: 'object',
666
659
  // required: ['x', 'y', 'z'],
@@ -670,13 +663,34 @@ const BooleanVector = Vector(Type.Boolean()) // const BooleanVector = {
670
663
  // z: { type: 'boolean' }
671
664
  // }
672
665
  // }
666
+
667
+ type BooleanVector = Static<typeof BooleanVector> // type BooleanVector = {
668
+ // x: boolean,
669
+ // y: boolean,
670
+ // z: boolean
671
+ // }
672
+ ```
673
+
674
+ The following creates a generic `Nullable<T>` type.
675
+
676
+ ```typescript
677
+ const Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
678
+
679
+ const T = Nullable(Type.String()) // const T = {
680
+ // anyOf: [
681
+ // { type: 'string' },
682
+ // { type: 'null' }
683
+ // ]
684
+ // }
685
+
686
+ type T = Static<typeof T> // type T = string | null
673
687
  ```
674
688
 
675
689
  <a name='types-references'></a>
676
690
 
677
691
  ### Reference Types
678
692
 
679
- Reference types are supported with `Type.Ref(...)`. Use `Type.Deref(...)` to dereference a type.
693
+ Reference types are supported with `Type.Ref(...)`. The target type must specify a valid `$id`.
680
694
 
681
695
  ```typescript
682
696
  const T = Type.String({ $id: 'T' }) // const T = {
@@ -687,11 +701,6 @@ const T = Type.String({ $id: 'T' }) // const T = {
687
701
  const R = Type.Ref(T) // const R = {
688
702
  // $ref: 'T'
689
703
  // }
690
-
691
- const U = Type.Deref(R) // const U = {
692
- // $id: 'T',
693
- // type: 'string'
694
- // }
695
704
  ```
696
705
 
697
706
  <a name='types-recursive'></a>
@@ -735,9 +744,9 @@ function test(node: Node) {
735
744
 
736
745
  ### Conditional Types
737
746
 
738
- Conditional types are supported with `Extends`, `Exclude` and `Extract`. These work the same as in TypeScript.
747
+ Conditional types are supported with `Extends`, `Exclude` and `Extract`.
739
748
 
740
- #### TypeScript
749
+ **TypeScript**
741
750
 
742
751
  ```typescript
743
752
  type T0 = string extends number ? true : false
@@ -747,7 +756,7 @@ type T1 = Extract<string | number, number>
747
756
  type T2 = Exclude<string | number, number>
748
757
  // ^ string
749
758
  ```
750
- #### TypeBox
759
+ **TypeBox**
751
760
  ```typescript
752
761
  const T0 = Type.Extends(Type.String(), Type.Number(), Type.Literal(true), Type.Literal(false))
753
762
  // ^ TLiteral<false>
@@ -761,7 +770,7 @@ const T2 = Type.Exclude(Type.Union([Type.String(), Type.Number()]), Type.Number(
761
770
 
762
771
  ### Unsafe
763
772
 
764
- Use `Type.Unsafe(...)` to create custom schemas with user defined inference rules.
773
+ Use `Type.Unsafe(...)` to create custom schematics with user defined inference rules.
765
774
 
766
775
  ```typescript
767
776
  const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
@@ -771,7 +780,7 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
771
780
  type T = Static<typeof T> // type T = string
772
781
  ```
773
782
 
774
- The `Type.Unsafe(...)` type allows for the expression of specific OpenAPI schema representations.
783
+ The `Type.Unsafe(...)` type can be useful to express specific OpenAPI schema representations.
775
784
 
776
785
  ```typescript
777
786
  import { Type, Static, TSchema } from '@sinclair/typebox'
@@ -806,7 +815,7 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
806
815
 
807
816
  ### Guards
808
817
 
809
- TypeBox provides a `TypeGuard` module for reflection and type assertion.
818
+ TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
810
819
 
811
820
  ```typescript
812
821
  import { Type, TypeGuard } from '@sinclair/typebox'
@@ -823,7 +832,7 @@ if(TypeGuard.TString(T)) {
823
832
 
824
833
  ### Strict
825
834
 
826
- TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and runtime type reflection. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict()` function that will omit these properties if necessary.
835
+ TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and reflection. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict()` function that will omit these properties if necessary.
827
836
 
828
837
  ```typescript
829
838
  const T = Type.Object({ // const T = {
@@ -852,7 +861,7 @@ const U = Type.Strict(T) // const U = {
852
861
 
853
862
  ## Values
854
863
 
855
- TypeBox provides an optional utility module that can be used to perform common operations on JavaScript values. This module includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This module is provided via additional import.
864
+ TypeBox provides an optional utility module that can be used to perform common operations on JavaScript values. This module includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This module is provided via optional import.
856
865
 
857
866
  ```typescript
858
867
  import { Value } from '@sinclair/typebox/value'
@@ -1222,33 +1231,37 @@ For additional comparative benchmarks, please refer to [typescript-runtime-type-
1222
1231
  This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/compile.ts).
1223
1232
 
1224
1233
  ```typescript
1225
- ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
1226
- (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
1227
- ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
1228
- Number2000 │ ' 451 ms' │ ' 16 ms' │ ' 28.19 x' │
1229
- String2000 │ ' 338 ms' │ ' 14 ms' │ ' 24.14 x' │
1230
- Boolean2000 │ ' 297 ms' │ ' 13 ms' │ ' 22.85 x' │
1231
- Null2000 │ ' 265 ms' │ ' 8 ms' │ ' 33.13 x' │
1232
- RegEx2000 │ ' 492 ms' │ ' 18 ms' │ ' 27.33 x' │
1233
- ObjectA2000 │ ' 2744 ms' │ ' 55 ms' │ ' 49.89 x' │
1234
- ObjectB 2000 │ ' 3005 ms' │ ' 44 ms' │ ' 68.30 x' │
1235
- Tuple2000 │ ' 1283 ms' │ ' 26 ms' │ ' 49.35 x' │
1236
- Union2000 │ ' 1263 ms' │ ' 27 ms' │ ' 46.78 x' │
1237
- Vector42000 │ ' 1622 ms' │ ' 23 ms' │ ' 70.52 x' │
1238
- Matrix42000 │ ' 888 ms' │ ' 12 ms' │ ' 74.00 x' │
1239
- Literal_String2000 │ ' 344 ms' │ ' 14 ms' │ ' 24.57 x' │
1240
- Literal_Number2000 │ ' 389 ms' │ ' 8 ms' │ ' 48.63 x' │
1241
- Literal_Boolean2000 │ ' 374 ms' │ ' 9 ms' │ ' 41.56 x' │
1242
- Array_Number2000 │ ' 710 ms' │ ' 12 ms' │ ' 59.17 x' │
1243
- Array_String2000 │ ' 739 ms' │ ' 9 ms' │ ' 82.11 x' │
1244
- Array_Boolean2000 │ ' 732 ms' │ ' 7 ms' │ ' 104.57 x' │
1245
- Array_ObjectA2000 │ ' 3733 ms' │ ' 42 ms' │ ' 88.88 x' │
1246
- Array_ObjectB 2000 │ ' 3602 ms' │ ' 42 ms' │ ' 85.76 x' │
1247
- Array_Tuple2000 │ ' 2204 ms' │ ' 20 ms' │ ' 110.20 x' │
1248
- Array_Union2000 │ ' 1533 ms' │ ' 24 ms' │ ' 63.88 x' │
1249
- Array_Vector42000 │ ' 2263 ms' │ ' 21 ms' │ ' 107.76 x' │
1250
- Array_Matrix42000 │ ' 1576 ms' │ ' 14 ms' │ ' 112.57 x' │
1251
- └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
1234
+ ┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
1235
+ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
1236
+ ├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
1237
+ Literal_String 1000 │ ' 260 ms' │ ' 8 ms' │ ' 32.50 x' │
1238
+ Literal_Number 1000 │ ' 198 ms' │ ' 4 ms' │ ' 49.50 x' │
1239
+ Literal_Boolean 1000 │ ' 185 ms' │ ' 5 ms' │ ' 37.00 x' │
1240
+ Primitive_Number 1000 │ ' 176 ms' │ ' 9 ms' │ ' 19.56 x' │
1241
+ Primitive_String 1000 │ ' 161 ms' │ ' 9 ms' │ ' 17.89 x' │
1242
+ Primitive_String_Pattern 1000 │ ' 215 ms' │ ' 12 ms' │ ' 17.92 x' │
1243
+ Primitive_Boolean1000 │ ' 133 ms' │ ' 5 ms' │ ' 26.60 x' │
1244
+ Primitive_Null 1000 │ ' 143 ms' │ ' 8 ms' │ ' 17.88 x' │
1245
+ Object_Unconstrained 1000 │ ' 1181 ms' │ ' 38 ms' │ ' 31.08 x' │
1246
+ Object_Constrained 1000 │ ' 1168 ms' │ ' 32 ms' │ ' 36.50 x' │
1247
+ Tuple_Primitive 1000 │ ' 557 ms' │ ' 16 ms' │ ' 34.81 x' │
1248
+ Tuple_Object 1000 │ ' 1119 ms' │ ' 17 ms' │ ' 65.82 x' │
1249
+ Composite_Intersect 1000 │ ' 569 ms' │ ' 22 ms' │ ' 25.86 x' │
1250
+ Composite_Union 1000 │ ' 513 ms' │ ' 23 ms' │ ' 22.30 x' │
1251
+ Math_Vector4 1000 │ ' 802 ms' │ ' 10 ms' │ ' 80.20 x' │
1252
+ Math_Matrix4 1000 │ ' 395 ms' │ ' 12 ms' │ ' 32.92 x' │
1253
+ Array_Primitive_Number 1000 │ ' 282 ms' │ ' 8 ms' │ ' 35.25 x' │
1254
+ Array_Primitive_String 1000 │ ' 321 ms' │ ' 5 ms' │ ' 64.20 x' │
1255
+ Array_Primitive_Boolean1000 │ ' 364 ms' │ ' 5 ms' │ ' 72.80 x' │
1256
+ Array_Object_Unconstrained 1000 │ ' 1573 ms' │ ' 18 ms' │ ' 87.39 x' │
1257
+ Array_Object_Constrained 1000 │ ' 1270 ms' │ ' 20 ms' │ ' 63.50 x' │
1258
+ Array_Tuple_Primitive 1000 │ ' 973 ms' │ ' 18 ms' │ ' 54.06 x' │
1259
+ Array_Tuple_Object 1000 │ ' 1253 ms' │ ' 16 ms' │ ' 78.31 x' │
1260
+ │ Array_Composite_Intersect │ 1000 │ ' 927 ms' │ ' 20 ms' │ ' 46.35 x' │
1261
+ │ Array_Composite_Union │ 1000 │ ' 1123 ms' │ ' 16 ms' │ ' 70.19 x' │
1262
+ │ Array_Math_Vector4 │ 1000 │ ' 1068 ms' │ ' 10 ms' │ ' 106.80 x' │
1263
+ │ Array_Math_Matrix4 │ 1000 │ ' 488 ms' │ ' 7 ms' │ ' 69.71 x' │
1264
+ └────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
1252
1265
  ```
1253
1266
 
1254
1267
  <a name='benchmark-validate'></a>
@@ -1258,35 +1271,39 @@ This benchmark measures compilation performance for varying types. You can revie
1258
1271
  This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/check.ts).
1259
1272
 
1260
1273
  ```typescript
1261
- ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
1262
- (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
1263
- ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
1264
- Number │ 1000000 │ ' 30 ms' │ ' 7 ms' │ ' 6 ms' │ ' 1.17 x' │
1265
- String │ 1000000 │ ' 23 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
1266
- Boolean │ 1000000 │ ' 22 ms' │ ' 21 ms' │ ' 10 ms' │ ' 2.10 x' │
1267
- Null │ 1000000 │ ' 27 ms' │ ' 20 ms' │ ' 10 ms' │ ' 2.00 x' │
1268
- RegEx │ 1000000 │ ' 163 ms' │ ' 47 ms' │ ' 38 ms' │ ' 1.24 x' │
1269
- ObjectA │ 1000000 │ ' 654 ms' │ ' 41 ms' │ ' 24 ms' │ ' 1.71 x' │
1270
- ObjectB │ 1000000 │ ' 1173 ms' │ ' 59 ms' │ ' 41 ms' │ ' 1.44 x' │
1271
- Tuple │ 1000000 │ ' 124 ms' │ ' 24 ms' │ ' 17 ms' │ ' 1.41 x' │
1272
- Union │ 1000000 │ ' 332 ms' │ ' 26 ms' │ ' 16 ms' │ ' 1.63 x' │
1273
- Recursive │ 1000000 │ ' 3129 ms' │ ' 412 ms' │ ' 102 ms' │ ' 4.04 x' │
1274
- Vector4 │ 1000000 │ ' 147 ms' │ ' 26 ms' │ ' 13 ms' │ ' 2.00 x' │
1275
- Matrix4 │ 1000000 │ ' 576 ms' │ ' 41 ms' │ ' 28 ms' │ ' 1.46 x' │
1276
- Literal_String │ 1000000 │ ' 51 ms' │ ' 21 ms' │ ' 10 ms' │ ' 2.10 x' │
1277
- Literal_Number │ 1000000 │ ' 47 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
1278
- Literal_Boolean │ 1000000 │ ' 47 ms' │ ' 21 ms' │ ' 10 ms' │ ' 2.10 x' │
1279
- Array_Number │ 1000000 │ ' 490 ms' │ ' 33 ms' │ ' 18 ms' │ ' 1.83 x' │
1280
- Array_String │ 1000000 │ ' 502 ms' │ ' 31 ms' │ ' 25 ms' │ ' 1.24 x' │
1281
- Array_Boolean │ 1000000 │ ' 465 ms' │ ' 33 ms' │ ' 27 ms' │ ' 1.22 x' │
1282
- Array_ObjectA │ 1000000 │ ' 15463 ms' │ ' 2470 ms' │ ' 2052 ms' │ ' 1.20 x' │
1283
- Array_ObjectB │ 1000000 │ ' 18047 ms' │ ' 2497 ms' │ ' 2348 ms' │ ' 1.06 x' │
1284
- Array_Tuple │ 1000000 │ ' 1958 ms' │ ' 99 ms' │ ' 77 ms' │ ' 1.29 x' │
1285
- Array_Union │ 1000000 │ ' 5348 ms' │ ' 254 ms' │ ' 89 ms' │ ' 2.85 x' │
1286
- Array_Recursive │ 1000000 │ ' 54643 ms' │ ' 8870 ms' │ ' 1158 ms' │ ' 7.66 x' │
1287
- Array_Vector4 │ 1000000 │ ' 2724 ms' │ ' 105 ms' │ ' 48 ms' │ ' 2.19 x' │
1288
- Array_Matrix4 │ 1000000 │ ' 13821 ms' │ ' 437 ms' │ ' 266 ms' │ ' 1.64 x' │
1289
- └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
1274
+ ┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
1275
+ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
1276
+ ├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
1277
+ Literal_String │ 1000000 │ ' 26 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
1278
+ Literal_Number │ 1000000 │ ' 21 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
1279
+ Literal_Boolean │ 1000000 │ ' 19 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
1280
+ Primitive_Number │ 1000000 │ ' 24 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
1281
+ Primitive_String │ 1000000 │ ' 26 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
1282
+ Primitive_String_Pattern │ 1000000 │ ' 159 ms' │ ' 45 ms' │ ' 37 ms' │ ' 1.22 x' │
1283
+ Primitive_Boolean │ 1000000 │ ' 23 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
1284
+ Primitive_Null │ 1000000 │ ' 23 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
1285
+ Object_Unconstrained │ 1000000 │ ' 809 ms' │ ' 35 ms' │ ' 30 ms' │ ' 1.17 x' │
1286
+ Object_Constrained │ 1000000 │ ' 1060 ms' │ ' 56 ms' │ ' 45 ms' │ ' 1.24 x' │
1287
+ Object_Recursive │ 1000000 │ ' 4965 ms' │ ' 397 ms' │ ' 100 ms' │ ' 3.97 x' │
1288
+ Tuple_Primitive │ 1000000 │ ' 159 ms' │ ' 22 ms' │ ' 16 ms' │ ' 1.38 x' │
1289
+ Tuple_Object │ 1000000 │ ' 658 ms' │ ' 31 ms' │ ' 27 ms' │ ' 1.15 x' │
1290
+ Composite_Intersect │ 1000000 │ ' 695 ms' │ ' 26 ms' │ ' 22 ms' │ ' 1.18 x' │
1291
+ Composite_Union │ 1000000 │ ' 503 ms' │ ' 24 ms' │ ' 19 ms' │ ' 1.26 x' │
1292
+ Math_Vector4 │ 1000000 │ ' 259 ms' │ ' 22 ms' │ ' 14 ms' │ ' 1.57 x' │
1293
+ Math_Matrix4 │ 1000000 │ ' 1007 ms' │ ' 40 ms' │ ' 29 ms' │ ' 1.38 x' │
1294
+ Array_Primitive_Number │ 1000000 │ ' 262 ms' │ ' 23 ms' │ ' 17 ms' │ ' 1.35 x' │
1295
+ Array_Primitive_String │ 1000000 │ ' 241 ms' │ ' 27 ms' │ ' 24 ms' │ ' 1.13 x' │
1296
+ Array_Primitive_Boolean │ 1000000 │ ' 141 ms' │ ' 23 ms' │ ' 20 ms' │ ' 1.15 x' │
1297
+ Array_Object_Unconstrained │ 1000000 │ ' 4976 ms' │ ' 70 ms' │ ' 67 ms' │ ' 1.04 x' │
1298
+ Array_Object_Constrained │ 1000000 │ ' 5234 ms' │ ' 143 ms' │ ' 120 ms' │ ' 1.19 x' │
1299
+ Array_Object_Recursive │ 1000000 │ ' 19605 ms' │ ' 1909 ms' │ ' 350 ms' │ ' 5.45 x' │
1300
+ Array_Tuple_Primitive │ 1000000 │ ' 706 ms' │ ' 39 ms' │ ' 32 ms' │ ' 1.22 x' │
1301
+ Array_Tuple_Object │ 1000000 │ ' 2951 ms' │ ' 67 ms' │ ' 63 ms' │ ' 1.06 x' │
1302
+ │ Array_Composite_Intersect │ 1000000 │ ' 2969 ms' │ ' 49 ms' │ ' 44 ms' │ ' 1.11 x' │
1303
+ │ Array_Composite_Union │ 1000000 │ ' 2191 ms' │ ' 77 ms' │ ' 41 ms' │ ' 1.88 x' │
1304
+ │ Array_Math_Vector4 │ 1000000 │ ' 1164 ms' │ ' 41 ms' │ ' 25 ms' │ ' 1.64 x' │
1305
+ │ Array_Math_Matrix4 │ 1000000 │ ' 4903 ms' │ ' 115 ms' │ ' 99 ms' │ ' 1.16 x' │
1306
+ └────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
1290
1307
  ```
1291
1308
 
1292
1309
  <a name='benchmark-compression'></a>
@@ -1299,15 +1316,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1299
1316
  ┌──────────────────────┬────────────┬────────────┬─────────────┐
1300
1317
  │ (index) │ Compiled │ Minified │ Compression │
1301
1318
  ├──────────────────────┼────────────┼────────────┼─────────────┤
1302
- │ typebox/compiler │ ' 65.4 kb' │ ' 32.2 kb' │ '2.03 x' │
1303
- │ typebox/conditional │ ' 45.5 kb' │ ' 18.6 kb' │ '2.45 x' │
1304
- │ typebox/custom │ ' 0.6 kb' │ ' 0.2 kb' │ '2.61 x' │
1305
- │ typebox/format │ ' 0.6 kb' │ ' 0.2 kb' │ '2.66 x' │
1306
- │ typebox/guard │ ' 23.8 kb' │ ' 11.4 kb' │ '2.08 x' │
1307
- │ typebox/hash │ ' 4.2 kb' │ ' 1.8 kb' │ '2.30 x' │
1308
- │ typebox/system │ ' 14.0 kb' │ ' 7.1 kb' │ '1.96 x' │
1309
- │ typebox/value │ ' 90.0 kb' │ ' 41.8 kb' │ '2.15 x' │
1310
- │ typebox │ ' 12.0 kb' │ ' 6.4 kb' │ '1.89 x' │
1319
+ │ typebox/compiler │ '108.8 kb' │ ' 48.9 kb' │ '2.23 x' │
1320
+ │ typebox/errors │ ' 93.2 kb' │ ' 41.5 kb' │ '2.24 x' │
1321
+ │ typebox/system │ ' 60.0 kb' │ ' 24.6 kb' │ '2.43 x' │
1322
+ │ typebox/value │ '153.5 kb' │ ' 66.7 kb' │ '2.30 x' │
1323
+ │ typebox │ ' 58.7 kb' │ ' 24.1 kb' │ '2.43 x' │
1311
1324
  └──────────────────────┴────────────┴────────────┴─────────────┘
1312
1325
  ```
1313
1326
 
@@ -17,4 +17,8 @@ export declare namespace TypeSystem {
17
17
  function Type<Type, Options = object>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => Types.TUnsafe<Type>;
18
18
  /** Creates a new string format */
19
19
  function Format<F extends string>(format: F, check: (value: string) => boolean): F;
20
+ /** @deprecated Use `TypeSystem.Type()` instead. */
21
+ function CreateType<Type, Options = object>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => Types.TUnsafe<Type>;
22
+ /** @deprecated Use `TypeSystem.Format()` instead. */
23
+ function CreateFormat<F extends string>(format: F, check: (value: string) => boolean): F;
20
24
  }
package/system/system.js CHANGED
@@ -31,13 +31,13 @@ exports.TypeSystem = exports.TypeSystemDuplicateFormat = exports.TypeSystemDupli
31
31
  const Types = require("../typebox");
32
32
  class TypeSystemDuplicateTypeKind extends Error {
33
33
  constructor(kind) {
34
- super(`Duplicate kind '${kind}' detected`);
34
+ super(`Duplicate type kind '${kind}' detected`);
35
35
  }
36
36
  }
37
37
  exports.TypeSystemDuplicateTypeKind = TypeSystemDuplicateTypeKind;
38
38
  class TypeSystemDuplicateFormat extends Error {
39
39
  constructor(kind) {
40
- super(`Duplicate format '${kind}' detected`);
40
+ super(`Duplicate string format '${kind}' detected`);
41
41
  }
42
42
  }
43
43
  exports.TypeSystemDuplicateFormat = TypeSystemDuplicateFormat;
@@ -66,4 +66,17 @@ var TypeSystem;
66
66
  return format;
67
67
  }
68
68
  TypeSystem.Format = Format;
69
+ // ------------------------------------------------------------------------
70
+ // Deprecated
71
+ // ------------------------------------------------------------------------
72
+ /** @deprecated Use `TypeSystem.Type()` instead. */
73
+ function CreateType(kind, check) {
74
+ return Type(kind, check);
75
+ }
76
+ TypeSystem.CreateType = CreateType;
77
+ /** @deprecated Use `TypeSystem.Format()` instead. */
78
+ function CreateFormat(format, check) {
79
+ return Format(format, check);
80
+ }
81
+ TypeSystem.CreateFormat = CreateFormat;
69
82
  })(TypeSystem = exports.TypeSystem || (exports.TypeSystem = {}));
package/typebox.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export declare const Recursive: unique symbol;
2
1
  export declare const Modifier: unique symbol;
3
2
  export declare const Hint: unique symbol;
4
3
  export declare const Kind: unique symbol;
@@ -27,7 +26,7 @@ export type TReadonlyOptional<T extends TSchema> = T & {
27
26
  export interface SchemaOptions {
28
27
  $schema?: string;
29
28
  /** Id for this schema */
30
- readonly $id?: string;
29
+ $id?: string;
31
30
  /** Title of this schema */
32
31
  title?: string;
33
32
  /** Description of this schema */
@@ -43,7 +42,6 @@ export interface TKind {
43
42
  }
44
43
  export interface TSchema extends SchemaOptions, TKind {
45
44
  [Modifier]?: string;
46
- [Recursive]?: string;
47
45
  [Hint]?: string;
48
46
  params: unknown[];
49
47
  static: unknown;
@@ -84,16 +82,17 @@ export interface TBoolean extends TSchema {
84
82
  type: 'boolean';
85
83
  }
86
84
  export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>;
87
- export type TDeref<T> = T extends TRef<infer U> ? TDeref<U> : T;
88
85
  export type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns'];
89
86
  export type TCompositeUnion<Left extends TSchema, Right extends TSchema> = Ensure<TUnion<[Left, Right]>>;
90
- export type TCompositeMerge<T extends TObject, Output extends Record<any, TSchema>> = Evaluate<{
91
- [Key in keyof T['properties']]: Key extends keyof Output ? TCompositeUnion<T['properties'][Key], Output[Key]> : T['properties'][Key];
92
- } & {
93
- [Key in keyof Output]: Key extends keyof T['properties'] ? TCompositeUnion<T['properties'][Key], Output[Key]> : Output[Key];
94
- }>;
95
- export type TCompositeReduce<T extends TObject[], Output extends {}> = T extends [infer L, ...infer R] ? TCompositeReduce<[...Assert<R, TObject[]>], TCompositeMerge<Assert<L, TObject>, Output>> : T extends [] ? Output : never;
96
- export type TComposite<T extends TObject[]> = Ensure<TObject<TCompositeReduce<T, {}>>>;
87
+ export type TCompositeUnionLeft<T extends TObject, Acc extends TProperties> = {
88
+ [K in keyof T['properties']]: K extends keyof Acc ? TCompositeUnion<T['properties'][K], Acc[K]> : T['properties'][K];
89
+ };
90
+ export type TCompositeUnionRight<T extends TObject, Acc extends TProperties> = {
91
+ [K in keyof Acc]: K extends keyof T['properties'] ? TCompositeUnion<T['properties'][K], Acc[K]> : Acc[K];
92
+ };
93
+ export type TCompositeUnionObject<T extends TObject, Acc extends TProperties> = Evaluate<TCompositeUnionLeft<T, Acc> & TCompositeUnionRight<T, Acc>>;
94
+ export type TCompositeProperties<T extends TObject[], Acc extends TProperties> = T extends [...infer L, infer R] ? TCompositeProperties<Assert<L, TObject[]>, TCompositeUnionObject<Assert<R, TObject>, Acc>> : T extends [] ? Acc : never;
95
+ export type TComposite<T extends TObject[] = TObject[]> = Ensure<TObject<TCompositeProperties<T, {}>>>;
97
96
  export type TConstructorParameterArray<T extends readonly TSchema[], P extends unknown[]> = [...{
98
97
  [K in keyof T]: Static<Assert<T[K], TSchema>, P>;
99
98
  }];
@@ -170,9 +169,9 @@ export type TKeyOfTuple<T extends TSchema> = {
170
169
  } extends infer U ? UnionToTuple<Exclude<{
171
170
  [K in keyof U]: U[K];
172
171
  }[keyof U], undefined>> : never;
173
- export type TKeyOf<T extends TSchema = TSchema, D = TDeref<T>> = (D extends TIntersect ? TKeyOfTuple<T> : D extends TUnion ? TKeyOfTuple<T> : D extends TObject ? TKeyOfTuple<T> : [
172
+ export type TKeyOf<T extends TSchema = TSchema> = (T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : [
174
173
  ]) extends infer R ? TUnionResult<Assert<R, TSchema[]>> : never;
175
- export type TLiteralValue = string | number | boolean | bigint;
174
+ export type TLiteralValue = string | number | boolean;
176
175
  export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
177
176
  [Kind]: 'Literal';
178
177
  static: T;
@@ -242,7 +241,7 @@ export type TOmitArray<T extends TSchema[], K extends keyof any> = Assert<{
242
241
  [K2 in keyof T]: TOmit<Assert<T[K2], TSchema>, K>;
243
242
  }, TSchema[]>;
244
243
  export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<Assert<Omit<T, K>, TProperties>>;
245
- export type TOmit<T extends TSchema, K extends keyof any, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : D extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : D extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : D;
244
+ export type TOmit<T extends TSchema, K extends keyof any> = 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;
246
245
  export type TParameters<T extends TFunction> = TTuple<T['parameters']>;
247
246
  export type TPartialArray<T extends TSchema[]> = Assert<{
248
247
  [K in keyof T]: TPartial<Assert<T[K], TSchema>>;
@@ -250,14 +249,14 @@ export type TPartialArray<T extends TSchema[]> = Assert<{
250
249
  export type TPartialProperties<T extends TProperties> = Evaluate<Assert<{
251
250
  [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]>;
252
251
  }, TProperties>>;
253
- export type TPartial<T extends TSchema, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : D extends TUnion<infer S> ? TUnion<TPartialArray<S>> : D extends TObject<infer S> ? TObject<TPartialProperties<S>> : D;
252
+ export type TPartial<T extends TSchema> = 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;
254
253
  export type TPickArray<T extends TSchema[], K extends keyof any> = {
255
254
  [K2 in keyof T]: TPick<Assert<T[K2], TSchema>, K>;
256
255
  };
257
256
  export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
258
257
  [K in keyof R]: Assert<R[K], TSchema> extends TSchema ? R[K] : never;
259
258
  }) : never;
260
- export type TPick<T extends TSchema, K extends keyof any, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : D extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : D extends TObject<infer S> ? TObject<TPickProperties<S, K>> : D;
259
+ export type TPick<T extends TSchema, K extends keyof any> = 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;
261
260
  export type TPromiseStatic<T extends TSchema, P extends unknown[]> = Promise<Static<T, P>>;
262
261
  export interface TPromise<T extends TSchema = TSchema> extends TSchema {
263
262
  [Kind]: 'Promise';
@@ -305,7 +304,7 @@ export type TRequiredArray<T extends TSchema[]> = Assert<{
305
304
  export type TRequiredProperties<T extends TProperties> = Evaluate<Assert<{
306
305
  [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];
307
306
  }, TProperties>>;
308
- export type TRequired<T extends TSchema, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TRequiredArray<S>> : D extends TUnion<infer S> ? TUnion<TRequiredArray<S>> : D extends TObject<infer S> ? TObject<TRequiredProperties<S>> : D;
307
+ export type TRequired<T extends TSchema> = T extends TIntersect<infer S> ? TIntersect<TRequiredArray<S>> : T extends TUnion<infer S> ? TUnion<TRequiredArray<S>> : T extends TObject<infer S> ? TObject<TRequiredProperties<S>> : T;
309
308
  export type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex';
310
309
  export interface StringOptions<Format extends string> extends SchemaOptions {
311
310
  minLength?: number;
@@ -385,12 +384,15 @@ export interface TVoid extends TSchema {
385
384
  type: 'null';
386
385
  typeOf: 'Void';
387
386
  }
388
- /** Creates a static type from a TypeBox type */
387
+ /** Creates a TypeScript static type from a TypeBox type */
389
388
  export type Static<T extends TSchema, P extends unknown[] = []> = (T & {
390
389
  params: P;
391
390
  })['static'];
392
391
  export type TypeRegistryValidationFunction<TSchema> = (schema: TSchema, value: unknown) => boolean;
392
+ /** A registry for user defined types */
393
393
  export declare namespace TypeRegistry {
394
+ /** Returns the entries in this registry */
395
+ function Entries(): Map<string, TypeRegistryValidationFunction<any>>;
394
396
  /** Clears all user defined types */
395
397
  function Clear(): void;
396
398
  /** Returns true if this registry contains this kind */
@@ -400,21 +402,11 @@ export declare namespace TypeRegistry {
400
402
  /** Gets a custom validation function for a user defined type */
401
403
  function Get(kind: string): TypeRegistryValidationFunction<any> | undefined;
402
404
  }
403
- export declare namespace ReferenceRegistry {
404
- /** Clears the reference registry */
405
- function Clear(): void;
406
- /** Returns true if this registry contains this schema */
407
- function Has(schema: TSchema): boolean;
408
- /** Sets this schema on this registry if a $id exists */
409
- function Set(schema: TSchema): void;
410
- /** Dereferences the schema one level deep */
411
- function DerefOne(schema: TSchema): TSchema;
412
- /** Dereferences the schema recursively */
413
- function Deref(schema: TSchema): TSchema;
414
- }
415
405
  export type FormatRegistryValidationFunction = (value: string) => boolean;
416
- /** Provides functions to create user defined string formats */
406
+ /** A registry for user defined string formats */
417
407
  export declare namespace FormatRegistry {
408
+ /** Returns the entries in this registry */
409
+ function Entries(): Map<string, FormatRegistryValidationFunction>;
418
410
  /** Clears all user defined string formats */
419
411
  function Clear(): void;
420
412
  /** Returns true if the user defined string format exists */
@@ -428,6 +420,7 @@ export declare class TypeGuardUnknownTypeError extends Error {
428
420
  readonly schema: unknown;
429
421
  constructor(schema: unknown);
430
422
  }
423
+ /** Provides functions to test if JavaScript values are TypeBox types */
431
424
  export declare namespace TypeGuard {
432
425
  /** Returns true if the given schema is TAny */
433
426
  function TAny(schema: unknown): schema is TAny;
@@ -496,8 +489,8 @@ export declare namespace TypeGuard {
496
489
  /** Returns true if the given schema is TSchema */
497
490
  function TSchema(schema: unknown): schema is TSchema;
498
491
  }
492
+ /** Fast undefined check used for properties of type undefined */
499
493
  export declare namespace ExtendsUndefined {
500
- /** Fast undefined check for properties of type undefined */
501
494
  function Check(schema: TSchema): boolean;
502
495
  }
503
496
  export declare enum TypeExtendsResult {
@@ -508,7 +501,7 @@ export declare enum TypeExtendsResult {
508
501
  export declare namespace TypeExtends {
509
502
  function Extends(left: TSchema, right: TSchema): TypeExtendsResult;
510
503
  }
511
- /** Specialized Clone for Types. Clones and removes non self-referential identifiers */
504
+ /** Specialized Clone for Types */
512
505
  export declare namespace TypeClone {
513
506
  /** Clones a type. This function will omit non-self referential identifiers on the cloned type. */
514
507
  function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T;
@@ -538,10 +531,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
538
531
  Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>;
539
532
  /** `[Standard]` Creates a Boolean type */
540
533
  Boolean(options?: SchemaOptions): TBoolean;
541
- /** `[Standard]` Creates a Composite object type that will Union any overlapping properties of the given Object array */
534
+ /** `[Standard]` Creates a Composite object type that will union any overlapping properties of the given object array */
542
535
  Composite<T extends TObject[]>(schemas: [...T], options?: ObjectOptions): TComposite<T>;
543
- /** `[Standard]` Dereferences the given TRef to its target type */
544
- Deref<T extends TRef>(schema: T): TDeref<T>;
545
536
  /** `[Standard]` Creates a Enum type */
546
537
  Enum<T extends Record<string, string | number>>(item: T, options?: SchemaOptions): TEnum<T>;
547
538
  /** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */