@sinclair/typebox 0.33.22 → 0.34.1
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/build/cjs/compiler/compiler.d.ts +2 -2
- package/build/cjs/compiler/compiler.js +13 -2
- package/build/cjs/errors/errors.js +7 -0
- package/build/cjs/index.d.ts +1 -2
- package/build/cjs/index.js +1 -2
- package/build/cjs/syntax/parse.d.ts +14 -15
- package/build/cjs/syntax/parse.js +6 -5
- package/build/cjs/syntax/runtime.d.ts +20 -4
- package/build/cjs/syntax/runtime.js +183 -28
- package/build/cjs/syntax/static.d.ts +107 -23
- package/build/cjs/type/array/array.d.ts +1 -1
- package/build/cjs/type/awaited/awaited.d.ts +8 -3
- package/build/cjs/type/awaited/awaited.js +20 -20
- package/build/cjs/type/computed/computed.d.ts +9 -0
- package/build/cjs/type/computed/computed.js +10 -0
- package/build/cjs/type/computed/index.d.ts +1 -0
- package/build/cjs/type/{deref → computed}/index.js +1 -1
- package/build/cjs/type/guard/kind.d.ts +12 -4
- package/build/cjs/type/guard/kind.js +15 -0
- package/build/cjs/type/guard/type.d.ts +9 -3
- package/build/cjs/type/guard/type.js +19 -0
- package/build/cjs/type/index.d.ts +1 -2
- package/build/cjs/type/index.js +1 -2
- package/build/cjs/type/indexed/indexed-from-mapped-key.d.ts +6 -6
- package/build/cjs/type/indexed/indexed-from-mapped-key.js +10 -10
- package/build/cjs/type/indexed/indexed-from-mapped-result.d.ts +5 -5
- package/build/cjs/type/indexed/indexed-from-mapped-result.js +11 -10
- package/build/cjs/type/indexed/indexed-property-keys.d.ts +6 -6
- package/build/cjs/type/indexed/indexed-property-keys.js +14 -14
- package/build/cjs/type/indexed/indexed.d.ts +34 -20
- package/build/cjs/type/indexed/indexed.js +59 -46
- package/build/cjs/type/intersect/intersect-evaluated.d.ts +6 -6
- package/build/cjs/type/intersect/intersect-evaluated.js +16 -16
- package/build/cjs/type/intersect/intersect.d.ts +2 -2
- package/build/cjs/type/intersect/intersect.js +6 -6
- package/build/cjs/type/keyof/keyof-from-mapped-result.d.ts +6 -6
- package/build/cjs/type/keyof/keyof-from-mapped-result.js +10 -10
- package/build/cjs/type/keyof/keyof-property-keys.d.ts +7 -7
- package/build/cjs/type/keyof/keyof-property-keys.js +22 -22
- package/build/cjs/type/keyof/keyof.d.ts +11 -6
- package/build/cjs/type/keyof/keyof.js +22 -13
- package/build/cjs/type/module/compute.d.ts +54 -0
- package/build/cjs/type/module/compute.js +154 -0
- package/build/cjs/type/module/index.d.ts +1 -0
- package/build/cjs/type/{strict → module}/index.js +1 -1
- package/build/cjs/type/module/infer.d.ts +43 -0
- package/build/cjs/type/module/infer.js +3 -0
- package/build/cjs/type/module/module.d.ts +27 -0
- package/build/cjs/type/module/module.js +37 -0
- package/build/cjs/type/not/not.d.ts +1 -1
- package/build/cjs/type/not/not.js +2 -2
- package/build/cjs/type/omit/omit-from-mapped-key.d.ts +6 -6
- package/build/cjs/type/omit/omit-from-mapped-key.js +10 -12
- package/build/cjs/type/omit/omit-from-mapped-result.d.ts +5 -5
- package/build/cjs/type/omit/omit-from-mapped-result.js +10 -10
- package/build/cjs/type/omit/omit.d.ts +26 -14
- package/build/cjs/type/omit/omit.js +46 -35
- package/build/cjs/type/partial/partial.d.ts +11 -7
- package/build/cjs/type/partial/partial.js +36 -24
- package/build/cjs/type/pick/pick-from-mapped-key.d.ts +6 -6
- package/build/cjs/type/pick/pick-from-mapped-key.js +10 -10
- package/build/cjs/type/pick/pick-from-mapped-result.d.ts +5 -5
- package/build/cjs/type/pick/pick-from-mapped-result.js +10 -10
- package/build/cjs/type/pick/pick.d.ts +25 -15
- package/build/cjs/type/pick/pick.js +47 -36
- package/build/cjs/type/record/record.d.ts +4 -2
- package/build/cjs/type/record/record.js +31 -28
- package/build/cjs/type/ref/ref.d.ts +4 -7
- package/build/cjs/type/ref/ref.js +3 -11
- package/build/cjs/type/required/required.d.ts +12 -8
- package/build/cjs/type/required/required.js +37 -25
- package/build/cjs/type/static/static.d.ts +1 -1
- package/build/cjs/type/tuple/tuple.d.ts +1 -1
- package/build/cjs/type/tuple/tuple.js +4 -4
- package/build/cjs/type/type/javascript.d.ts +10 -10
- package/build/cjs/type/type/javascript.js +6 -6
- package/build/cjs/type/type/json.d.ts +45 -68
- package/build/cjs/type/type/json.js +82 -98
- package/build/cjs/type/type/type.d.ts +1 -2
- package/build/cjs/type/type/type.js +55 -57
- package/build/cjs/type/union/union-evaluated.d.ts +6 -6
- package/build/cjs/type/union/union-evaluated.js +11 -10
- package/build/cjs/type/union/union.d.ts +1 -1
- package/build/cjs/type/union/union.js +4 -4
- package/build/cjs/value/cast/cast.js +8 -1
- package/build/cjs/value/check/check.js +11 -4
- package/build/cjs/value/clean/clean.js +8 -1
- package/build/cjs/value/convert/convert.js +7 -0
- package/build/cjs/value/create/create.js +7 -0
- package/build/cjs/value/default/default.js +7 -0
- package/build/cjs/value/transform/decode.js +18 -7
- package/build/cjs/value/transform/encode.js +18 -7
- package/build/cjs/value/transform/has.js +18 -18
- package/build/esm/compiler/compiler.d.mts +2 -2
- package/build/esm/compiler/compiler.mjs +13 -2
- package/build/esm/errors/errors.mjs +7 -0
- package/build/esm/index.d.mts +1 -2
- package/build/esm/index.mjs +1 -2
- package/build/esm/syntax/parse.d.mts +14 -15
- package/build/esm/syntax/parse.mjs +6 -5
- package/build/esm/syntax/runtime.d.mts +20 -4
- package/build/esm/syntax/runtime.mjs +183 -28
- package/build/esm/syntax/static.d.mts +107 -23
- package/build/esm/type/array/array.d.mts +1 -1
- package/build/esm/type/awaited/awaited.d.mts +8 -3
- package/build/esm/type/awaited/awaited.mjs +20 -20
- package/build/esm/type/computed/computed.d.mts +9 -0
- package/build/esm/type/computed/computed.mjs +6 -0
- package/build/esm/type/computed/index.d.mts +1 -0
- package/build/esm/type/computed/index.mjs +1 -0
- package/build/esm/type/guard/kind.d.mts +12 -4
- package/build/esm/type/guard/kind.mjs +12 -0
- package/build/esm/type/guard/type.d.mts +9 -3
- package/build/esm/type/guard/type.mjs +17 -0
- package/build/esm/type/index.d.mts +1 -2
- package/build/esm/type/index.mjs +1 -2
- package/build/esm/type/indexed/indexed-from-mapped-key.d.mts +6 -6
- package/build/esm/type/indexed/indexed-from-mapped-key.mjs +10 -10
- package/build/esm/type/indexed/indexed-from-mapped-result.d.mts +5 -5
- package/build/esm/type/indexed/indexed-from-mapped-result.mjs +11 -10
- package/build/esm/type/indexed/indexed-property-keys.d.mts +6 -6
- package/build/esm/type/indexed/indexed-property-keys.mjs +14 -14
- package/build/esm/type/indexed/indexed.d.mts +34 -20
- package/build/esm/type/indexed/indexed.mjs +55 -42
- package/build/esm/type/intersect/intersect-evaluated.d.mts +6 -6
- package/build/esm/type/intersect/intersect-evaluated.mjs +16 -16
- package/build/esm/type/intersect/intersect.d.mts +2 -2
- package/build/esm/type/intersect/intersect.mjs +6 -6
- package/build/esm/type/keyof/keyof-from-mapped-result.d.mts +6 -6
- package/build/esm/type/keyof/keyof-from-mapped-result.mjs +10 -10
- package/build/esm/type/keyof/keyof-property-keys.d.mts +7 -7
- package/build/esm/type/keyof/keyof-property-keys.mjs +22 -22
- package/build/esm/type/keyof/keyof.d.mts +11 -6
- package/build/esm/type/keyof/keyof.mjs +22 -13
- package/build/esm/type/module/compute.d.mts +54 -0
- package/build/esm/type/module/compute.mjs +148 -0
- package/build/esm/type/module/index.d.mts +1 -0
- package/build/esm/type/module/index.mjs +1 -0
- package/build/esm/type/module/infer.d.mts +43 -0
- package/build/esm/type/module/infer.mjs +1 -0
- package/build/esm/type/module/module.d.mts +27 -0
- package/build/esm/type/module/module.mjs +31 -0
- package/build/esm/type/not/not.d.mts +1 -1
- package/build/esm/type/not/not.mjs +2 -2
- package/build/esm/type/omit/omit-from-mapped-key.d.mts +6 -6
- package/build/esm/type/omit/omit-from-mapped-key.mjs +10 -12
- package/build/esm/type/omit/omit-from-mapped-result.d.mts +5 -5
- package/build/esm/type/omit/omit-from-mapped-result.mjs +10 -10
- package/build/esm/type/omit/omit.d.mts +26 -14
- package/build/esm/type/omit/omit.mjs +43 -32
- package/build/esm/type/partial/partial.d.mts +11 -7
- package/build/esm/type/partial/partial.mjs +29 -17
- package/build/esm/type/pick/pick-from-mapped-key.d.mts +6 -6
- package/build/esm/type/pick/pick-from-mapped-key.mjs +10 -10
- package/build/esm/type/pick/pick-from-mapped-result.d.mts +5 -5
- package/build/esm/type/pick/pick-from-mapped-result.mjs +10 -10
- package/build/esm/type/pick/pick.d.mts +25 -15
- package/build/esm/type/pick/pick.mjs +43 -32
- package/build/esm/type/record/record.d.mts +4 -2
- package/build/esm/type/record/record.mjs +15 -12
- package/build/esm/type/ref/ref.d.mts +4 -7
- package/build/esm/type/ref/ref.mjs +3 -11
- package/build/esm/type/required/required.d.mts +12 -8
- package/build/esm/type/required/required.mjs +33 -21
- package/build/esm/type/static/static.d.mts +1 -1
- package/build/esm/type/tuple/tuple.d.mts +1 -1
- package/build/esm/type/tuple/tuple.mjs +4 -4
- package/build/esm/type/type/javascript.d.mts +10 -10
- package/build/esm/type/type/javascript.mjs +6 -6
- package/build/esm/type/type/json.d.mts +45 -68
- package/build/esm/type/type/json.mjs +47 -63
- package/build/esm/type/type/type.d.mts +1 -2
- package/build/esm/type/type/type.mjs +1 -2
- package/build/esm/type/union/union-evaluated.d.mts +6 -6
- package/build/esm/type/union/union-evaluated.mjs +11 -10
- package/build/esm/type/union/union.d.mts +1 -1
- package/build/esm/type/union/union.mjs +4 -4
- package/build/esm/value/cast/cast.mjs +9 -2
- package/build/esm/value/check/check.mjs +11 -4
- package/build/esm/value/clean/clean.mjs +9 -2
- package/build/esm/value/convert/convert.mjs +7 -0
- package/build/esm/value/create/create.mjs +7 -0
- package/build/esm/value/default/default.mjs +7 -0
- package/build/esm/value/transform/decode.mjs +13 -2
- package/build/esm/value/transform/encode.mjs +13 -2
- package/build/esm/value/transform/has.mjs +2 -2
- package/package.json +1 -1
- package/readme.md +230 -97
- package/build/cjs/type/deref/deref.d.ts +0 -22
- package/build/cjs/type/deref/deref.js +0 -106
- package/build/cjs/type/deref/index.d.ts +0 -1
- package/build/cjs/type/strict/index.d.ts +0 -1
- package/build/cjs/type/strict/strict.d.ts +0 -12
- package/build/cjs/type/strict/strict.js +0 -16
- package/build/esm/type/deref/deref.d.mts +0 -22
- package/build/esm/type/deref/deref.mjs +0 -102
- package/build/esm/type/deref/index.d.mts +0 -1
- package/build/esm/type/deref/index.mjs +0 -1
- package/build/esm/type/strict/index.d.mts +0 -1
- package/build/esm/type/strict/index.mjs +0 -1
- package/build/esm/type/strict/strict.d.mts +0 -12
- package/build/esm/type/strict/strict.mjs +0 -12
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="https://
|
|
7
|
+
<img src="https://raw.githubusercontent.com/sinclairzx81/typebox/refs/heads/master/typebox.png" />
|
|
8
8
|
|
|
9
9
|
<br />
|
|
10
10
|
<br />
|
|
@@ -68,8 +68,7 @@ License MIT
|
|
|
68
68
|
- [Options](#types-options)
|
|
69
69
|
- [Properties](#types-properties)
|
|
70
70
|
- [Generics](#types-generics)
|
|
71
|
-
- [
|
|
72
|
-
- [Recursive](#types-recursive)
|
|
71
|
+
- [Modules](#types-modules)
|
|
73
72
|
- [Template Literal](#types-template-literal)
|
|
74
73
|
- [Indexed](#types-indexed)
|
|
75
74
|
- [Mapped](#types-mapped)
|
|
@@ -77,6 +76,13 @@ License MIT
|
|
|
77
76
|
- [Transform](#types-transform)
|
|
78
77
|
- [Guard](#types-guard)
|
|
79
78
|
- [Unsafe](#types-unsafe)
|
|
79
|
+
- [Syntax](#syntax)
|
|
80
|
+
- [Parse](#syntax-parse)
|
|
81
|
+
- [Compose](#syntax-compose)
|
|
82
|
+
- [Context](#syntax-context)
|
|
83
|
+
- [Module](#syntax-module)
|
|
84
|
+
- [Static](#syntax-static)
|
|
85
|
+
- [Limits](#syntax-limits)
|
|
80
86
|
- [Values](#values)
|
|
81
87
|
- [Assert](#values-assert)
|
|
82
88
|
- [Create](#values-create)
|
|
@@ -764,103 +770,41 @@ const T = Nullable(Type.String()) // const T = {
|
|
|
764
770
|
type T = Static<typeof T> // type T = string | null
|
|
765
771
|
```
|
|
766
772
|
|
|
767
|
-
<a name='types-
|
|
773
|
+
<a name='types-modules'></a>
|
|
768
774
|
|
|
769
|
-
###
|
|
775
|
+
### Module Types
|
|
770
776
|
|
|
771
|
-
|
|
777
|
+
TypeBox Modules are containers for related types. They function as namespaces and enable internal types to reference each other via string references. Modules support both singular and mutually recursive types. They provide a mechanism to create circular types irrespective of the order in which types are defined.
|
|
772
778
|
|
|
773
779
|
```typescript
|
|
774
|
-
const Vector = Type.Object({ // const Vector = {
|
|
775
|
-
x: Type.Number(), // type: 'object',
|
|
776
|
-
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
777
|
-
}, { $id: 'Vector' }) // properties: {
|
|
778
|
-
// x: { type: 'number' },
|
|
779
|
-
// y: { type: 'number' }
|
|
780
|
-
// },
|
|
781
|
-
// $id: 'Vector'
|
|
782
|
-
// }
|
|
783
|
-
|
|
784
|
-
const VectorRef = Type.Ref(Vector) // const VectorRef = {
|
|
785
|
-
// $ref: 'Vector'
|
|
786
|
-
// }
|
|
787
|
-
|
|
788
|
-
type VectorRef = Static<typeof VectorRef> // type VectorRef = {
|
|
789
|
-
// x: number,
|
|
790
|
-
// y: number
|
|
791
|
-
// }
|
|
792
|
-
```
|
|
793
|
-
Use Deref to dereference a type. This function will replace any interior reference with the target type.
|
|
794
|
-
```typescript
|
|
795
|
-
const Vertex = Type.Object({ // const Vertex = {
|
|
796
|
-
position: VectorRef, // type: 'object',
|
|
797
|
-
texcoord: VectorRef, // required: ['position', 'texcoord'],
|
|
798
|
-
}) // properties: {
|
|
799
|
-
// position: { $ref: 'Vector' },
|
|
800
|
-
// texcoord: { $ref: 'Vector' }
|
|
801
|
-
// }
|
|
802
|
-
// }
|
|
803
780
|
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
// type: 'object',
|
|
818
|
-
// required: ['x', 'y', 'z'],
|
|
819
|
-
// properties: {
|
|
820
|
-
// x: { type: 'number' },
|
|
821
|
-
// y: { type: 'number' }
|
|
822
|
-
// }
|
|
823
|
-
// }
|
|
824
|
-
// }
|
|
825
|
-
// }
|
|
826
|
-
```
|
|
827
|
-
Note that Ref types do not store structural information about the type they're referencing. Because of this, these types cannot be used with some mapping types (such as Partial or Pick). For applications that require mapping on Ref, use Deref to normalize the type first.
|
|
828
|
-
|
|
829
|
-
<a name='types-recursive'></a>
|
|
781
|
+
// The following creates a Module of circular recursive Types.
|
|
782
|
+
|
|
783
|
+
const Module = Type.Module({
|
|
784
|
+
A: Type.Object({
|
|
785
|
+
b: Type.Ref('B') // Ref B:
|
|
786
|
+
}),
|
|
787
|
+
B: Type.Object({
|
|
788
|
+
c: Type.Ref('C') // Ref C:
|
|
789
|
+
}),
|
|
790
|
+
C: Type.Object({
|
|
791
|
+
a: Type.Ref('A') // Ref A:
|
|
792
|
+
}),
|
|
793
|
+
})
|
|
830
794
|
|
|
831
|
-
|
|
795
|
+
// Module Types must be imported before use.
|
|
832
796
|
|
|
833
|
-
|
|
797
|
+
const A = Module.Import('A') // const A: TImport<{...}, 'A'>
|
|
834
798
|
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
// type: 'array',
|
|
845
|
-
// items: {
|
|
846
|
-
// $ref: 'Node'
|
|
847
|
-
// }
|
|
848
|
-
// }
|
|
849
|
-
// },
|
|
850
|
-
// required: [
|
|
851
|
-
// 'id',
|
|
852
|
-
// 'nodes'
|
|
853
|
-
// ]
|
|
854
|
-
// }
|
|
855
|
-
|
|
856
|
-
type Node = Static<typeof Node> // type Node = {
|
|
857
|
-
// id: string
|
|
858
|
-
// nodes: Node[]
|
|
859
|
-
// }
|
|
860
|
-
|
|
861
|
-
function test(node: Node) {
|
|
862
|
-
const id = node.nodes[0].nodes[0].id // id is string
|
|
863
|
-
}
|
|
799
|
+
type A = Static<typeof A> // type A = {
|
|
800
|
+
// b: {
|
|
801
|
+
// c: {
|
|
802
|
+
// a: {
|
|
803
|
+
// b: ...
|
|
804
|
+
// }
|
|
805
|
+
// }
|
|
806
|
+
// }
|
|
807
|
+
// }
|
|
864
808
|
```
|
|
865
809
|
|
|
866
810
|
<a name='types-template-literal'></a>
|
|
@@ -1077,6 +1021,195 @@ if(TypeGuard.IsString(T)) {
|
|
|
1077
1021
|
}
|
|
1078
1022
|
```
|
|
1079
1023
|
|
|
1024
|
+
<a name='syntax'></a>
|
|
1025
|
+
|
|
1026
|
+
## Syntax Types
|
|
1027
|
+
|
|
1028
|
+
TypeBox provides support for Syntax Types, enabling it to parse TypeScript syntax directly into TypeBox types. Syntax Types serve as a DSL frontend for TypeBox's type builder and are useful for converting existing TypeScript type definitions into Json Schema schematics.
|
|
1029
|
+
|
|
1030
|
+
Syntax Types are provided via optional import.
|
|
1031
|
+
|
|
1032
|
+
```typescript
|
|
1033
|
+
import { Parse } from '@sinclair/typebox/syntax'
|
|
1034
|
+
```
|
|
1035
|
+
|
|
1036
|
+
<a name='syntax-parse'></a>
|
|
1037
|
+
|
|
1038
|
+
### Parse
|
|
1039
|
+
|
|
1040
|
+
Use the Parse function to convert a TypeScript string into a TypeBox type. TypeBox will infer the appropriate TSchema type or return undefined if there is a syntax error.
|
|
1041
|
+
|
|
1042
|
+
```typescript
|
|
1043
|
+
const A = Parse('string') // const A: TString
|
|
1044
|
+
|
|
1045
|
+
const B = Parse('[1, 2, 3]') // const B: TTuple<[
|
|
1046
|
+
// TLiteral<1>,
|
|
1047
|
+
// TLiteral<2>,
|
|
1048
|
+
// TLiteral<3>
|
|
1049
|
+
// ]>
|
|
1050
|
+
|
|
1051
|
+
const C = Parse(`{ x: number, y: number }`) // const C: TObject<{
|
|
1052
|
+
// x: TNumber
|
|
1053
|
+
// y: TNumber
|
|
1054
|
+
// }>
|
|
1055
|
+
```
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
<a name='syntax-compose'></a>
|
|
1060
|
+
|
|
1061
|
+
### Compose
|
|
1062
|
+
|
|
1063
|
+
Syntax Types are designed to be interchangeable with standard Types.
|
|
1064
|
+
|
|
1065
|
+
```typescript
|
|
1066
|
+
const T = Type.Object({ // const T: TObject<{
|
|
1067
|
+
x: Parse('number'), // x: TNumber,
|
|
1068
|
+
y: Parse('number'), // y: TNumber,
|
|
1069
|
+
z: Parse('number') // z: TNumber
|
|
1070
|
+
}) // }>
|
|
1071
|
+
```
|
|
1072
|
+
|
|
1073
|
+
<a name='syntax-module'></a>
|
|
1074
|
+
|
|
1075
|
+
### Module
|
|
1076
|
+
|
|
1077
|
+
Syntax Types also support Module parsing. This can provide a more terse syntax for creating Module definitions, but comes with an inference performance cost. Module parsing supports interface and type alias definitions. Generics types are currently unsupported.
|
|
1078
|
+
|
|
1079
|
+
```typescript
|
|
1080
|
+
const Module = Parse(`module {
|
|
1081
|
+
|
|
1082
|
+
export interface User {
|
|
1083
|
+
id: string
|
|
1084
|
+
name: string
|
|
1085
|
+
email: string
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
export type PartialUser = (
|
|
1089
|
+
Pick<User, 'id'> &
|
|
1090
|
+
Partial<Omit<User, 'id'>>
|
|
1091
|
+
)
|
|
1092
|
+
|
|
1093
|
+
}`)
|
|
1094
|
+
|
|
1095
|
+
const PartialUser = Module.Import('PartialUser') // TImport<{...}, 'PartialUser'>
|
|
1096
|
+
|
|
1097
|
+
type PartialUser = Static<typeof PartialUser> // type PartialUser = {
|
|
1098
|
+
// id: string,
|
|
1099
|
+
// } & {
|
|
1100
|
+
// name?: string,
|
|
1101
|
+
// email?: string,
|
|
1102
|
+
// }
|
|
1103
|
+
```
|
|
1104
|
+
|
|
1105
|
+
<a name='syntax-context'></a>
|
|
1106
|
+
|
|
1107
|
+
### Context
|
|
1108
|
+
|
|
1109
|
+
The Parse function accepts an initial Context argument, allowing external types to be passed into the parser.
|
|
1110
|
+
|
|
1111
|
+
```typescript
|
|
1112
|
+
const T = Type.Object({ // could be written as: Parse(`{
|
|
1113
|
+
x: Type.Number(), // x: number,
|
|
1114
|
+
y: Type.Number(), // y: number,
|
|
1115
|
+
z: Type.Number() // z: number
|
|
1116
|
+
}) // }`)
|
|
1117
|
+
|
|
1118
|
+
const A = Parse({ T }, 'Partial<T>') // const A: TObject<{
|
|
1119
|
+
// x: TOptional<TNumber>,
|
|
1120
|
+
// y: TOptional<TNumber>,
|
|
1121
|
+
// z: TOptional<TNumber>
|
|
1122
|
+
// }>
|
|
1123
|
+
|
|
1124
|
+
const B = Parse({ T }, 'keyof T') // const B: TUnion<[
|
|
1125
|
+
// TLiteral<'x'>,
|
|
1126
|
+
// TLiteral<'y'>,
|
|
1127
|
+
// TLiteral<'z'>
|
|
1128
|
+
// ]>
|
|
1129
|
+
|
|
1130
|
+
const C = Parse({ T }, 'T & { w: number }') // const C: TIntersect<[TObject<{
|
|
1131
|
+
// x: TNumber;
|
|
1132
|
+
// y: TNumber;
|
|
1133
|
+
// z: TNumber;
|
|
1134
|
+
// }>, TObject<{
|
|
1135
|
+
// w: TNumber;
|
|
1136
|
+
// }>]>
|
|
1137
|
+
```
|
|
1138
|
+
|
|
1139
|
+
<a name='syntax-static'></a>
|
|
1140
|
+
|
|
1141
|
+
### Static
|
|
1142
|
+
|
|
1143
|
+
Syntax Types provide two Static types for inferring TypeScript syntax from strings.
|
|
1144
|
+
|
|
1145
|
+
```typescript
|
|
1146
|
+
import { StaticParseAsSchema, StaticParseAsType } from '@sinclair/typebox/syntax'
|
|
1147
|
+
|
|
1148
|
+
// Will infer as a TSchema
|
|
1149
|
+
|
|
1150
|
+
type S = StaticParseAsSchema<{}, '{ x: number }'> // type S: TObject<{
|
|
1151
|
+
// x: TNumber
|
|
1152
|
+
// }>
|
|
1153
|
+
|
|
1154
|
+
// Will infer as a type
|
|
1155
|
+
|
|
1156
|
+
type T = StaticParseAsType<{}, '{ x: number }'> // type T = {
|
|
1157
|
+
// x: number
|
|
1158
|
+
//
|
|
1159
|
+
```
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
<a name='syntax-limits'></a>
|
|
1163
|
+
|
|
1164
|
+
### Limitations
|
|
1165
|
+
|
|
1166
|
+
Syntax Types work by having TypeBox parse TypeScript syntax within the TypeScript type system. This approach can place some strain on the TypeScript compiler and language service, potentially affecting responsiveness. While TypeBox makes a best-effort attempt to optimize for Syntax Types, users should be mindful of the following structures:
|
|
1167
|
+
|
|
1168
|
+
```typescript
|
|
1169
|
+
// Excessively wide structures will result in instantiation limits exceeding
|
|
1170
|
+
const A = Parse(`[
|
|
1171
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1172
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1173
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1174
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1175
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1176
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1177
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1178
|
+
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1179
|
+
]`)
|
|
1180
|
+
|
|
1181
|
+
// Excessively nested structures will result in instantiation limits exceeding
|
|
1182
|
+
const B = Parse(`{
|
|
1183
|
+
x: {
|
|
1184
|
+
y: {
|
|
1185
|
+
z: {
|
|
1186
|
+
w: 1 <-- Type instantiation is excessively deep and possibly infinite.
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
}`)
|
|
1191
|
+
```
|
|
1192
|
+
|
|
1193
|
+
In cases where Syntax Types busts through TypeScript instantiation limits, TypeBox offers a fallback ParseOnly function which will Parse the types at runtime, but not infer the type. This function can also be used for parsing non-constant strings.
|
|
1194
|
+
|
|
1195
|
+
```typescript
|
|
1196
|
+
import { ParseOnly } from '@sinclair/typebox/syntax'
|
|
1197
|
+
|
|
1198
|
+
// Where A is TSchema | undefined
|
|
1199
|
+
|
|
1200
|
+
const A = ParseOnly(`{
|
|
1201
|
+
x: {
|
|
1202
|
+
y: {
|
|
1203
|
+
z: {
|
|
1204
|
+
w: 1
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
}`)
|
|
1209
|
+
```
|
|
1210
|
+
|
|
1211
|
+
For more information on TypeBox's parsing infrastructure, refer to the [ParseBox](https://github.com/sinclairzx81/parsebox) project.
|
|
1212
|
+
|
|
1080
1213
|
<a name='values'></a>
|
|
1081
1214
|
|
|
1082
1215
|
## Values
|
|
@@ -1760,12 +1893,12 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1760
1893
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1761
1894
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1762
1895
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1763
|
-
│ typebox/compiler │ '
|
|
1764
|
-
│ typebox/errors │ '
|
|
1765
|
-
│ typebox/
|
|
1896
|
+
│ typebox/compiler │ '122.4 kb' │ ' 53.4 kb' │ '2.29 x' │
|
|
1897
|
+
│ typebox/errors │ ' 67.6 kb' │ ' 29.6 kb' │ '2.28 x' │
|
|
1898
|
+
│ typebox/syntax │ '132.9 kb' │ ' 54.2 kb' │ '2.45 x' │
|
|
1766
1899
|
│ typebox/system │ ' 7.4 kb' │ ' 3.2 kb' │ '2.33 x' │
|
|
1767
|
-
│ typebox/value │ '
|
|
1768
|
-
│ typebox │ '
|
|
1900
|
+
│ typebox/value │ '150.1 kb' │ ' 62.2 kb' │ '2.41 x' │
|
|
1901
|
+
│ typebox │ '106.8 kb' │ ' 43.2 kb' │ '2.47 x' │
|
|
1769
1902
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1770
1903
|
```
|
|
1771
1904
|
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { TSchema } from '../schema/index';
|
|
2
|
-
import type { Evaluate } from '../helpers/index';
|
|
3
|
-
import type { TTuple } from '../tuple/index';
|
|
4
|
-
import type { TIntersect } from '../intersect/index';
|
|
5
|
-
import type { TUnion } from '../union/index';
|
|
6
|
-
import type { TPromise } from '../promise/index';
|
|
7
|
-
import type { TAsyncIterator } from '../async-iterator/index';
|
|
8
|
-
import type { TIterator } from '../iterator/index';
|
|
9
|
-
import type { TArray } from '../array/index';
|
|
10
|
-
import type { TConstructor } from '../constructor/index';
|
|
11
|
-
import type { TFunction } from '../function/index';
|
|
12
|
-
import type { TRef } from '../ref/index';
|
|
13
|
-
import type { TObject, TProperties } from '../object/index';
|
|
14
|
-
export type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Acc, TDeref<L>]> : Acc);
|
|
15
|
-
type FromProperties<T extends TProperties> = Evaluate<{
|
|
16
|
-
[K in keyof T]: TDeref<T[K]>;
|
|
17
|
-
}>;
|
|
18
|
-
declare function FromProperties(properties: TProperties, references: TSchema[]): never;
|
|
19
|
-
export type TDeref<T extends TSchema> = T extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TConstructor<TFromRest<S>, TDeref<R>> : T extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFunction<TFromRest<S>, TDeref<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TObject<FromProperties<S>> : T extends TArray<infer S extends TSchema> ? TArray<TDeref<S>> : T extends TPromise<infer S extends TSchema> ? TPromise<TDeref<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<TDeref<S>> : T extends TIterator<infer S extends TSchema> ? TIterator<TDeref<S>> : T extends TRef<infer S extends TSchema> ? TDeref<S> : T;
|
|
20
|
-
/** `[Json]` Creates a dereferenced type */
|
|
21
|
-
export declare function Deref<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T>;
|
|
22
|
-
export {};
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.Deref = Deref;
|
|
5
|
-
const type_1 = require("../clone/type");
|
|
6
|
-
const index_1 = require("../discard/index");
|
|
7
|
-
const value_1 = require("../guard/value");
|
|
8
|
-
// ------------------------------------------------------------------
|
|
9
|
-
// TypeGuard
|
|
10
|
-
// ------------------------------------------------------------------
|
|
11
|
-
const kind_1 = require("../guard/kind");
|
|
12
|
-
function FromRest(schema, references) {
|
|
13
|
-
return schema.map((schema) => Deref(schema, references));
|
|
14
|
-
}
|
|
15
|
-
// prettier-ignore
|
|
16
|
-
function FromProperties(properties, references) {
|
|
17
|
-
const Acc = {};
|
|
18
|
-
for (const K of globalThis.Object.getOwnPropertyNames(properties)) {
|
|
19
|
-
Acc[K] = Deref(properties[K], references);
|
|
20
|
-
}
|
|
21
|
-
return Acc;
|
|
22
|
-
}
|
|
23
|
-
// prettier-ignore
|
|
24
|
-
function FromConstructor(schema, references) {
|
|
25
|
-
schema.parameters = FromRest(schema.parameters, references);
|
|
26
|
-
schema.returns = Deref(schema.returns, references);
|
|
27
|
-
return schema;
|
|
28
|
-
}
|
|
29
|
-
// prettier-ignore
|
|
30
|
-
function FromFunction(schema, references) {
|
|
31
|
-
schema.parameters = FromRest(schema.parameters, references);
|
|
32
|
-
schema.returns = Deref(schema.returns, references);
|
|
33
|
-
return schema;
|
|
34
|
-
}
|
|
35
|
-
// prettier-ignore
|
|
36
|
-
function FromIntersect(schema, references) {
|
|
37
|
-
schema.allOf = FromRest(schema.allOf, references);
|
|
38
|
-
return schema;
|
|
39
|
-
}
|
|
40
|
-
// prettier-ignore
|
|
41
|
-
function FromUnion(schema, references) {
|
|
42
|
-
schema.anyOf = FromRest(schema.anyOf, references);
|
|
43
|
-
return schema;
|
|
44
|
-
}
|
|
45
|
-
// prettier-ignore
|
|
46
|
-
function FromTuple(schema, references) {
|
|
47
|
-
if ((0, value_1.IsUndefined)(schema.items))
|
|
48
|
-
return schema;
|
|
49
|
-
schema.items = FromRest(schema.items, references);
|
|
50
|
-
return schema;
|
|
51
|
-
}
|
|
52
|
-
// prettier-ignore
|
|
53
|
-
function FromArray(schema, references) {
|
|
54
|
-
schema.items = Deref(schema.items, references);
|
|
55
|
-
return schema;
|
|
56
|
-
}
|
|
57
|
-
// prettier-ignore
|
|
58
|
-
function FromObject(schema, references) {
|
|
59
|
-
schema.properties = FromProperties(schema.properties, references);
|
|
60
|
-
return schema;
|
|
61
|
-
}
|
|
62
|
-
// prettier-ignore
|
|
63
|
-
function FromPromise(schema, references) {
|
|
64
|
-
schema.item = Deref(schema.item, references);
|
|
65
|
-
return schema;
|
|
66
|
-
}
|
|
67
|
-
// prettier-ignore
|
|
68
|
-
function FromAsyncIterator(schema, references) {
|
|
69
|
-
schema.items = Deref(schema.items, references);
|
|
70
|
-
return schema;
|
|
71
|
-
}
|
|
72
|
-
// prettier-ignore
|
|
73
|
-
function FromIterator(schema, references) {
|
|
74
|
-
schema.items = Deref(schema.items, references);
|
|
75
|
-
return schema;
|
|
76
|
-
}
|
|
77
|
-
// prettier-ignore
|
|
78
|
-
function FromRef(schema, references) {
|
|
79
|
-
const target = references.find(remote => remote.$id === schema.$ref);
|
|
80
|
-
if (target === undefined)
|
|
81
|
-
throw Error(`Unable to dereference schema with $id ${schema.$ref}`);
|
|
82
|
-
const discard = (0, index_1.Discard)(target, ['$id']);
|
|
83
|
-
return Deref(discard, references);
|
|
84
|
-
}
|
|
85
|
-
// prettier-ignore
|
|
86
|
-
function DerefResolve(schema, references) {
|
|
87
|
-
return ((0, kind_1.IsConstructor)(schema) ? FromConstructor(schema, references) :
|
|
88
|
-
(0, kind_1.IsFunction)(schema) ? FromFunction(schema, references) :
|
|
89
|
-
(0, kind_1.IsIntersect)(schema) ? FromIntersect(schema, references) :
|
|
90
|
-
(0, kind_1.IsUnion)(schema) ? FromUnion(schema, references) :
|
|
91
|
-
(0, kind_1.IsTuple)(schema) ? FromTuple(schema, references) :
|
|
92
|
-
(0, kind_1.IsArray)(schema) ? FromArray(schema, references) :
|
|
93
|
-
(0, kind_1.IsObject)(schema) ? FromObject(schema, references) :
|
|
94
|
-
(0, kind_1.IsPromise)(schema) ? FromPromise(schema, references) :
|
|
95
|
-
(0, kind_1.IsAsyncIterator)(schema) ? FromAsyncIterator(schema, references) :
|
|
96
|
-
(0, kind_1.IsIterator)(schema) ? FromIterator(schema, references) :
|
|
97
|
-
(0, kind_1.IsRef)(schema) ? FromRef(schema, references) :
|
|
98
|
-
schema);
|
|
99
|
-
}
|
|
100
|
-
// ------------------------------------------------------------------
|
|
101
|
-
// TDeref
|
|
102
|
-
// ------------------------------------------------------------------
|
|
103
|
-
/** `[Json]` Creates a dereferenced type */
|
|
104
|
-
function Deref(schema, references) {
|
|
105
|
-
return DerefResolve((0, type_1.CloneType)(schema), (0, type_1.CloneRest)(references));
|
|
106
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './deref';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './strict';
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { TSchema } from '../schema/index';
|
|
2
|
-
export type TStrict<T extends TSchema> = T;
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
5
|
-
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
6
|
-
* is how Strict works internally.
|
|
7
|
-
*
|
|
8
|
-
* ```typescript
|
|
9
|
-
* JSON.parse(JSON.stringify(Type.String()))
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
|
-
export declare function Strict<T extends TSchema>(schema: T): TStrict<T>;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.Strict = Strict;
|
|
5
|
-
/**
|
|
6
|
-
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
7
|
-
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
8
|
-
* is how Strict works internally.
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* JSON.parse(JSON.stringify(Type.String()))
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
function Strict(schema) {
|
|
15
|
-
return JSON.parse(JSON.stringify(schema));
|
|
16
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { TSchema } from '../schema/index.mjs';
|
|
2
|
-
import type { Evaluate } from '../helpers/index.mjs';
|
|
3
|
-
import type { TTuple } from '../tuple/index.mjs';
|
|
4
|
-
import type { TIntersect } from '../intersect/index.mjs';
|
|
5
|
-
import type { TUnion } from '../union/index.mjs';
|
|
6
|
-
import type { TPromise } from '../promise/index.mjs';
|
|
7
|
-
import type { TAsyncIterator } from '../async-iterator/index.mjs';
|
|
8
|
-
import type { TIterator } from '../iterator/index.mjs';
|
|
9
|
-
import type { TArray } from '../array/index.mjs';
|
|
10
|
-
import type { TConstructor } from '../constructor/index.mjs';
|
|
11
|
-
import type { TFunction } from '../function/index.mjs';
|
|
12
|
-
import type { TRef } from '../ref/index.mjs';
|
|
13
|
-
import type { TObject, TProperties } from '../object/index.mjs';
|
|
14
|
-
export type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Acc, TDeref<L>]> : Acc);
|
|
15
|
-
type FromProperties<T extends TProperties> = Evaluate<{
|
|
16
|
-
[K in keyof T]: TDeref<T[K]>;
|
|
17
|
-
}>;
|
|
18
|
-
declare function FromProperties(properties: TProperties, references: TSchema[]): never;
|
|
19
|
-
export type TDeref<T extends TSchema> = T extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TConstructor<TFromRest<S>, TDeref<R>> : T extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFunction<TFromRest<S>, TDeref<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TObject<FromProperties<S>> : T extends TArray<infer S extends TSchema> ? TArray<TDeref<S>> : T extends TPromise<infer S extends TSchema> ? TPromise<TDeref<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<TDeref<S>> : T extends TIterator<infer S extends TSchema> ? TIterator<TDeref<S>> : T extends TRef<infer S extends TSchema> ? TDeref<S> : T;
|
|
20
|
-
/** `[Json]` Creates a dereferenced type */
|
|
21
|
-
export declare function Deref<T extends TSchema>(schema: T, references: TSchema[]): TDeref<T>;
|
|
22
|
-
export {};
|