@sinclair/typebox 0.34.15 → 0.34.17
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.js +5 -0
- package/build/cjs/errors/errors.js +3 -0
- package/build/cjs/index.d.ts +3 -0
- package/build/cjs/index.js +3 -0
- package/build/cjs/syntax/index.d.ts +1 -1
- package/build/cjs/syntax/index.js +1 -1
- package/build/cjs/syntax/runtime.d.ts +40 -54
- package/build/cjs/syntax/runtime.js +91 -203
- package/build/cjs/syntax/static.d.ts +80 -146
- package/build/cjs/syntax/syntax.d.ts +33 -0
- package/build/cjs/syntax/syntax.js +37 -0
- package/build/cjs/type/argument/argument.d.ts +9 -0
- package/build/cjs/type/argument/argument.js +10 -0
- package/build/cjs/type/argument/index.d.ts +1 -0
- package/build/cjs/type/argument/index.js +18 -0
- package/build/cjs/type/constructor-parameters/constructor-parameters.d.ts +3 -3
- package/build/cjs/type/constructor-parameters/constructor-parameters.js +3 -1
- package/build/cjs/type/guard/kind.d.ts +9 -6
- package/build/cjs/type/guard/kind.js +6 -0
- package/build/cjs/type/guard/type.d.ts +9 -6
- package/build/cjs/type/guard/type.js +9 -0
- package/build/cjs/type/index.d.ts +2 -0
- package/build/cjs/type/index.js +2 -0
- package/build/cjs/type/instance-type/instance-type.d.ts +5 -4
- package/build/cjs/type/instance-type/instance-type.js +3 -1
- package/build/cjs/type/instantiate/index.d.ts +1 -0
- package/build/cjs/type/instantiate/index.js +18 -0
- package/build/cjs/type/instantiate/instantiate.d.ts +25 -0
- package/build/cjs/type/instantiate/instantiate.js +32 -0
- package/build/cjs/type/parameters/parameters.d.ts +3 -3
- package/build/cjs/type/parameters/parameters.js +3 -1
- package/build/cjs/type/record/record.d.ts +16 -1
- package/build/cjs/type/record/record.js +53 -23
- package/build/cjs/type/remap/index.d.ts +1 -0
- package/build/cjs/type/remap/index.js +18 -0
- package/build/cjs/type/remap/remap.d.ts +19 -0
- package/build/cjs/type/remap/remap.js +35 -0
- package/build/cjs/type/return-type/return-type.d.ts +5 -4
- package/build/cjs/type/return-type/return-type.js +3 -1
- package/build/cjs/type/type/javascript.d.ts +10 -4
- package/build/cjs/type/type/javascript.js +44 -34
- package/build/cjs/type/type/type.d.ts +3 -0
- package/build/cjs/type/type/type.js +123 -117
- package/build/cjs/value/check/check.js +5 -0
- package/build/cjs/value/create/create.js +5 -0
- package/build/esm/compiler/compiler.mjs +5 -0
- package/build/esm/errors/errors.mjs +3 -0
- package/build/esm/index.d.mts +3 -0
- package/build/esm/index.mjs +3 -0
- package/build/esm/syntax/index.d.mts +1 -1
- package/build/esm/syntax/index.mjs +1 -1
- package/build/esm/syntax/runtime.d.mts +40 -54
- package/build/esm/syntax/runtime.mjs +91 -203
- package/build/esm/syntax/static.d.mts +80 -146
- package/build/esm/syntax/syntax.d.mts +33 -0
- package/build/esm/syntax/syntax.mjs +30 -0
- package/build/esm/type/argument/argument.d.mts +9 -0
- package/build/esm/type/argument/argument.mjs +6 -0
- package/build/esm/type/argument/index.d.mts +1 -0
- package/build/esm/type/argument/index.mjs +1 -0
- package/build/esm/type/constructor-parameters/constructor-parameters.d.mts +3 -3
- package/build/esm/type/constructor-parameters/constructor-parameters.mjs +3 -1
- package/build/esm/type/guard/kind.d.mts +9 -6
- package/build/esm/type/guard/kind.mjs +5 -0
- package/build/esm/type/guard/type.d.mts +9 -6
- package/build/esm/type/guard/type.mjs +8 -0
- package/build/esm/type/index.d.mts +2 -0
- package/build/esm/type/index.mjs +2 -0
- package/build/esm/type/instance-type/instance-type.d.mts +5 -4
- package/build/esm/type/instance-type/instance-type.mjs +3 -1
- package/build/esm/type/instantiate/index.d.mts +1 -0
- package/build/esm/type/instantiate/index.mjs +1 -0
- package/build/esm/type/instantiate/instantiate.d.mts +25 -0
- package/build/esm/type/instantiate/instantiate.mjs +28 -0
- package/build/esm/type/parameters/parameters.d.mts +3 -3
- package/build/esm/type/parameters/parameters.mjs +3 -1
- package/build/esm/type/record/record.d.mts +16 -1
- package/build/esm/type/record/record.mjs +35 -8
- package/build/esm/type/remap/index.d.mts +1 -0
- package/build/esm/type/remap/index.mjs +1 -0
- package/build/esm/type/remap/remap.d.mts +19 -0
- package/build/esm/type/remap/remap.mjs +31 -0
- package/build/esm/type/return-type/return-type.d.mts +5 -4
- package/build/esm/type/return-type/return-type.mjs +3 -1
- package/build/esm/type/type/javascript.d.mts +10 -4
- package/build/esm/type/type/javascript.mjs +10 -0
- package/build/esm/type/type/type.d.mts +3 -0
- package/build/esm/type/type/type.mjs +3 -0
- package/build/esm/value/check/check.mjs +5 -0
- package/build/esm/value/create/create.mjs +5 -0
- package/package.json +11 -1
- package/parser/package.json +4 -0
- package/readme.md +107 -184
- package/build/cjs/syntax/parse.d.ts +0 -15
- package/build/cjs/syntax/parse.js +0 -19
- package/build/esm/syntax/parse.d.mts +0 -15
- package/build/esm/syntax/parse.mjs +0 -14
- /package/build/cjs/{syntax/parsebox → parser}/index.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/index.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/guard.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/guard.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/index.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/index.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/module.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/module.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/parse.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/parse.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/token.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/token.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/types.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/runtime/types.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/index.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/index.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/parse.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/parse.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/token.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/token.js +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/types.d.ts +0 -0
- /package/build/cjs/{syntax/parsebox → parser}/static/types.js +0 -0
- /package/build/esm/{syntax/parsebox → parser}/index.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/index.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/guard.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/guard.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/index.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/index.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/module.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/module.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/parse.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/parse.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/token.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/token.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/types.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/runtime/types.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/index.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/index.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/parse.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/parse.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/token.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/token.mjs +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/types.d.mts +0 -0
- /package/build/esm/{syntax/parsebox → parser}/static/types.mjs +0 -0
|
@@ -42,6 +42,9 @@ function IsDefined(value) {
|
|
|
42
42
|
function FromAny(schema, references, value) {
|
|
43
43
|
return true;
|
|
44
44
|
}
|
|
45
|
+
function FromArgument(schema, references, value) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
45
48
|
function FromArray(schema, references, value) {
|
|
46
49
|
if (!IsArray(value))
|
|
47
50
|
return false;
|
|
@@ -390,6 +393,8 @@ function Visit(schema, references, value) {
|
|
|
390
393
|
switch (schema_[Kind]) {
|
|
391
394
|
case 'Any':
|
|
392
395
|
return FromAny(schema_, references_, value);
|
|
396
|
+
case 'Argument':
|
|
397
|
+
return FromArgument(schema_, references_, value);
|
|
393
398
|
case 'Array':
|
|
394
399
|
return FromArray(schema_, references_, value);
|
|
395
400
|
case 'AsyncIterator':
|
|
@@ -34,6 +34,9 @@ function FromAny(schema, references) {
|
|
|
34
34
|
return {};
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
+
function FromArgument(schema, references) {
|
|
38
|
+
return {};
|
|
39
|
+
}
|
|
37
40
|
function FromArray(schema, references) {
|
|
38
41
|
if (schema.uniqueItems === true && !HasPropertyKey(schema, 'default')) {
|
|
39
42
|
throw new ValueCreateError(schema, 'Array with the uniqueItems constraint requires a default value');
|
|
@@ -392,6 +395,8 @@ function Visit(schema, references) {
|
|
|
392
395
|
switch (schema_[Kind]) {
|
|
393
396
|
case 'Any':
|
|
394
397
|
return FromAny(schema_, references_);
|
|
398
|
+
case 'Argument':
|
|
399
|
+
return FromArgument(schema_, references_);
|
|
395
400
|
case 'Array':
|
|
396
401
|
return FromArray(schema_, references_);
|
|
397
402
|
case 'AsyncIterator':
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.34.
|
|
3
|
+
"version": "0.34.17",
|
|
4
4
|
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -62,6 +62,16 @@
|
|
|
62
62
|
"default": "./build/esm/errors/index.mjs"
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
|
+
"./parser": {
|
|
66
|
+
"require": {
|
|
67
|
+
"types": "./build/cjs/parser/index.d.ts",
|
|
68
|
+
"default": "./build/cjs/parser/index.js"
|
|
69
|
+
},
|
|
70
|
+
"import": {
|
|
71
|
+
"types": "./build/esm/parser/index.d.mts",
|
|
72
|
+
"default": "./build/esm/parser/index.mjs"
|
|
73
|
+
}
|
|
74
|
+
},
|
|
65
75
|
"./syntax": {
|
|
66
76
|
"require": {
|
|
67
77
|
"types": "./build/cjs/syntax/index.d.ts",
|
package/readme.md
CHANGED
|
@@ -77,10 +77,6 @@ License MIT
|
|
|
77
77
|
- [Transform](#types-transform)
|
|
78
78
|
- [Guard](#types-guard)
|
|
79
79
|
- [Unsafe](#types-unsafe)
|
|
80
|
-
- [Syntax](#syntax)
|
|
81
|
-
- [Parse](#syntax-parse)
|
|
82
|
-
- [Static](#syntax-static)
|
|
83
|
-
- [Limits](#syntax-limits)
|
|
84
80
|
- [Values](#values)
|
|
85
81
|
- [Assert](#values-assert)
|
|
86
82
|
- [Create](#values-create)
|
|
@@ -100,6 +96,11 @@ License MIT
|
|
|
100
96
|
- [Errors](#values-errors)
|
|
101
97
|
- [Mutate](#values-mutate)
|
|
102
98
|
- [Pointer](#values-pointer)
|
|
99
|
+
- [Syntax](#syntax)
|
|
100
|
+
- [Type](#syntax-type)
|
|
101
|
+
- [Options](#syntax-options)
|
|
102
|
+
- [Parameters](#syntax-parameters)
|
|
103
|
+
- [Generics](#syntax-generics)
|
|
103
104
|
- [TypeRegistry](#typeregistry)
|
|
104
105
|
- [Type](#typeregistry-type)
|
|
105
106
|
- [Format](#typeregistry-format)
|
|
@@ -730,46 +731,42 @@ Object properties can be modified with Readonly and Optional. The following tabl
|
|
|
730
731
|
|
|
731
732
|
### Generic Types
|
|
732
733
|
|
|
733
|
-
Generic types can be created with
|
|
734
|
+
Generic types can be created with generic functions
|
|
734
735
|
|
|
735
736
|
```typescript
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
Type.Object({ // type Vector<T> = {
|
|
740
|
-
x: T, // x: T,
|
|
741
|
-
y: T, // y: T,
|
|
742
|
-
z: T // z: T
|
|
743
|
-
}) // }
|
|
737
|
+
const Nullable = <T extends TSchema>(T: T) => { // type Nullable<T> = T | null
|
|
738
|
+
return Type.Union([T, Type.Null())
|
|
739
|
+
}
|
|
744
740
|
|
|
745
|
-
const
|
|
741
|
+
const T = Nullable(Type.String()) // type T = Nullable<string>
|
|
746
742
|
```
|
|
747
743
|
|
|
748
|
-
Generic types
|
|
744
|
+
Generic types can also be created with Argument types
|
|
749
745
|
|
|
750
746
|
```typescript
|
|
751
|
-
const
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
747
|
+
const Vector = Type.Object({ // type Vector<A_0, A_1, A_2> = {
|
|
748
|
+
x: Type.Argument(0), // x: A_0,
|
|
749
|
+
y: Type.Argument(1), // y: A_1,
|
|
750
|
+
z: Type.Argument(2), // z: A_2
|
|
751
|
+
}) // }
|
|
752
|
+
|
|
753
|
+
const T = Type.Instantiate(Vector, [ // type T = Vector<
|
|
754
|
+
Type.Boolean(), // boolean,
|
|
755
|
+
Type.Number(), // number,
|
|
756
|
+
Type.String() // string
|
|
757
|
+
]) // >
|
|
761
758
|
```
|
|
762
759
|
|
|
763
760
|
<a name='types-recursive'></a>
|
|
764
761
|
|
|
765
762
|
### Recursive Types
|
|
766
763
|
|
|
767
|
-
Use the Recursive function to create
|
|
764
|
+
Use the Recursive function to create recursive types
|
|
768
765
|
|
|
769
766
|
```typescript
|
|
770
|
-
const Node = Type.Recursive(
|
|
767
|
+
const Node = Type.Recursive(This => Type.Object({ // const Node = {
|
|
771
768
|
id: Type.String(), // $id: 'Node',
|
|
772
|
-
nodes: Type.Array(
|
|
769
|
+
nodes: Type.Array(This) // type: 'object',
|
|
773
770
|
}), { $id: 'Node' }) // properties: {
|
|
774
771
|
// id: {
|
|
775
772
|
// type: 'string'
|
|
@@ -1044,162 +1041,6 @@ if(TypeGuard.IsString(T)) {
|
|
|
1044
1041
|
}
|
|
1045
1042
|
```
|
|
1046
1043
|
|
|
1047
|
-
<a name='syntax'></a>
|
|
1048
|
-
|
|
1049
|
-
## Syntax Types
|
|
1050
|
-
|
|
1051
|
-
TypeBox has support for parsing TypeScript type annotations directly into TypeBox types. This feature supports both runtime and static parsing, with TypeBox implementing TypeScript parsers within the TypeScript type system itself. Syntax Types use the TypeBox Json Schema representations as an AST target for TypeScript types, providing a direct mapping between TypeScript syntax and Json Schema. Syntax Types are offered as a syntactical frontend to the Standard Type Builder API.
|
|
1052
|
-
|
|
1053
|
-
This feature is available via optional import.
|
|
1054
|
-
|
|
1055
|
-
```typescript
|
|
1056
|
-
import { Parse } from '@sinclair/typebox/syntax'
|
|
1057
|
-
```
|
|
1058
|
-
|
|
1059
|
-
<a name='syntax-parse'></a>
|
|
1060
|
-
|
|
1061
|
-
### Parse
|
|
1062
|
-
|
|
1063
|
-
Use the Parse function to transform a TypeScript type annotation into a TypeBox type. This function will return the parsed TypeBox type or undefined on error.
|
|
1064
|
-
|
|
1065
|
-
```typescript
|
|
1066
|
-
const A = Parse('string') // const A: TString
|
|
1067
|
-
|
|
1068
|
-
const B = Parse('[1, 2, 3]') // const B: TTuple<[
|
|
1069
|
-
// TLiteral<1>,
|
|
1070
|
-
// TLiteral<2>,
|
|
1071
|
-
// TLiteral<3>
|
|
1072
|
-
// ]>
|
|
1073
|
-
|
|
1074
|
-
const C = Parse(`{ x: number, y: number }`) // const C: TObject<{
|
|
1075
|
-
// x: TNumber
|
|
1076
|
-
// y: TNumber
|
|
1077
|
-
// }>
|
|
1078
|
-
```
|
|
1079
|
-
|
|
1080
|
-
Syntax Types can compose with Standard Types created via the Type Builder API
|
|
1081
|
-
|
|
1082
|
-
```typescript
|
|
1083
|
-
const T = Type.Object({ // const T: TObject<{
|
|
1084
|
-
x: Parse('number'), // x: TNumber,
|
|
1085
|
-
y: Parse('string'), // y: TString,
|
|
1086
|
-
z: Parse('boolean') // z: TBoolean
|
|
1087
|
-
}) // }>
|
|
1088
|
-
```
|
|
1089
|
-
|
|
1090
|
-
Standard Types can also be passed to and referenced within Syntax Types.
|
|
1091
|
-
|
|
1092
|
-
```typescript
|
|
1093
|
-
const X = Type.Number()
|
|
1094
|
-
const Y = Type.String()
|
|
1095
|
-
const Z = Type.Boolean()
|
|
1096
|
-
|
|
1097
|
-
const T = Parse({ X, Y, Z }, `{
|
|
1098
|
-
x: X,
|
|
1099
|
-
y: Y,
|
|
1100
|
-
z: Z
|
|
1101
|
-
}`)
|
|
1102
|
-
```
|
|
1103
|
-
|
|
1104
|
-
Syntax Types also support Module parsing.
|
|
1105
|
-
|
|
1106
|
-
```typescript
|
|
1107
|
-
const Foo = Parse(`module Foo {
|
|
1108
|
-
|
|
1109
|
-
export type PartialUser = Pick<User, 'id'> & Partial<Omit<User, 'id'>>
|
|
1110
|
-
|
|
1111
|
-
export interface User {
|
|
1112
|
-
id: string
|
|
1113
|
-
name: string
|
|
1114
|
-
email: string
|
|
1115
|
-
}
|
|
1116
|
-
|
|
1117
|
-
}`)
|
|
1118
|
-
|
|
1119
|
-
const PartialUser = Foo.Import('PartialUser') // TImport<{...}, 'PartialUser'>
|
|
1120
|
-
|
|
1121
|
-
type PartialUser = Static<typeof PartialUser> // type PartialUser = {
|
|
1122
|
-
// id: string,
|
|
1123
|
-
// } & {
|
|
1124
|
-
// name?: string,
|
|
1125
|
-
// email?: string,
|
|
1126
|
-
// }
|
|
1127
|
-
```
|
|
1128
|
-
|
|
1129
|
-
<a name='syntax-static'></a>
|
|
1130
|
-
|
|
1131
|
-
### Static
|
|
1132
|
-
|
|
1133
|
-
Syntax Types provide two Static types specific to inferring TypeBox and TypeScript types from strings.
|
|
1134
|
-
|
|
1135
|
-
```typescript
|
|
1136
|
-
import { StaticParseAsSchema, StaticParseAsType } from '@sinclair/typebox/syntax'
|
|
1137
|
-
|
|
1138
|
-
// Will infer as a TSchema
|
|
1139
|
-
|
|
1140
|
-
type S = StaticParseAsSchema<{}, '{ x: number }'> // type S: TObject<{
|
|
1141
|
-
// x: TNumber
|
|
1142
|
-
// }>
|
|
1143
|
-
|
|
1144
|
-
// Will infer as a type
|
|
1145
|
-
|
|
1146
|
-
type T = StaticParseAsType<{}, '{ x: number }'> // type T = {
|
|
1147
|
-
// x: number
|
|
1148
|
-
// }
|
|
1149
|
-
```
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
<a name='syntax-limits'></a>
|
|
1153
|
-
|
|
1154
|
-
### Limitations
|
|
1155
|
-
|
|
1156
|
-
TypeBox parses TypeScript types directly within the TypeScript type system. This process does come with an inference cost, which scales with the size and complexity of the types being parsed. Although TypeBox strives to optimize Syntax Types, users should be aware of the following structures:
|
|
1157
|
-
|
|
1158
|
-
```typescript
|
|
1159
|
-
// Excessively wide structures will result in instantiation limits exceeding
|
|
1160
|
-
const A = Parse(`[
|
|
1161
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1162
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1163
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1164
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1165
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1166
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1167
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1168
|
-
0, 1, 2, 3, 4, 5, 6, 7,
|
|
1169
|
-
]`)
|
|
1170
|
-
|
|
1171
|
-
// Excessively nested structures will result in instantiation limits exceeding
|
|
1172
|
-
const B = Parse(`{
|
|
1173
|
-
x: {
|
|
1174
|
-
y: {
|
|
1175
|
-
z: {
|
|
1176
|
-
w: 1 <-- Type instantiation is excessively deep and possibly infinite.
|
|
1177
|
-
}
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
}`)
|
|
1181
|
-
```
|
|
1182
|
-
|
|
1183
|
-
If Syntax Types exceed TypeScript's instantiation limits, users are advised to fall back to the Standard Type Builder API. Alternatively, TypeBox offers a `ParseOnly` function that parses the TypeScript syntax at runtime without statically inferring the schema.
|
|
1184
|
-
|
|
1185
|
-
```typescript
|
|
1186
|
-
import { ParseOnly } from '@sinclair/typebox/syntax'
|
|
1187
|
-
|
|
1188
|
-
// const A: TSchema | undefined
|
|
1189
|
-
|
|
1190
|
-
const A = ParseOnly(`{
|
|
1191
|
-
x: {
|
|
1192
|
-
y: {
|
|
1193
|
-
z: {
|
|
1194
|
-
w: 1
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
}
|
|
1198
|
-
}`)
|
|
1199
|
-
```
|
|
1200
|
-
|
|
1201
|
-
For more information on static parsing, refer to the [ParseBox](https://github.com/sinclairzx81/parsebox) project.
|
|
1202
|
-
|
|
1203
1044
|
<a name='values'></a>
|
|
1204
1045
|
|
|
1205
1046
|
## Values
|
|
@@ -1490,6 +1331,86 @@ ValuePointer.Set(A, '/y', 1) // A' = { x: 1, y: 1, z: 0
|
|
|
1490
1331
|
ValuePointer.Set(A, '/z', 1) // A' = { x: 1, y: 1, z: 1 }
|
|
1491
1332
|
```
|
|
1492
1333
|
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
<a name='syntax'></a>
|
|
1337
|
+
|
|
1338
|
+
## Syntax Types
|
|
1339
|
+
|
|
1340
|
+
TypeBox provides optional support for runtime and type level parsing from TypeScript syntax.
|
|
1341
|
+
|
|
1342
|
+
```typescript
|
|
1343
|
+
import { Syntax } from '@sinclair/typebox/syntax'
|
|
1344
|
+
```
|
|
1345
|
+
|
|
1346
|
+
<a name='syntax-type'></a>
|
|
1347
|
+
|
|
1348
|
+
### Type
|
|
1349
|
+
|
|
1350
|
+
Use the Syntax function to create TypeBox type from TypeScript syntax.
|
|
1351
|
+
|
|
1352
|
+
```typescript
|
|
1353
|
+
const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
|
|
1354
|
+
// x: TNumber
|
|
1355
|
+
// y: TNumber
|
|
1356
|
+
// }>
|
|
1357
|
+
```
|
|
1358
|
+
|
|
1359
|
+
<a name='syntax-options'></a>
|
|
1360
|
+
|
|
1361
|
+
### Options
|
|
1362
|
+
|
|
1363
|
+
Options can be passed to types on the last parameter
|
|
1364
|
+
|
|
1365
|
+
```typescript
|
|
1366
|
+
const T = Syntax(`number`, { // const T = {
|
|
1367
|
+
minimum: 0, // type: 'number',
|
|
1368
|
+
maximum: 10 // minimum: 0,
|
|
1369
|
+
}) // maximum: 10
|
|
1370
|
+
// }
|
|
1371
|
+
```
|
|
1372
|
+
|
|
1373
|
+
<a name="syntax-parameters"></a>
|
|
1374
|
+
|
|
1375
|
+
### Parameters
|
|
1376
|
+
|
|
1377
|
+
Syntax types can be parameterized to accept exterior types.
|
|
1378
|
+
|
|
1379
|
+
```typescript
|
|
1380
|
+
const T = Syntax('number')
|
|
1381
|
+
|
|
1382
|
+
const S = Syntax({ T }, `{ x: T, y: T, z: T }`) // const S: TObject<{
|
|
1383
|
+
// x: TNumber,
|
|
1384
|
+
// y: TNumber,
|
|
1385
|
+
// z: TNumber
|
|
1386
|
+
// }>
|
|
1387
|
+
```
|
|
1388
|
+
|
|
1389
|
+
<a name="syntax-generics"></a>
|
|
1390
|
+
|
|
1391
|
+
### Generics
|
|
1392
|
+
|
|
1393
|
+
Generic types can be created by passing Argument types as parameters.
|
|
1394
|
+
|
|
1395
|
+
```typescript
|
|
1396
|
+
// Generic Vector Type
|
|
1397
|
+
|
|
1398
|
+
const Vector = Syntax({ // type Vector<X, Y, Z> = {
|
|
1399
|
+
X: Type.Argument(0), // x: X
|
|
1400
|
+
Y: Type.Argument(1), // y: Y,
|
|
1401
|
+
Z: Type.Argument(2) // z: Z
|
|
1402
|
+
}, // }
|
|
1403
|
+
`{
|
|
1404
|
+
x: X,
|
|
1405
|
+
y: Y,
|
|
1406
|
+
z: Z
|
|
1407
|
+
}`)
|
|
1408
|
+
|
|
1409
|
+
// Instanced Vector Type
|
|
1410
|
+
|
|
1411
|
+
const Up = Syntax({ Vector }, `Vector<0, 1, 0>`) // type Up = Vector<0, 1, 0>
|
|
1412
|
+
```
|
|
1413
|
+
|
|
1493
1414
|
<a name='typeregistry'></a>
|
|
1494
1415
|
|
|
1495
1416
|
## TypeRegistry
|
|
@@ -1778,8 +1699,10 @@ The following is a list of community packages that offer general tooling, extend
|
|
|
1778
1699
|
| [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas |
|
|
1779
1700
|
| [sveltekit-superforms](https://github.com/ciscoheat/sveltekit-superforms) | A comprehensive SvelteKit form library for server and client validation |
|
|
1780
1701
|
| [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
|
|
1702
|
+
| [typebox-cli](https://github.com/gsuess/typebox-cli) | Generate Schema with typebox from the CLI |
|
|
1781
1703
|
| [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
|
|
1782
1704
|
|
|
1705
|
+
|
|
1783
1706
|
<a name='benchmark'></a>
|
|
1784
1707
|
|
|
1785
1708
|
## Benchmark
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import * as Types from '../type/index';
|
|
2
|
-
import { Static } from './parsebox/index';
|
|
3
|
-
import { Main } from './static';
|
|
4
|
-
/** `[Syntax]` Infers a TypeBox type from TypeScript syntax. */
|
|
5
|
-
export type StaticParseAsSchema<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = Static.Parse<Main, Code, Context>[0];
|
|
6
|
-
/** `[Syntax]` Infers a TypeScript type from TypeScript syntax. */
|
|
7
|
-
export type StaticParseAsType<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = StaticParseAsSchema<Context, Code> extends infer Type extends Types.TSchema ? Types.StaticDecode<Type> : undefined;
|
|
8
|
-
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
|
|
9
|
-
export declare function Parse<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<Context, Code>;
|
|
10
|
-
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
|
|
11
|
-
export declare function Parse<Code extends string>(code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<{}, Code>;
|
|
12
|
-
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
|
|
13
|
-
export declare function ParseOnly<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined;
|
|
14
|
-
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax */
|
|
15
|
-
export declare function ParseOnly<Code extends string>(code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.Parse = Parse;
|
|
5
|
-
exports.ParseOnly = ParseOnly;
|
|
6
|
-
const Types = require("../type/index");
|
|
7
|
-
const runtime_1 = require("./runtime");
|
|
8
|
-
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
|
|
9
|
-
function Parse(...args) {
|
|
10
|
-
return ParseOnly.apply(null, args);
|
|
11
|
-
}
|
|
12
|
-
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
|
|
13
|
-
function ParseOnly(...args) {
|
|
14
|
-
const withContext = typeof args[0] === 'string' ? false : true;
|
|
15
|
-
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
|
|
16
|
-
const type = runtime_1.Module.Parse('Main', code, context)[0];
|
|
17
|
-
// Note: Parsing may return either a ModuleInstance or Type. We only apply options on the Type.
|
|
18
|
-
return Types.KindGuard.IsSchema(type) ? Types.CloneType(type, options) : type;
|
|
19
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import * as Types from '../type/index.mjs';
|
|
2
|
-
import { Static } from './parsebox/index.mjs';
|
|
3
|
-
import { Main } from './static.mjs';
|
|
4
|
-
/** `[Syntax]` Infers a TypeBox type from TypeScript syntax. */
|
|
5
|
-
export type StaticParseAsSchema<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = Static.Parse<Main, Code, Context>[0];
|
|
6
|
-
/** `[Syntax]` Infers a TypeScript type from TypeScript syntax. */
|
|
7
|
-
export type StaticParseAsType<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = StaticParseAsSchema<Context, Code> extends infer Type extends Types.TSchema ? Types.StaticDecode<Type> : undefined;
|
|
8
|
-
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
|
|
9
|
-
export declare function Parse<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<Context, Code>;
|
|
10
|
-
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
|
|
11
|
-
export declare function Parse<Code extends string>(code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<{}, Code>;
|
|
12
|
-
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
|
|
13
|
-
export declare function ParseOnly<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined;
|
|
14
|
-
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax */
|
|
15
|
-
export declare function ParseOnly<Code extends string>(code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import * as Types from '../type/index.mjs';
|
|
2
|
-
import { Module } from './runtime.mjs';
|
|
3
|
-
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
|
|
4
|
-
export function Parse(...args) {
|
|
5
|
-
return ParseOnly.apply(null, args);
|
|
6
|
-
}
|
|
7
|
-
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
|
|
8
|
-
export function ParseOnly(...args) {
|
|
9
|
-
const withContext = typeof args[0] === 'string' ? false : true;
|
|
10
|
-
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
|
|
11
|
-
const type = Module.Parse('Main', code, context)[0];
|
|
12
|
-
// Note: Parsing may return either a ModuleInstance or Type. We only apply options on the Type.
|
|
13
|
-
return Types.KindGuard.IsSchema(type) ? Types.CloneType(type, options) : type;
|
|
14
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|