@sinclair/typebox 0.34.14 → 0.34.16
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/syntax/index.d.ts +1 -1
- package/build/cjs/syntax/index.js +1 -1
- package/build/cjs/syntax/runtime.d.ts +38 -54
- package/build/cjs/syntax/runtime.js +60 -202
- package/build/cjs/syntax/static.d.ts +62 -148
- package/build/cjs/syntax/syntax.d.ts +33 -0
- package/build/cjs/syntax/syntax.js +37 -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/instance-type/instance-type.d.ts +5 -4
- package/build/cjs/type/instance-type/instance-type.js +3 -1
- package/build/cjs/type/parameters/parameters.d.ts +3 -3
- package/build/cjs/type/parameters/parameters.js +3 -1
- 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 +4 -4
- package/build/cjs/value/convert/convert.js +1 -1
- package/build/esm/syntax/index.d.mts +1 -1
- package/build/esm/syntax/index.mjs +1 -1
- package/build/esm/syntax/runtime.d.mts +38 -54
- package/build/esm/syntax/runtime.mjs +60 -202
- package/build/esm/syntax/static.d.mts +62 -148
- package/build/esm/syntax/syntax.d.mts +33 -0
- package/build/esm/syntax/syntax.mjs +30 -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/instance-type/instance-type.d.mts +5 -4
- package/build/esm/type/instance-type/instance-type.mjs +3 -1
- package/build/esm/type/parameters/parameters.d.mts +3 -3
- package/build/esm/type/parameters/parameters.mjs +3 -1
- 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 +4 -4
- package/build/esm/value/convert/convert.mjs +1 -1
- package/package.json +11 -1
- package/parser/package.json +4 -0
- package/readme.md +86 -161
- 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
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { CreateType } from '../create/type.mjs';
|
|
2
|
+
import { Never } from '../never/index.mjs';
|
|
3
|
+
import * as KindGuard from '../guard/kind.mjs';
|
|
2
4
|
/** `[JavaScript]` Extracts the ReturnType from the given Function type */
|
|
3
5
|
export function ReturnType(schema, options) {
|
|
4
|
-
return CreateType(schema.returns, options);
|
|
6
|
+
return (KindGuard.IsFunction(schema) ? CreateType(schema.returns, options) : Never(options));
|
|
5
7
|
}
|
|
@@ -26,7 +26,7 @@ export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
|
26
26
|
/** `[JavaScript]` Creates a BigInt type */
|
|
27
27
|
BigInt(options?: BigIntOptions): TBigInt;
|
|
28
28
|
/** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
|
|
29
|
-
ConstructorParameters<Type extends
|
|
29
|
+
ConstructorParameters<Type extends TSchema>(schema: Type, options?: SchemaOptions): TConstructorParameters<Type>;
|
|
30
30
|
/** `[JavaScript]` Creates a Constructor type */
|
|
31
31
|
Constructor<Parameters extends TSchema[], InstanceType extends TSchema>(parameters: [...Parameters], instanceType: InstanceType, options?: SchemaOptions): TConstructor<Parameters, InstanceType>;
|
|
32
32
|
/** `[JavaScript]` Creates a Date type */
|
|
@@ -34,11 +34,11 @@ export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
|
34
34
|
/** `[JavaScript]` Creates a Function type */
|
|
35
35
|
Function<Parameters extends TSchema[], ReturnType extends TSchema>(parameters: [...Parameters], returnType: ReturnType, options?: SchemaOptions): TFunction<Parameters, ReturnType>;
|
|
36
36
|
/** `[JavaScript]` Extracts the InstanceType from the given Constructor type */
|
|
37
|
-
InstanceType<Type extends
|
|
37
|
+
InstanceType<Type extends TSchema>(schema: Type, options?: SchemaOptions): TInstanceType<Type>;
|
|
38
38
|
/** `[JavaScript]` Creates an Iterator type */
|
|
39
39
|
Iterator<Type extends TSchema>(items: Type, options?: SchemaOptions): TIterator<Type>;
|
|
40
40
|
/** `[JavaScript]` Extracts the Parameters from the given Function type */
|
|
41
|
-
Parameters<Type extends
|
|
41
|
+
Parameters<Type extends TSchema>(schema: Type, options?: SchemaOptions): TParameters<Type>;
|
|
42
42
|
/** `[JavaScript]` Creates a Promise type */
|
|
43
43
|
Promise<Type extends TSchema>(item: Type, options?: SchemaOptions): TPromise<Type>;
|
|
44
44
|
/** `[JavaScript]` Creates a RegExp type */
|
|
@@ -46,7 +46,7 @@ export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
|
46
46
|
/** `[JavaScript]` Creates a RegExp type */
|
|
47
47
|
RegExp(regex: RegExp, options?: RegExpOptions): TRegExp;
|
|
48
48
|
/** `[JavaScript]` Extracts the ReturnType from the given Function type */
|
|
49
|
-
ReturnType<Type extends
|
|
49
|
+
ReturnType<Type extends TSchema>(type: Type, options?: SchemaOptions): TReturnType<Type>;
|
|
50
50
|
/** `[JavaScript]` Creates a Symbol type */
|
|
51
51
|
Symbol(options?: SchemaOptions): TSymbol;
|
|
52
52
|
/** `[JavaScript]` Creates a Undefined type */
|
|
@@ -72,7 +72,7 @@ function TryConvertNumber(value) {
|
|
|
72
72
|
return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value;
|
|
73
73
|
}
|
|
74
74
|
function TryConvertInteger(value) {
|
|
75
|
-
return IsStringNumeric(value) ? parseInt(value) : IsNumber(value) ? value
|
|
75
|
+
return IsStringNumeric(value) ? parseInt(value) : IsNumber(value) ? Math.trunc(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value;
|
|
76
76
|
}
|
|
77
77
|
function TryConvertNull(value) {
|
|
78
78
|
return IsString(value) && value.toLowerCase() === 'null' ? null : value;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.34.
|
|
3
|
+
"version": "0.34.16",
|
|
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)
|
|
@@ -1044,162 +1045,6 @@ if(TypeGuard.IsString(T)) {
|
|
|
1044
1045
|
}
|
|
1045
1046
|
```
|
|
1046
1047
|
|
|
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
1048
|
<a name='values'></a>
|
|
1204
1049
|
|
|
1205
1050
|
## Values
|
|
@@ -1490,6 +1335,87 @@ ValuePointer.Set(A, '/y', 1) // A' = { x: 1, y: 1, z: 0
|
|
|
1490
1335
|
ValuePointer.Set(A, '/z', 1) // A' = { x: 1, y: 1, z: 1 }
|
|
1491
1336
|
```
|
|
1492
1337
|
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
<a name='syntax'></a>
|
|
1341
|
+
|
|
1342
|
+
## Syntax Types
|
|
1343
|
+
|
|
1344
|
+
TypeBox provides optional support for runtime and type level parsing from TypeScript syntax.
|
|
1345
|
+
|
|
1346
|
+
```typescript
|
|
1347
|
+
import { Syntax } from '@sinclair/typebox/syntax'
|
|
1348
|
+
```
|
|
1349
|
+
|
|
1350
|
+
<a name='syntax-type'></a>
|
|
1351
|
+
|
|
1352
|
+
### Type
|
|
1353
|
+
|
|
1354
|
+
Use the Syntax function to create TypeBox type from TypeScript syntax.
|
|
1355
|
+
|
|
1356
|
+
```typescript
|
|
1357
|
+
const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
|
|
1358
|
+
// x: TNumber
|
|
1359
|
+
// y: TNumber
|
|
1360
|
+
// }>
|
|
1361
|
+
```
|
|
1362
|
+
|
|
1363
|
+
<a name='syntax-options'></a>
|
|
1364
|
+
|
|
1365
|
+
### Options
|
|
1366
|
+
|
|
1367
|
+
Options can be passed to types on the last parameter
|
|
1368
|
+
|
|
1369
|
+
```typescript
|
|
1370
|
+
const T = Syntax(`number`, { // const T = {
|
|
1371
|
+
minimum: 0, // type: 'number',
|
|
1372
|
+
maximum: 10 // minimum: 0,
|
|
1373
|
+
}) // maximum: 10
|
|
1374
|
+
// }
|
|
1375
|
+
```
|
|
1376
|
+
|
|
1377
|
+
<a name="syntax-parameters"></a>
|
|
1378
|
+
|
|
1379
|
+
### Parameters
|
|
1380
|
+
|
|
1381
|
+
Syntax types can be parameterized to accept exterior types.
|
|
1382
|
+
|
|
1383
|
+
```typescript
|
|
1384
|
+
const T = Syntax('number')
|
|
1385
|
+
|
|
1386
|
+
const S = Syntax({ T }, `{ x: T, y: T, z: T }`) // const S: TObject<{
|
|
1387
|
+
// x: TNumber,
|
|
1388
|
+
// y: TNumber,
|
|
1389
|
+
// z: TNumber
|
|
1390
|
+
// }>
|
|
1391
|
+
```
|
|
1392
|
+
|
|
1393
|
+
<a name="syntax-generics"></a>
|
|
1394
|
+
|
|
1395
|
+
### Generics
|
|
1396
|
+
|
|
1397
|
+
Generic syntax types can be created using parameterized types.
|
|
1398
|
+
|
|
1399
|
+
```typescript
|
|
1400
|
+
// Generic Syntax Type
|
|
1401
|
+
|
|
1402
|
+
const Vector = <T extends string>(T: T) => Syntax({ T: Syntax(T) }, `{
|
|
1403
|
+
x: T,
|
|
1404
|
+
y: T,
|
|
1405
|
+
z: T
|
|
1406
|
+
}`)
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
// Instanced Generic Syntax Type
|
|
1410
|
+
|
|
1411
|
+
const NumberVector = Vector('number') // const NumberVector: TObject<{
|
|
1412
|
+
// x: TNumber,
|
|
1413
|
+
// y: TNumber,
|
|
1414
|
+
// z: TNumber
|
|
1415
|
+
// }>
|
|
1416
|
+
```
|
|
1417
|
+
|
|
1418
|
+
|
|
1493
1419
|
<a name='typeregistry'></a>
|
|
1494
1420
|
|
|
1495
1421
|
## TypeRegistry
|
|
@@ -1779,7 +1705,6 @@ The following is a list of community packages that offer general tooling, extend
|
|
|
1779
1705
|
| [sveltekit-superforms](https://github.com/ciscoheat/sveltekit-superforms) | A comprehensive SvelteKit form library for server and client validation |
|
|
1780
1706
|
| [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
|
|
1781
1707
|
| [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
|
|
1782
|
-
| [typebox-validators](https://github.com/jtlapp/typebox-validators) | Advanced validators supporting discriminated and heterogeneous unions |
|
|
1783
1708
|
|
|
1784
1709
|
<a name='benchmark'></a>
|
|
1785
1710
|
|
|
@@ -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
|