You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeBox provides optional support for runtime and type level parsing from TypeScript syntax.
1309
+
TypeBox has support for parsing TypeScript syntax at runtime as well as statically in the type system. This feature offers a syntactical frontend to the TypeBox type builder.
1310
+
1311
+
Syntax types are available via optional import.
1341
1312
1342
1313
```typescript
1343
1314
import { Syntax } from'@sinclair/typebox/syntax'
1344
1315
```
1345
1316
1346
-
<a name='syntax-type'></a>
1317
+
<a name='syntax-create'></a>
1318
+
1319
+
### Create
1320
+
1321
+
Use the Syntax function to create TypeBox types from TypeScript syntax
1347
1322
1348
-
### Type
1323
+
```typescript
1324
+
constT=Syntax(`{ x: number, y: number }`) // const T: TObject<{
1325
+
// x: TNumber,
1326
+
// y: TNumber
1327
+
// }>
1328
+
```
1329
+
1330
+
<a name="syntax-parameters"></a>
1331
+
1332
+
### Parameters
1349
1333
1350
-
Use the Syntax function to create TypeBox type from TypeScript syntax.
1334
+
Syntax types can be parameterized to receive exterior types.
1351
1335
1352
1336
```typescript
1353
1337
constT=Syntax(`{ x: number, y: number }`) // const T: TObject<{
1354
-
// x: TNumber
1338
+
// x: TNumber,
1355
1339
// y: TNumber
1356
1340
// }>
1341
+
1342
+
constS=Syntax({ T }, `Partial<T>`) // const S: TObject<{
1343
+
// x: TOptional<TNumber>,
1344
+
// y: TOptional<TNumber>
1345
+
// }>
1357
1346
```
1358
1347
1359
1348
<a name='syntax-options'></a>
1360
1349
1361
1350
### Options
1362
1351
1363
-
Options can be passed to types on the last parameter
1352
+
Options can be passed via the last parameter
1364
1353
1365
1354
```typescript
1366
1355
constT=Syntax(`number`, { // const T = {
@@ -1370,42 +1359,30 @@ const T = Syntax(`number`, { // const T = {
1370
1359
// }
1371
1360
```
1372
1361
1373
-
<a name="syntax-parameters"></a>
1374
-
1375
-
### Parameters
1376
-
1377
-
Syntax types can be parameterized to accept exterior types.
1378
-
1379
-
```typescript
1380
-
constT=Syntax('number')
1381
-
1382
-
constS=Syntax({ T }, `{ x: T, y: T, z: T }`) // const S: TObject<{
1383
-
// x: TNumber,
1384
-
// y: TNumber,
1385
-
// z: TNumber
1386
-
// }>
1387
-
```
1388
-
1389
1362
<a name="syntax-generics"></a>
1390
1363
1391
1364
### Generics
1392
1365
1393
-
Generic types can be created using Argument types.
1366
+
Generic types can be created using positional argument types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgAVAAxwAvClG4AFBQCCUAOYBXED0wAeSwA+CgBKIxMzOHMARlt7TCdXD29fGD9o4LDjUwsAJji0BJxnNy8ff1zMiXCcuAA1HgYYaAKHYqQrABoo6O7zfPxugAMECTg4HAAuKMtOsbhUaZi58YAvJdyJfCGwmsiAISw6Ggam6BpWosckU+aoAmHR8an6xrv07tm4IJWF6dvoAFur1voFfutXmcoEDvsCwVsdtUuLw4IdjgCoBc7MgFEo-MieBAiKijsATm9zoFxtT2Ow4ASSeiKZi4k9qeyOZyudyeTzadSXkgXiDFrC4BDrIN5ryZbK5ez+eNRULpl9RSCJQ9pfKdbqFXS1tMVWLRV8IbF8Nq9da5fzCEA))
1394
1367
1395
1368
```typescript
1396
-
constVector=Syntax(`{
1397
-
x: Argument<0>,
1398
-
y: Argument<1>,
1399
-
z: Argument<2>
1369
+
constT0=Syntax('Argument<0>')
1370
+
constT1=Syntax('Argument<1>')
1371
+
constT2=Syntax('Argument<2>')
1372
+
1373
+
constVector=Syntax({ T0, T1, T2 }, `{
1374
+
x: T0,
1375
+
y: T1,
1376
+
z: T2
1400
1377
}`)
1401
1378
1402
-
constBasis=Syntax({ Vector }, `{
1379
+
constBasisVectors=Syntax({ Vector }, `{
1403
1380
x: Vector<1, 0, 0>,
1404
1381
y: Vector<0, 1, 0>,
1405
1382
z: Vector<0, 0, 1>,
1406
1383
}`)
1407
1384
1408
-
typeBasis=Static<typeofBasis>// type Basis = {
1385
+
typeBasisVectors=Static<typeofBasisVectors>// type BasisVectors = {
1409
1386
// x: { x: 1, y: 0, z: 0 },
1410
1387
// y: { x: 0, y: 1, z: 0 },
1411
1388
// z: { x: 0, y: 0, z: 1 }
@@ -1573,6 +1550,44 @@ const C = TypeCompiler.Code(Type.String()) // const C = `return functi
1573
1550
// }`
1574
1551
```
1575
1552
1553
+
<a name='typemap'></a>
1554
+
1555
+
## TypeMap
1556
+
1557
+
TypeBox offers an external package for bi-directional mapping between TypeBox, Valibot, and Zod type libraries. It also includes syntax parsing support for Valibot and Zod and supports the Standard Schema specification. For more details on TypeMap, refer to the project repository.
Once installed it offers advanced structural remapping between various runtime type libraries ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAyqgHYwCGBcAWhACZwC+cAZlBCHAOQACAzsFIBjADYVgUAPQx0GEBTDcAUMuERS-eMjgBeFHJy4AFAAMkuAFxxSAVxAAjDFEKprdx88IAvd-adQzKYAlHBwUlJw6pra1sgA8g4AVhjCMAA8CMphObl5+QWFRcW5ETlWKABy-s4A3NkljU3NBWVhblU1UPUtvX3FbXC+nZ7dDf0TE2VMAHyq0VrEesRklCbIoS1lC-BE1twWfqOuRwE+p87MKmoaiwBKy3T0xkTBAHRgFFD8GMZ2oqJNnltrd4HdrFlJltImEKh4Aj0oU1Bh14XVxkiBjChhcxpjGtMwkA))
0 commit comments