deno.land / x / typebox@0.32.21 / readme.md

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
<div align='center'><h1>TypeBox</h1><p>Json Schema Type Builder with Static Type Resolution for TypeScript</p><img src="https://github.com/sinclairzx81/typebox/blob/master/typebox.png?raw=true" /><br /><br />[![npm version](https://badge.fury.io/js/%40sinclair%2Ftypebox.svg)](https://badge.fury.io/js/%40sinclair%2Ftypebox)[![Downloads](https://img.shields.io/npm/dm/%40sinclair%2Ftypebox.svg)](https://www.npmjs.com/package/%40sinclair%2Ftypebox)[![Build](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml/badge.svg)](https://github.com/sinclairzx81/typebox/actions/workflows/build.yml)[![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
</div><a name="Install"></a>## Install
```bash$ npm install @sinclair/typebox --save```
## Example
```typescriptimport { Type, type Static } from '@sinclair/typebox'
const T = Type.Object({ // const T = { x: Type.Number(), // type: 'object', y: Type.Number(), // required: ['x', 'y', 'z'], z: Type.Number() // properties: {}) // x: { type: 'number' }, // y: { type: 'number' }, // z: { type: 'number' } // } // }type T = Static<typeof T> // type T = { // x: number, // y: number, // z: number // }```

<a name="Overview"></a>## Overview
TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation.
This library is designed to allow Json Schema to compose with the same flexibility as TypeScript's programmable type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
License MIT
## Contents- [Install](#install)- [Overview](#overview)- [Usage](#usage)- [Types](#types) - [Json](#types-json) - [JavaScript](#types-javascript) - [Import](#types-import) - [Options](#types-options) - [Properties](#types-properties) - [Generics](#types-generics) - [References](#types-references) - [Recursive](#types-recursive) - [Template Literal](#types-template-literal) - [Indexed](#types-indexed) - [Mapped](#types-mapped) - [Conditional](#types-conditional) - [Intrinsic](#types-intrinsic) - [Transform](#types-transform) - [Rest](#types-rest) - [Guard](#types-guard) - [Unsafe](#types-unsafe) - [Strict](#types-strict)- [Values](#values) - [Create](#values-create) - [Clone](#values-clone) - [Check](#values-check) - [Convert](#values-convert) - [Default](#values-default) - [Clean](#values-clean) - [Cast](#values-cast) - [Decode](#values-decode) - [Encode](#values-decode) - [Equal](#values-equal) - [Hash](#values-hash) - [Diff](#values-diff) - [Patch](#values-patch) - [Errors](#values-errors) - [Mutate](#values-mutate) - [Pointer](#values-pointer)- [TypeRegistry](#typeregistry) - [Type](#typeregistry-type) - [Format](#typeregistry-format)- [TypeCheck](#typecheck) - [Ajv](#typecheck-ajv) - [TypeCompiler](#typecheck-typecompiler)- [TypeSystem](#typesystem) - [Policies](#typesystem-policies)- [Error Function](#error-function)- [Workbench](#workbench)- [Codegen](#codegen)- [Ecosystem](#ecosystem)- [Benchmark](#benchmark) - [Compile](#benchmark-compile) - [Validate](#benchmark-validate) - [Compression](#benchmark-compression)- [Contribute](#contribute)<a name="usage"></a>## Usage
The following shows general usage.
```typescriptimport { Type, type Static } from '@sinclair/typebox'
//--------------------------------------------------------------------------------------------//// Let's say you have the following type ...////--------------------------------------------------------------------------------------------
type T = { id: string, name: string, timestamp: number}
//--------------------------------------------------------------------------------------------//// ... you can express this type in the following way.////--------------------------------------------------------------------------------------------
const T = Type.Object({ // const T = { id: Type.String(), // type: 'object', name: Type.String(), // properties: { timestamp: Type.Integer() // id: {}) // type: 'string' // }, // name: { // type: 'string' // }, // timestamp: { // type: 'integer' // } // }, // required: [ // 'id', // 'name', // 'timestamp' // ] // }//--------------------------------------------------------------------------------------------//// ... then infer back to the original static type this way.////--------------------------------------------------------------------------------------------
type T = Static<typeof T> // type T = { // id: string, // name: string, // timestamp: number // }//--------------------------------------------------------------------------------------------//// ... then use the type both as Json Schema and as a TypeScript type.////--------------------------------------------------------------------------------------------
import { Value } from '@sinclair/typebox/value'
function receive(value: T) { // ... as a Static Type
if(Value.Check(T, value)) { // ... as a Json Schema // ok... }}```
<a name='types'></a>## Types
TypeBox types are Json Schema fragments that compose into more complex types. Each fragment is structured such that any Json Schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox offers a set of Json Types which are used to create Json Schema compliant schematics as well as a JavaScript type set used to create schematics for constructs native to JavaScript.
<a name='types-json'></a>### Json Types
The following table lists the supported Json types. These types are fully compatible with the Json Schema Draft 7 specification.
```typescript┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐│ TypeBox │ TypeScript │ Json Schema ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Any() │ type T = any │ const T = { } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Unknown() │ type T = unknown │ const T = { } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.String() │ type T = string │ const T = { ││ │ │ type: 'string' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Number() │ type T = number │ const T = { ││ │ │ type: 'number' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Integer() │ type T = number │ const T = { ││ │ │ type: 'integer' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Boolean() │ type T = boolean │ const T = { ││ │ │ type: 'boolean' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Null() │ type T = null │ const T = { ││ │ │ type: 'null' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Literal(42) │ type T = 42 │ const T = { ││ │ │ const: 42, ││ │ │ type: 'number' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Array( │ type T = number[] │ const T = { ││ Type.Number() │ │ type: 'array', ││ ) │ │ items: { ││ │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Object({ │ type T = { │ const T = { ││ x: Type.Number(), │ x: number, │ type: 'object', ││ y: Type.Number() │ y: number │ required: ['x', 'y'], ││ }) │ } │ properties: { ││ │ │ x: { ││ │ │ type: 'number' ││ │ │ }, ││ │ │ y: { ││ │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Tuple([ │ type T = [number, number] │ const T = { ││ Type.Number(), │ │ type: 'array', ││ Type.Number() │ │ items: [{ ││ ]) │ │ type: 'number' ││ │ │ }, { ││ │ │ type: 'number' ││ │ │ }], ││ │ │ additionalItems: false, ││ │ │ minItems: 2, ││ │ │ maxItems: 2 ││ │ │ } ││ │ │ ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ enum Foo { │ enum Foo { │ const T = { ││ A, │ A, │ anyOf: [{ ││ B │ B │ type: 'number', ││ } │ } │ const: 0 ││ │ │ }, { ││ const T = Type.Enum(Foo) │ type T = Foo │ type: 'number', ││ │ │ const: 1 ││ │ │ }] ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Const({ │ type T = { │ const T = { ││ x: 1, │ readonly x: 1, │ type: 'object', ││ y: 2, │ readonly y: 2 │ required: ['x', 'y'], ││ } as const) │ } │ properties: { ││ │ │ x: { ││ │ │ type: 'number', ││ │ │ const: 1 ││ │ │ }, ││ │ │ y: { ││ │ │ type: 'number', ││ │ │ const: 2 ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.KeyOf( │ type T = keyof { │ const T = { ││ Type.Object({ │ x: number, │ anyOf: [{ ││ x: Type.Number(), │ y: number │ type: 'string', ││ y: Type.Number() │ } │ const: 'x' ││ }) │ │ }, { ││ ) │ │ type: 'string', ││ │ │ const: 'y' ││ │ │ }] ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Union([ │ type T = string | number │ const T = { ││ Type.String(), │ │ anyOf: [{ ││ Type.Number() │ │ type: 'string' ││ ]) │ │ }, { ││ │ │ type: 'number' ││ │ │ }] ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Intersect([ │ type T = { │ const T = { ││ Type.Object({ │ x: number │ allOf: [{ ││ x: Type.Number() │ } & { │ type: 'object', ││ }), │ y: number │ required: ['x'], ││ Type.Object({ │ } │ properties: { ││ y: Type.Number() │ │ x: { ││ ]) │ │ type: 'number' ││ ]) │ │ } ││ │ │ } ││ │ │ }, { ││ │ │ type: 'object', |│ │ │ required: ['y'], ││ │ │ properties: { ││ │ │ y: { ││ │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ }] ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Composite([ │ type T = { │ const T = { ││ Type.Object({ │ x: number, │ type: 'object', ││ x: Type.Number() │ y: number │ required: ['x', 'y'], ││ }), │ } │ properties: { ││ Type.Object({ │ │ x: { ││ y: Type.Number() │ │ type: 'number' ││ }) │ │ }, ││ ]) │ │ y: { ││ │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Never() │ type T = never │ const T = { ││ │ │ not: {} ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Not( | type T = unknown │ const T = { ││ Type.String() │ │ not: { ││ ) │ │ type: 'string' ││ │ │ } ││ │ │ } │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Extends( │ type T = │ const T = { ││ Type.String(), │ string extends number │ const: false, ││ Type.Number(), │ ? true │ type: 'boolean' ││ Type.Literal(true), │ : false │ } ││ Type.Literal(false) │ │ ││ ) │ │ ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Extract( │ type T = Extract< │ const T = { ││ Type.Union([ │ string | number, │ type: 'string' ││ Type.String(), │ string │ } ││ Type.Number(), │ > │ ││ ]), │ │ ││ Type.String() │ │ ││ ) │ │ ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Exclude( │ type T = Exclude< │ const T = { ││ Type.Union([ │ string | number, │ type: 'number' ││ Type.String(), │ string │ } ││ Type.Number(), │ > │ ││ ]), │ │ ││ Type.String() │ │ ││ ) │ │ ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Mapped( │ type T = { │ const T = { ││ Type.Union([ │ [_ in 'x' | 'y'] : number │ type: 'object', ││ Type.Literal('x'), │ } │ required: ['x', 'y'], ││ Type.Literal('y') │ │ properties: { ││ ]), │ │ x: { ││ () => Type.Number() │ │ type: 'number' ││ ) │ │ }, ││ │ │ y: { ││ │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const U = Type.Union([ │ type U = 'open' | 'close' │ const T = { ││ Type.Literal('open'), │ │ type: 'string', ││ Type.Literal('close') │ type T = `on${U}` │ pattern: '^on(open|close)$' ││ ]) │ │ } ││ │ │ ││ const T = Type │ │ ││ .TemplateLiteral([ │ │ ││ Type.Literal('on'), │ │ ││ U │ │ ││ ]) │ │ ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Record( │ type T = Record< │ const T = { ││ Type.String(), │ string, │ type: 'object', ││ Type.Number() │ number │ patternProperties: { ││ ) │ > │ '^.*$': { ││ │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Partial( │ type T = Partial<{ │ const T = { ││ Type.Object({ │ x: number, │ type: 'object', ││ x: Type.Number(), │ y: number │ properties: { ││ y: Type.Number() | }> │ x: { ││ }) │ │ type: 'number' ││ ) │ │ }, ││ │ │ y: { ││ │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Required( │ type T = Required<{ │ const T = { ││ Type.Object({ │ x?: number, │ type: 'object', ││ x: Type.Optional( │ y?: number │ required: ['x', 'y'], ││ Type.Number() | }> │ properties: { ││ ), │ │ x: { ││ y: Type.Optional( │ │ type: 'number' ││ Type.Number() │ │ }, ││ ) │ │ y: { ││ }) │ │ type: 'number' ││ ) │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Pick( │ type T = Pick<{ │ const T = { ││ Type.Object({ │ x: number, │ type: 'object', ││ x: Type.Number(), │ y: number │ required: ['x'], ││ y: Type.Number() │ }, 'x'> │ properties: { ││ }), ['x'] | │ x: { ││ ) │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Omit( │ type T = Omit<{ │ const T = { ││ Type.Object({ │ x: number, │ type: 'object', ││ x: Type.Number(), │ y: number │ required: ['y'], ││ y: Type.Number() │ }, 'x'> │ properties: { ││ }), ['x'] | │ y: { ││ ) │ │ type: 'number' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Index( │ type T = { │ const T = { ││ Type.Object({ │ x: number, │ type: 'number' ││ x: Type.Number(), │ y: string │ } ││ y: Type.String() │ }['x'] │ ││ }), ['x'] │ │ ││ ) │ │ ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const A = Type.Tuple([ │ type A = [0, 1] │ const T = { ││ Type.Literal(0), │ type B = [2, 3] │ type: 'array', ││ Type.Literal(1) │ type T = [ │ items: [ ││ ]) │ ...A, │ { const: 0 }, ││ const B = Type.Tuple([ │ ...B │ { const: 1 }, │| Type.Literal(2), │ ] │ { const: 2 }, │| Type.Literal(3) │ │ { const: 3 } ││ ]) │ │ ], ││ const T = Type.Tuple([ │ │ additionalItems: false, │| ...Type.Rest(A), │ │ minItems: 4, │| ...Type.Rest(B) │ │ maxItems: 4 ││ ]) │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Uncapitalize( │ type T = Uncapitalize< │ const T = { ││ Type.Literal('Hello') │ 'Hello' │ type: 'string', ││ ) │ > │ const: 'hello' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Capitalize( │ type T = Capitalize< │ const T = { ││ Type.Literal('hello') │ 'hello' │ type: 'string', ││ ) │ > │ const: 'Hello' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Uppercase( │ type T = Uppercase< │ const T = { ││ Type.Literal('hello') │ 'hello' │ type: 'string', ││ ) │ > │ const: 'HELLO' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Lowercase( │ type T = Lowercase< │ const T = { ││ Type.Literal('HELLO') │ 'HELLO' │ type: 'string', ││ ) │ > │ const: 'hello' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Object({ │ type T = { │ const R = { ││ x: Type.Number(), │ x: number, │ $ref: 'T' ││ y: Type.Number() │ y: number │ } ││ }, { $id: 'T' }) | } │ ││ │ │ ││ const R = Type.Ref(T) │ type R = T │ ││ │ │ ││ │ │ ││ │ │ ││ │ │ │└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘```
<a name='types-javascript'></a>### JavaScript Types
TypeBox provides an extended type set that can be used to create schematics for common JavaScript constructs. These types can not be used with any standard Json Schema validator; but can be used to frame schematics for interfaces that may receive Json validated data. JavaScript types are prefixed with the `[JavaScript]` jsdoc comment for convenience. The following table lists the supported types.
```typescript┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐│ TypeBox │ TypeScript │ Extended Schema ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Constructor([ │ type T = new ( │ const T = { ││ Type.String(), │ arg0: string, │ type: 'Constructor', ││ Type.Number() │ arg0: number │ parameters: [{ ││ ], Type.Boolean()) │ ) => boolean │ type: 'string' ││ │ │ }, { ││ │ │ type: 'number' ││ │ │ }], ││ │ │ returns: { ││ │ │ type: 'boolean' ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Function([ │ type T = ( │ const T = { │| Type.String(), │ arg0: string, │ type: 'Function', ││ Type.Number() │ arg1: number │ parameters: [{ ││ ], Type.Boolean()) │ ) => boolean │ type: 'string' ││ │ │ }, { ││ │ │ type: 'number' ││ │ │ }], ││ │ │ returns: { ││ │ │ type: 'boolean' ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Promise( │ type T = Promise<string> │ const T = { ││ Type.String() │ │ type: 'Promise', ││ ) │ │ item: { ││ │ │ type: 'string' ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = │ type T = │ const T = { ││ Type.AsyncIterator( │ AsyncIterableIterator< │ type: 'AsyncIterator', ││ Type.String() │ string │ items: { ││ ) │ > │ type: 'string' ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Iterator( │ type T = │ const T = { ││ Type.String() │ IterableIterator<string> │ type: 'Iterator', ││ ) │ │ items: { ││ │ │ type: 'string' ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.RegExp(/abc/i) │ type T = string │ const T = { ││ │ │ type: 'RegExp' ││ │ │ source: 'abc' ││ │ │ flags: 'i' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Uint8Array() │ type T = Uint8Array │ const T = { ││ │ │ type: 'Uint8Array' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Date() │ type T = Date │ const T = { ││ │ │ type: 'Date' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Undefined() │ type T = undefined │ const T = { ││ │ │ type: 'undefined' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Symbol() │ type T = symbol │ const T = { ││ │ │ type: 'symbol' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.BigInt() │ type T = bigint │ const T = { ││ │ │ type: 'bigint' ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Void() │ type T = void │ const T = { ││ │ │ type: 'void' ││ │ │ } ││ │ │ │└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘```
<a name='types-import'></a>### Import
Import the Type namespace to bring in the full TypeBox type system. This is recommended for most users.
```typescriptimport { Type, type Static } from '@sinclair/typebox'```
You can also selectively import types. This enables modern bundlers to tree shake for unused types.
```typescriptimport { Object, Number, String, Boolean, type Static } from '@sinclair/typebox'```
<a name='types-options'></a>### Options
You can pass Json Schema options on the last argument of any given type. Option hints specific to each type are provided for convenience.
```typescript// String must be an emailconst T = Type.String({ // const T = { format: 'email' // type: 'string',}) // format: 'email' // }// Number must be a multiple of 2const T = Type.Number({ // const T = { multipleOf: 2 // type: 'number',}) // multipleOf: 2 // }// Array must have at least 5 integer valuesconst T = Type.Array(Type.Integer(), { // const T = { minItems: 5 // type: 'array',}) // minItems: 5, // items: { // type: 'integer' // } // }```
<a name='types-properties'></a>### Properties
Object properties can be modified with Readonly and Optional. The following table shows how these modifiers map between TypeScript and Json Schema.
```typescript┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐│ TypeBox │ TypeScript │ Json Schema ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Object({ │ type T = { │ const T = { ││ name: Type.ReadonlyOptional( │ readonly name?: string │ type: 'object', ││ Type.String() │ } │ properties: { ││ ) │ │ name: { ││ }) │ │ type: 'string' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Object({ │ type T = { │ const T = { ││ name: Type.Readonly( │ readonly name: string │ type: 'object', ││ Type.String() │ } │ properties: { ││ ) │ │ name: { ││ }) │ │ type: 'string' ││ │ │ } ││ │ │ }, ││ │ │ required: ['name'] ││ │ │ } ││ │ │ │├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ const T = Type.Object({ │ type T = { │ const T = { ││ name: Type.Optional( │ name?: string │ type: 'object', ││ Type.String() │ } │ properties: { ││ ) │ │ name: { ││ }) │ │ type: 'string' ││ │ │ } ││ │ │ } ││ │ │ } ││ │ │ │└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘```
<a name='types-generics'></a>### Generic Types
Generic types can be created with functions. TypeBox types extend the TSchema interface so you should constrain parameters to this type. The following creates a generic Vector type.
```typescriptimport { Type, type Static, type TSchema } from '@sinclair/typebox'
const Vector = <T extends TSchema>(T: T) => Type.Object({ // type Vector<T> = { x: T, // x: T, y: T, // y: T, z: T // z: T }) // }const NumberVector = Vector(Type.Number()) // type NumberVector = Vector<number>```
Generic types are often used to create aliases for complex types. The following creates a Nullable generic type.
```typescriptconst Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
const T = Nullable(Type.String()) // const T = { // anyOf: [ // { type: 'string' }, // { type: 'null' } // ] // }type T = Static<typeof T> // type T = string | null```
<a name='types-references'></a>### Reference Types
Reference types can be created with Ref. These types infer the same as the target type but only store a named `$ref` to the target type.
```typescriptconst Vector = Type.Object({ // const Vector = { x: Type.Number(), // type: 'object', y: Type.Number(), // required: ['x', 'y', 'z'],}, { $id: 'Vector' }) // properties: { // x: { type: 'number' }, // y: { type: 'number' } // }, // $id: 'Vector' // }const VectorRef = Type.Ref(Vector) // const VectorRef = { // $ref: 'Vector' // }type VectorRef = Static<typeof VectorRef> // type VectorRef = { // x: number, // y: number // }```Use Deref to dereference a type. This function will replace any interior reference with the target type.```typescriptconst Vertex = Type.Object({ // const Vertex = { position: VectorRef, // type: 'object', texcoord: VectorRef, // required: ['position', 'texcoord'],}) // properties: { // position: { $ref: 'Vector' }, // texcoord: { $ref: 'Vector' } // } // }const VertexDeref = Type.Deref(Vertex, [Vector]) // const VertexDeref = { // type: 'object', // required: ['position', 'texcoord'], // properties: { // position: { // type: 'object', // required: ['x', 'y', 'z'], // properties: { // x: { type: 'number' }, // y: { type: 'number' } // } // }, // texcoord: { // type: 'object', // required: ['x', 'y', 'z'], // properties: { // x: { type: 'number' }, // y: { type: 'number' } // } // } // } // }```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.
<a name='types-recursive'></a>### Recursive Types
TypeBox supports recursive data structures with Recursive. This type wraps an interior type and provides it a `this` context that allows the type to reference itself. The following creates a recursive type. Singular recursive inference is also supported.
```typescriptconst Node = Type.Recursive(This => Type.Object({ // const Node = { id: Type.String(), // $id: 'Node', nodes: Type.Array(This) // type: 'object',}), { $id: 'Node' }) // properties: { // id: { // type: 'string' // }, // nodes: { // type: 'array', // items: { // $ref: 'Node' // } // } // }, // required: [ // 'id', // 'nodes' // ] // }type Node = Static<typeof Node> // type Node = { // id: string // nodes: Node[] // }function test(node: Node) { const id = node.nodes[0].nodes[0].id // id is string}```
<a name='types-template-literal'></a>### Template Literal Types
TypeBox supports template literal types with the TemplateLiteral function. This type can be created using a syntax similar to the TypeScript template literal syntax or composed from exterior types. TypeBox encodes template literals as regular expressions which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing that enables template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
```typescript// TypeScript
type K = `prop${'A'|'B'|'C'}` // type T = 'propA' | 'propB' | 'propC'
type R = Record<K, string> // type R = { // propA: string // propB: string // propC: string // }// TypeBox
const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLiteral<[ // TLiteral<'prop'>, // TUnion<[ // TLiteral<'A'>, // TLiteral<'B'>, // TLiteral<'C'>, // ]> // ]>const R = Type.Record(K, Type.String()) // const R: TObject<{ // hello1: TString, // hello2: TString, // hello3: TString, // }>```
<a name='types-indexed'></a>### Indexed Access Types
TypeBox supports indexed access types with the Index function. This function enables uniform access to interior property and element types without having to extract them from the underlying schema representation. Index types are supported for Object, Array, Tuple, Union and Intersect types.
```typescriptconst T = Type.Object({ // type T = { x: Type.Number(), // x: number, y: Type.String(), // y: string, z: Type.Boolean() // z: boolean}) // }
const A = Type.Index(T, ['x']) // type A = T['x'] // // ... evaluated as // // const A: TNumberconst B = Type.Index(T, ['x', 'y']) // type B = T['x' | 'y'] // // ... evaluated as // // const B: TUnion<[ // TNumber, // TString, // ]>const C = Type.Index(T, Type.KeyOf(T)) // type C = T[keyof T] // // ... evaluated as // // const C: TUnion<[ // TNumber, // TString, // TBoolean // ]>```
<a name='types-mapped'></a>### Mapped Types
TypeBox supports mapped types with the Mapped function. This function accepts two arguments, the first is a union type typically derived from KeyOf, the second is a mapping function that receives a mapping key `K` that can be used to index properties of a type. The following implements a mapped type that remaps each property to be `T | null`
```typescriptconst T = Type.Object({ // type T = { x: Type.Number(), // x: number, y: Type.String(), // y: string, z: Type.Boolean() // z: boolean}) // }
const M = Type.Mapped(Type.KeyOf(T), K => { // type M = { [K in keyof T]: T[K] | null } return Type.Union([Type.Index(T, K), Type.Null()]) //}) // ... evaluated as // // const M: TObject<{ // x: TUnion<[TNumber, TNull]>, // y: TUnion<[TString, TNull]>, // z: TUnion<[TBoolean, TNull]> // }>```
<a name='types-conditional'></a>### Conditional Types
TypeBox supports runtime conditional types with the Extends function. This function performs a structural assignability check against the first (`left`) and second (`right`) arguments and will return either the third (`true`) or fourth (`false`) argument based on the result. The conditional types Exclude and Extract are also supported. The following shows both TypeScript and TypeBox examples of conditional types.
```typescript// Extendsconst A = Type.Extends( // type A = string extends number ? 1 : 2 Type.String(), // Type.Number(), // ... evaluated as Type.Literal(1), // Type.Literal(2) // const A: TLiteral<2>)
// Extractconst B = Type.Extract( // type B = Extract<1 | 2 | 3, 1> Type.Union([ // Type.Literal(1), // ... evaluated as Type.Literal(2), // Type.Literal(3) // const B: TLiteral<1> ]), Type.Literal(1))
// Excludeconst C = Type.Exclude( // type C = Exclude<1 | 2 | 3, 1> Type.Union([ // Type.Literal(1), // ... evaluated as Type.Literal(2), // Type.Literal(3) // const C: TUnion<[ ]), // TLiteral<2>, Type.Literal(1) // TLiteral<3>,) // ]>```
<a name='types-intrinsic'></a>### Intrinsic Types
TypeBox supports the TypeScript intrinsic string manipulation types Uppercase, Lowercase, Capitalize and Uncapitalize. These types can be used to remap Literal, Template Literal and Union of Literal types.
```typescript// TypeScripttype A = Capitalize<'hello'> // type A = 'Hello'
type B = Capitalize<'hello' | 'world'> // type C = 'Hello' | 'World'
type C = Capitalize<`hello${1|2|3}`> // type B = 'Hello1' | 'Hello2' | 'Hello3'
// TypeBoxconst A = Type.Capitalize(Type.Literal('hello')) // const A: TLiteral<'Hello'>
const B = Type.Capitalize(Type.Union([ // const B: TUnion<[ Type.Literal('hello'), // TLiteral<'Hello'>, Type.Literal('world') // TLiteral<'World'>])) // ]>
const C = Type.Capitalize( // const C: TTemplateLiteral<[ Type.TemplateLiteral('hello${1|2|3}') // TLiteral<'Hello'>,) // TUnion<[ // TLiteral<'1'>, // TLiteral<'2'>, // TLiteral<'3'> // ]> // ]>```
<a name='types-transform'></a>### Transform Types
TypeBox supports value decoding and encoding with Transform types. These types work in tandem with the Encode and Decode functions available on the Value and TypeCompiler submodules. Transform types can be used to convert Json encoded values into constructs more natural to JavaScript. The following creates a Transform type to decode numbers into Dates using the Value submodule.
```typescriptimport { Value } from '@sinclair/typebox/value'
const T = Type.Transform(Type.Number()) .Decode(value => new Date(value)) // decode: number to Date .Encode(value => value.getTime()) // encode: Date to numberconst D = Value.Decode(T, 0) // const D = Date(1970-01-01T00:00:00.000Z)const E = Value.Encode(T, D) // const E = 0```Use the StaticEncode or StaticDecode types to infer a Transform type.```typescriptimport { Static, StaticDecode, StaticEncode } from '@sinclair/typebox'
const T = Type.Transform(Type.Array(Type.Number(), { uniqueItems: true })) .Decode(value => new Set(value)) .Encode(value => [...value])type D = StaticDecode<typeof T> // type D = Set<number> type E = StaticEncode<typeof T> // type E = Array<number>type T = Static<typeof T> // type T = Array<number>```
<a name='types-rest'></a>### Rest Types
TypeBox provides the Rest type to uniformly extract variadic tuples from Intersect, Union and Tuple types. This type can be useful to remap variadic types into different forms. The following uses Rest to remap a Tuple into a Union.
```typescriptconst T = Type.Tuple([ // const T: TTuple<[ Type.String(), // TString, Type.Number() // TNumber]) // ]>
const R = Type.Rest(T) // const R: [TString, TNumber]
const U = Type.Union(R) // const T: TUnion<[ // TString, // TNumber // ]>```
<a name='types-unsafe'></a>### Unsafe Types
TypeBox supports user defined types with Unsafe. This type allows you to specify both schema representation and inference type. The following creates an Unsafe type with a number schema that infers as string.
```typescriptconst T = Type.Unsafe<string>({ type: 'number' }) // const T = { type: 'number' }
type T = Static<typeof T> // type T = string - ?```The Unsafe type is often used to create schematics for extended specifications like OpenAPI.```typescript
const Nullable = <T extends TSchema>(schema: T) => Type.Unsafe<Static<T> | null>({ ...schema, nullable: true })
const T = Nullable(Type.String()) // const T = { // type: 'string', // nullable: true // }type T = Static<typeof T> // type T = string | null
const StringEnum = <T extends string[]>(values: [...T]) => Type.Unsafe<T[number]>({ type: 'string', enum: values })const S = StringEnum(['A', 'B', 'C']) // const S = { // enum: ['A', 'B', 'C'] // }type S = Static<typeof T> // type S = 'A' | 'B' | 'C'```<a name='types-guard'></a>### TypeGuard
TypeBox can check its own types with the TypeGuard module. This module is written for type introspection and provides structural tests for every built-in TypeBox type. Functions of this module return `is` guards which can be used with control flow assertions to obtain schema inference for unknown values. The following guards that the value `T` is TString.
```typescriptimport { TypeGuard, Kind } from '@sinclair/typebox'
const T = { [Kind]: 'String', type: 'string' }
if(TypeGuard.IsString(T)) {
// T is TString}```
<a name='types-strict'></a>### Strict
TypeBox types contain various symbol properties that are used for reflection, composition and compilation. These properties are not strictly valid Json Schema; so in some cases it may be desirable to omit them. TypeBox provides a `Strict` function that will omit these properties if necessary.
```typescriptconst T = Type.Object({ // const T = { name: Type.Optional(Type.String()) // [Symbol(TypeBox.Kind)]: 'Object',}) // type: 'object', // properties: { // name: { // type: 'string', // [Symbol(TypeBox.Kind)]: 'String', // [Symbol(TypeBox.Optional)]: 'Optional' // } // } // }const U = Type.Strict(T) // const U = { // type: 'object', // properties: { // name: { // type: 'string' // } // } // }```
<a name='values'></a>## Values
TypeBox provides an optional Value submodule that can be used to perform structural operations on JavaScript values. This submodule includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This submodule is provided via optional import.
```typescriptimport { Value } from '@sinclair/typebox/value'```
<a name='values-create'></a>### Create
Use the Create function to create a value from a type. TypeBox will use default values if specified.
```typescriptconst T = Type.Object({ x: Type.Number(), y: Type.Number({ default: 42 }) })
const A = Value.Create(T) // const A = { x: 0, y: 42 }```
<a name='values-clone'></a>### Clone
Use the Clone function to deeply clone a value.
```typescriptconst A = Value.Clone({ x: 1, y: 2, z: 3 }) // const A = { x: 1, y: 2, z: 3 }```
<a name='values-check'></a>### Check
Use the Check function to type check a value.
```typescriptconst T = Type.Object({ x: Type.Number() })
const R = Value.Check(T, { x: 1 }) // const R = true```
<a name='values-convert'></a>### Convert
Use the Convert function to convert a value into its target type if a reasonable conversion is possible. This function may return an invalid value and should be checked before use. Its return type is `unknown`.
```typescriptconst T = Type.Object({ x: Type.Number() })
const R1 = Value.Convert(T, { x: '3.14' }) // const R1 = { x: 3.14 }
const R2 = Value.Convert(T, { x: 'not a number' }) // const R2 = { x: 'not a number' }```
<a name='values-clean'></a>### Clean
Use Clean to remove excess properties from a value. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first.
```typescriptconst T = Type.Object({ x: Type.Number(), y: Type.Number() })
const X = Value.Clean(T, null) // const 'X = null
const Y = Value.Clean(T, { x: 1 }) // const 'Y = { x: 1 }
const Z = Value.Clean(T, { x: 1, y: 2, z: 3 }) // const 'Z = { x: 1, y: 2 }```
<a name='values-default'></a>### Default
Use Default to generate missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first.
```typescriptconst T = Type.Object({ x: Type.Number({ default: 0 }), y: Type.Number({ default: 0 })})
const X = Value.Default(T, null) // const 'X = null - non-enumerable
const Y = Value.Default(T, { }) // const 'Y = { x: 0, y: 0 }
const Z = Value.Default(T, { x: 1 }) // const 'Z = { x: 1, y: 0 }```
<a name='values-cast'></a>### Cast
Use the Cast function to upcast a value into a target type. This function will retain as much infomation as possible from the original value. The Cast function is intended to be used in data migration scenarios where existing values need to be upgraded to match a modified type.
```typescriptconst T = Type.Object({ x: Type.Number(), y: Type.Number() }, { additionalProperties: false })
const X = Value.Cast(T, null) // const X = { x: 0, y: 0 }
const Y = Value.Cast(T, { x: 1 }) // const Y = { x: 1, y: 0 }
const Z = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const Z = { x: 1, y: 2 }```
<a name='values-decode'></a>### Decode
Use the Decode function to decode a value from a type or throw if the value is invalid. The return value will infer as the decoded type. This function will run Transform codecs if available.
```typescriptconst A = Value.Decode(Type.String(), 'hello') // const A = 'hello'
const B = Value.Decode(Type.String(), 42) // throw```<a name='values-decode'></a>### Encode
Use the Encode function to encode a value to a type or throw if the value is invalid. The return value will infer as the encoded type. This function will run Transform codecs if available.
```typescriptconst A = Value.Encode(Type.String(), 'hello') // const A = 'hello'
const B = Value.Encode(Type.String(), 42) // throw```
<a name='values-equal'></a>### Equal
Use the Equal function to deeply check for value equality.
```typescriptconst R = Value.Equal( // const R = true { x: 1, y: 2, z: 3 }, { x: 1, y: 2, z: 3 })```
<a name='values-hash'></a>### Hash
Use the Hash function to create a [FNV1A-64](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non cryptographic hash of a value.
```typescriptconst A = Value.Hash({ x: 1, y: 2, z: 3 }) // const A = 2910466848807138541n
const B = Value.Hash({ x: 1, y: 4, z: 3 }) // const B = 1418369778807423581n```
<a name='values-diff'></a>### Diff
Use the Diff function to generate a sequence of edits that will transform one value into another.
```typescriptconst E = Value.Diff( // const E = [ { x: 1, y: 2, z: 3 }, // { type: 'update', path: '/y', value: 4 }, { y: 4, z: 5, w: 6 } // { type: 'update', path: '/z', value: 5 },) // { type: 'insert', path: '/w', value: 6 }, // { type: 'delete', path: '/x' } // ]```
<a name='values-patch'></a>### Patch
Use the Patch function to apply a sequence of edits.
```typescriptconst A = { x: 1, y: 2 }
const B = { x: 3 }
const E = Value.Diff(A, B) // const E = [ // { type: 'update', path: '/x', value: 3 }, // { type: 'delete', path: '/y' } // ]const C = Value.Patch<typeof B>(A, E) // const C = { x: 3 }```
<a name='values-errors'></a>### Errors
Use the Errors function to enumerate validation errors.
```typescriptconst T = Type.Object({ x: Type.Number(), y: Type.Number() })
const R = [...Value.Errors(T, { x: '42' })] // const R = [{ // schema: { type: 'number' }, // path: '/x', // value: '42', // message: 'Expected number' // }, { // schema: { type: 'number' }, // path: '/y', // value: undefined, // message: 'Expected number' // }]```
<a name='values-mutate'></a>### Mutate
Use the Mutate function to perform a deep mutable value assignment while retaining internal references.
```typescriptconst Y = { z: 1 } // const Y = { z: 1 }const X = { y: Y } // const X = { y: { z: 1 } }const A = { x: X } // const A = { x: { y: { z: 1 } } }
Value.Mutate(A, { x: { y: { z: 2 } } }) // A' = { x: { y: { z: 2 } } }
const R0 = A.x.y.z === 2 // const R0 = trueconst R1 = A.x.y === Y // const R1 = trueconst R2 = A.x === X // const R2 = true```
<a name='values-pointer'></a>### Pointer
Use ValuePointer to perform mutable updates on existing values using [RFC6901](https://www.rfc-editor.org/rfc/rfc6901) Json Pointers.
```typescriptimport { ValuePointer } from '@sinclair/typebox/value'
const A = { x: 0, y: 0, z: 0 }
ValuePointer.Set(A, '/x', 1) // A' = { x: 1, y: 0, z: 0 }ValuePointer.Set(A, '/y', 1) // A' = { x: 1, y: 1, z: 0 }ValuePointer.Set(A, '/z', 1) // A' = { x: 1, y: 1, z: 1 }```
<a name='typeregistry'></a>## TypeRegistry
The TypeBox type system can be extended with additional types and formats using the TypeRegistry and FormatRegistry modules. These modules integrate deeply with TypeBox's internal type checking infrastructure and can be used to create application specific types, or register schematics for alternative specifications.
<a name='typeregistry-type'></a>### TypeRegistry
Use the TypeRegistry to register a type. The Kind must match the registered type name.
```typescriptimport { TSchema, Kind, TypeRegistry } from '@sinclair/typebox'
TypeRegistry.Set('Foo', (schema, value) => value === 'foo')
const Foo = { [Kind]: 'Foo' } as TSchema
const A = Value.Check(Foo, 'foo') // const A = true
const B = Value.Check(Foo, 'bar') // const B = false```
<a name='typeregistry-format'></a>### FormatRegistry
Use the FormatRegistry to register a string format.
```typescriptimport { FormatRegistry } from '@sinclair/typebox'
FormatRegistry.Set('foo', (value) => value === 'foo')
const T = Type.String({ format: 'foo' })
const A = Value.Check(T, 'foo') // const A = true
const B = Value.Check(T, 'bar') // const B = false```
<a name='typecheck'></a>## TypeCheck
TypeBox types target Json Schema Draft 7 and are compatible with any validator that supports this specification. TypeBox also provides a built in type checking compiler designed specifically for TypeBox types that offers high performance compilation and value checking.
The following sections detail using Ajv and the TypeBox compiler infrastructure.
<a name='typecheck-ajv'></a>## Ajv
The following shows the recommended setup for Ajv.
```bash$ npm install ajv ajv-formats --save```
```typescriptimport { Type } from '@sinclair/typebox'import addFormats from 'ajv-formats'import Ajv from 'ajv'
const ajv = addFormats(new Ajv({}), [ 'date-time', 'time', 'date', 'email', 'hostname', 'ipv4', 'ipv6', 'uri', 'uri-reference', 'uuid', 'uri-template', 'json-pointer', 'relative-json-pointer', 'regex'])
const validate = ajv.compile(Type.Object({ x: Type.Number(), y: Type.Number(), z: Type.Number()}))
const R = validate({ x: 1, y: 2, z: 3 }) // const R = true```
<a name='typecheck-typecompiler'></a>### TypeCompiler
The TypeBox TypeCompiler is a high performance JIT validation compiler that transforms TypeBox types into optimized JavaScript validation routines. The compiler is tuned for fast compilation as well as fast value assertion. It is built to serve as a validation backend that can be integrated into larger applications. It can also be used for code generation.
The TypeCompiler is provided as an optional import.
```typescriptimport { TypeCompiler } from '@sinclair/typebox/compiler'```
Use the Compile function to JIT compile a type. Note that compilation is generally an expensive operation and should only be performed once per type during application start up. TypeBox does not cache previously compiled types, and applications are expected to hold references to each compiled type for the lifetime of the application.
```typescriptconst C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{ x: Type.Number(), // x: TNumber; y: Type.Number(), // y: TNumber; z: Type.Number() // z: TNumber;})) // }>>
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true```
Use the Errors function to generate diagnostic errors for a value. The Errors function will return an iterator that when enumerated; will perform an exhaustive check across the entire value yielding any error found. For performance, this function should only be called after a failed Check. Applications may also choose to yield only the first value to avoid exhaustive error generation.
```typescriptconst C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{ x: Type.Number(), // x: TNumber; y: Type.Number(), // y: TNumber; z: Type.Number() // z: TNumber;})) // }>>
const value = { }
const first = C.Errors(value).First() // const first = { // schema: { type: 'number' }, // path: '/x', // value: undefined, // message: 'Expected number' // }const all = [...C.Errors(value)] // const all = [{ // schema: { type: 'number' }, // path: '/x', // value: undefined, // message: 'Expected number' // }, { // schema: { type: 'number' }, // path: '/y', // value: undefined, // message: 'Expected number' // }, { // schema: { type: 'number' }, // path: '/z', // value: undefined, // message: 'Expected number' // }]```
Use the Code function to generate assertion functions as strings. This function can be used to generate code that can be written to disk as importable modules. This technique is sometimes referred to as Ahead of Time (AOT) compilation. The following generates code to check a string.
```typescriptconst C = TypeCompiler.Code(Type.String()) // const C = `return function check(value) { // return ( // (typeof value === 'string') // ) // }````
<a name='typesystem'></a>## TypeSystem
The TypeBox TypeSystem module provides configurations to use either Json Schema or TypeScript type checking semantics. Configurations made to the TypeSystem module are observed by the TypeCompiler, Value and Error modules.
<a name='typesystem-policies'></a>### Policies
TypeBox validates using standard Json Schema assertion policies by default. The TypeSystemPolicy module can override some of these to have TypeBox assert values inline with TypeScript static checks. It also provides overrides for certain checking rules related to non-serializable values (such as void) which can be helpful in Json based protocols such as Json Rpc 2.0.
The following overrides are available.
```typescriptimport { TypeSystemPolicy } from '@sinclair/typebox/system'
// Disallow undefined values for optional properties (default is false)//// const A: { x?: number } = { x: undefined } - disallowed when enabled
TypeSystemPolicy.ExactOptionalPropertyTypes = true
// Allow arrays to validate as object types (default is false)//// const A: {} = [] - allowed in TS
TypeSystemPolicy.AllowArrayObject = true
// Allow numeric values to be NaN or + or - Infinity (default is false)//// const A: number = NaN - allowed in TS
TypeSystemPolicy.AllowNaN = true
// Allow void types to check with undefined and null (default is false)//// Used to signal void return on Json-Rpc 2.0 protocol
TypeSystemPolicy.AllowNullVoid = true```
<a name='error-function'></a>## Error Function
Error messages in TypeBox can be customized by defining an ErrorFunction. This function allows for the localization of error messages as well as enabling custom error messages for custom types. By default, TypeBox will generate messages using the `en-US` locale. To support additional locales, you can replicate the function found in `src/errors/function.ts` and create a locale specific translation. The function can then be set via SetErrorFunction.
The following example shows an inline error function that intercepts errors for String, Number and Boolean only. The DefaultErrorFunction is used to return a default error message.

```typescriptimport { SetErrorFunction, DefaultErrorFunction, ValueErrorType } from '@sinclair/typebox/errors'
SetErrorFunction((error) => { // i18n override switch(error.errorType) { /* en-US */ case ValueErrorType.String: return 'Expected string' /* fr-FR */ case ValueErrorType.Number: return 'Nombre attendu' /* ko-KR */ case ValueErrorType.Boolean: return '예상 부울' /* en-US */ default: return DefaultErrorFunction(error) }})const T = Type.Object({ // const T: TObject<{ x: Type.String(), // TString, y: Type.Number(), // TNumber, z: Type.Boolean() // TBoolean}) // }>
const E = [...Value.Errors(T, { // const E = [{ x: null, // type: 48, y: null, // schema: { ... }, z: null // path: '/x',})] // value: null, // message: 'Expected string' // }, { // type: 34, // schema: { ... }, // path: '/y', // value: null, // message: 'Nombre attendu' // }, { // type: 14, // schema: { ... }, // path: '/z', // value: null, // message: '예상 부울' // }]```
<a name='workbench'></a>## TypeBox Workbench
TypeBox offers a web based code generation tool that can convert TypeScript types into TypeBox types as well as several other ecosystem libraries.
[TypeBox Workbench Link Here](https://sinclairzx81.github.io/typebox-workbench/)
<a name='codegen'></a>## TypeBox Codegen
TypeBox provides a code generation library that can be integrated into toolchains to automate type translation between TypeScript and TypeBox. This library also includes functionality to transform TypeScript types to other ecosystem libraries.
[TypeBox Codegen Link Here](https://github.com/sinclairzx81/typebox-codegen)
<a name='ecosystem'></a>## Ecosystem
The following is a list of community packages that offer general tooling, extended functionality and framework integration support for TypeBox.
| Package | Description || ------------- | ------------- || [drizzle-typebox](https://www.npmjs.com/package/drizzle-typebox) | Generates TypeBox types from Drizzle ORM schemas || [elysia](https://github.com/elysiajs/elysia) | Fast and friendly Bun web framework || [fastify-type-provider-typebox](https://github.com/fastify/fastify-type-provider-typebox) | Fastify TypeBox integration with the Fastify Type Provider || [feathersjs](https://github.com/feathersjs/feathers) | The API and real-time application framework || [fetch-typebox](https://github.com/erfanium/fetch-typebox) | Drop-in replacement for fetch that brings easy integration with TypeBox || [h3-typebox](https://github.com/kevinmarrec/h3-typebox) | Schema validation utilities for h3 using TypeBox & Ajv || [http-wizard](https://github.com/flodlc/http-wizard) | Type safe http client library for Fastify || [openapi-box](https://github.com/geut/openapi-box) | Generate TypeBox types from OpenApi IDL + Http client library || [prismabox](https://github.com/m1212e/prismabox) | Converts a prisma.schema to typebox schema matching the database models || [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas || [sveltekit-superforms](https://github.com/ciscoheat/sveltekit-superforms) | A comprehensive SvelteKit form library for server and client validation || [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types || [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas || [typebox-validators](https://github.com/jtlapp/typebox-validators) | Advanced validators supporting discriminated and heterogeneous unions |
<a name='benchmark'></a>## Benchmark
This project maintains a set of benchmarks that measure Ajv, Value and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. The results below show for Ajv version 8.12.0 running on Node 20.10.0.
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
<a name='benchmark-compile'></a>### Compile
This benchmark measures compilation performance for varying types.
```typescript┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┐│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┤│ Literal_String │ 1000 │ ' 242 ms' │ ' 10 ms' │ ' 24.20 x' ││ Literal_Number │ 1000 │ ' 200 ms' │ ' 8 ms' │ ' 25.00 x' ││ Literal_Boolean │ 1000 │ ' 168 ms' │ ' 6 ms' │ ' 28.00 x' ││ Primitive_Number │ 1000 │ ' 165 ms' │ ' 8 ms' │ ' 20.63 x' ││ Primitive_String │ 1000 │ ' 154 ms' │ ' 6 ms' │ ' 25.67 x' ││ Primitive_String_Pattern │ 1000 │ ' 208 ms' │ ' 14 ms' │ ' 14.86 x' ││ Primitive_Boolean │ 1000 │ ' 142 ms' │ ' 6 ms' │ ' 23.67 x' ││ Primitive_Null │ 1000 │ ' 143 ms' │ ' 6 ms' │ ' 23.83 x' ││ Object_Unconstrained │ 1000 │ ' 1217 ms' │ ' 31 ms' │ ' 39.26 x' ││ Object_Constrained │ 1000 │ ' 1275 ms' │ ' 26 ms' │ ' 49.04 x' ││ Object_Vector3 │ 1000 │ ' 405 ms' │ ' 12 ms' │ ' 33.75 x' ││ Object_Box3D │ 1000 │ ' 1833 ms' │ ' 27 ms' │ ' 67.89 x' ││ Tuple_Primitive │ 1000 │ ' 475 ms' │ ' 13 ms' │ ' 36.54 x' ││ Tuple_Object │ 1000 │ ' 1267 ms' │ ' 30 ms' │ ' 42.23 x' ││ Composite_Intersect │ 1000 │ ' 604 ms' │ ' 18 ms' │ ' 33.56 x' ││ Composite_Union │ 1000 │ ' 545 ms' │ ' 20 ms' │ ' 27.25 x' ││ Math_Vector4 │ 1000 │ ' 829 ms' │ ' 12 ms' │ ' 69.08 x' ││ Math_Matrix4 │ 1000 │ ' 405 ms' │ ' 10 ms' │ ' 40.50 x' ││ Array_Primitive_Number │ 1000 │ ' 372 ms' │ ' 12 ms' │ ' 31.00 x' ││ Array_Primitive_String │ 1000 │ ' 327 ms' │ ' 5 ms' │ ' 65.40 x' ││ Array_Primitive_Boolean │ 1000 │ ' 300 ms' │ ' 4 ms' │ ' 75.00 x' ││ Array_Object_Unconstrained │ 1000 │ ' 1755 ms' │ ' 21 ms' │ ' 83.57 x' ││ Array_Object_Constrained │ 1000 │ ' 1516 ms' │ ' 20 ms' │ ' 75.80 x' ││ Array_Tuple_Primitive │ 1000 │ ' 825 ms' │ ' 14 ms' │ ' 58.93 x' ││ Array_Tuple_Object │ 1000 │ ' 1616 ms' │ ' 16 ms' │ ' 101.00 x' ││ Array_Composite_Intersect │ 1000 │ ' 776 ms' │ ' 16 ms' │ ' 48.50 x' ││ Array_Composite_Union │ 1000 │ ' 820 ms' │ ' 14 ms' │ ' 58.57 x' ││ Array_Math_Vector4 │ 1000 │ ' 1166 ms' │ ' 15 ms' │ ' 77.73 x' ││ Array_Math_Matrix4 │ 1000 │ ' 695 ms' │ ' 8 ms' │ ' 86.88 x' │└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┘```
<a name='benchmark-validate'></a>### Validate
This benchmark measures validation performance for varying types.
```typescript┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤│ Literal_String │ 1000000 │ ' 18 ms' │ ' 5 ms' │ ' 4 ms' │ ' 1.25 x' ││ Literal_Number │ 1000000 │ ' 16 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' ││ Literal_Boolean │ 1000000 │ ' 15 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' ││ Primitive_Number │ 1000000 │ ' 21 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' ││ Primitive_String │ 1000000 │ ' 22 ms' │ ' 18 ms' │ ' 9 ms' │ ' 2.00 x' ││ Primitive_String_Pattern │ 1000000 │ ' 155 ms' │ ' 41 ms' │ ' 34 ms' │ ' 1.21 x' ││ Primitive_Boolean │ 1000000 │ ' 18 ms' │ ' 17 ms' │ ' 9 ms' │ ' 1.89 x' ││ Primitive_Null │ 1000000 │ ' 19 ms' │ ' 17 ms' │ ' 9 ms' │ ' 1.89 x' ││ Object_Unconstrained │ 1000000 │ ' 1003 ms' │ ' 32 ms' │ ' 24 ms' │ ' 1.33 x' ││ Object_Constrained │ 1000000 │ ' 1265 ms' │ ' 49 ms' │ ' 38 ms' │ ' 1.29 x' ││ Object_Vector3 │ 1000000 │ ' 418 ms' │ ' 22 ms' │ ' 13 ms' │ ' 1.69 x' ││ Object_Box3D │ 1000000 │ ' 2035 ms' │ ' 56 ms' │ ' 49 ms' │ ' 1.14 x' ││ Object_Recursive │ 1000000 │ ' 5243 ms' │ ' 326 ms' │ ' 157 ms' │ ' 2.08 x' ││ Tuple_Primitive │ 1000000 │ ' 153 ms' │ ' 20 ms' │ ' 12 ms' │ ' 1.67 x' ││ Tuple_Object │ 1000000 │ ' 781 ms' │ ' 28 ms' │ ' 18 ms' │ ' 1.56 x' ││ Composite_Intersect │ 1000000 │ ' 742 ms' │ ' 25 ms' │ ' 14 ms' │ ' 1.79 x' ││ Composite_Union │ 1000000 │ ' 558 ms' │ ' 24 ms' │ ' 13 ms' │ ' 1.85 x' ││ Math_Vector4 │ 1000000 │ ' 246 ms' │ ' 22 ms' │ ' 11 ms' │ ' 2.00 x' ││ Math_Matrix4 │ 1000000 │ ' 1052 ms' │ ' 43 ms' │ ' 28 ms' │ ' 1.54 x' ││ Array_Primitive_Number │ 1000000 │ ' 272 ms' │ ' 22 ms' │ ' 12 ms' │ ' 1.83 x' ││ Array_Primitive_String │ 1000000 │ ' 235 ms' │ ' 24 ms' │ ' 14 ms' │ ' 1.71 x' ││ Array_Primitive_Boolean │ 1000000 │ ' 134 ms' │ ' 23 ms' │ ' 14 ms' │ ' 1.64 x' ││ Array_Object_Unconstrained │ 1000000 │ ' 6280 ms' │ ' 65 ms' │ ' 59 ms' │ ' 1.10 x' ││ Array_Object_Constrained │ 1000000 │ ' 6076 ms' │ ' 130 ms' │ ' 119 ms' │ ' 1.09 x' ││ Array_Object_Recursive │ 1000000 │ ' 22738 ms' │ ' 1730 ms' │ ' 635 ms' │ ' 2.72 x' ││ Array_Tuple_Primitive │ 1000000 │ ' 689 ms' │ ' 35 ms' │ ' 30 ms' │ ' 1.17 x' ││ Array_Tuple_Object │ 1000000 │ ' 3266 ms' │ ' 63 ms' │ ' 52 ms' │ ' 1.21 x' ││ Array_Composite_Intersect │ 1000000 │ ' 3310 ms' │ ' 44 ms' │ ' 36 ms' │ ' 1.22 x' ││ Array_Composite_Union │ 1000000 │ ' 2432 ms' │ ' 69 ms' │ ' 33 ms' │ ' 2.09 x' ││ Array_Math_Vector4 │ 1000000 │ ' 1158 ms' │ ' 37 ms' │ ' 24 ms' │ ' 1.54 x' ││ Array_Math_Matrix4 │ 1000000 │ ' 5435 ms' │ ' 132 ms' │ ' 92 ms' │ ' 1.43 x' │└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘```
<a name='benchmark-compression'></a>### Compression
The following table lists esbuild compiled and minified sizes for each TypeBox module.
```typescript┌──────────────────────┬────────────┬────────────┬─────────────┐│ (index) │ Compiled │ Minified │ Compression │├──────────────────────┼────────────┼────────────┼─────────────┤│ typebox/compiler │ '120.6 kb' │ ' 52.9 kb' │ '2.28 x' ││ typebox/errors │ ' 55.7 kb' │ ' 25.5 kb' │ '2.19 x' ││ typebox/system │ ' 4.7 kb' │ ' 2.0 kb' │ '2.33 x' ││ typebox/value │ '146.2 kb' │ ' 62.0 kb' │ '2.36 x' ││ typebox │ ' 91.4 kb' │ ' 37.8 kb' │ '2.42 x' │└──────────────────────┴────────────┴────────────┴─────────────┘```
<a name='contribute'></a>## Contribute
TypeBox is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The TypeBox project prefers open community discussion before accepting new features.
typebox

Version Info

Tagged at
a week ago