@sinclair/typebox 0.34.16 → 0.34.18

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.
Files changed (68) hide show
  1. package/build/cjs/compiler/compiler.js +5 -0
  2. package/build/cjs/errors/errors.js +3 -0
  3. package/build/cjs/index.d.ts +3 -0
  4. package/build/cjs/index.js +3 -0
  5. package/build/cjs/syntax/runtime.d.ts +3 -1
  6. package/build/cjs/syntax/runtime.js +33 -3
  7. package/build/cjs/syntax/static.d.ts +22 -2
  8. package/build/cjs/type/argument/argument.d.ts +9 -0
  9. package/build/cjs/type/argument/argument.js +10 -0
  10. package/build/cjs/type/argument/index.d.ts +1 -0
  11. package/build/cjs/type/argument/index.js +18 -0
  12. package/build/cjs/type/guard/kind.d.ts +9 -6
  13. package/build/cjs/type/guard/kind.js +6 -0
  14. package/build/cjs/type/guard/type.d.ts +9 -6
  15. package/build/cjs/type/guard/type.js +9 -0
  16. package/build/cjs/type/index.d.ts +2 -0
  17. package/build/cjs/type/index.js +2 -0
  18. package/build/cjs/type/instantiate/index.d.ts +1 -0
  19. package/build/cjs/type/instantiate/index.js +18 -0
  20. package/build/cjs/type/instantiate/instantiate.d.ts +25 -0
  21. package/build/cjs/type/instantiate/instantiate.js +32 -0
  22. package/build/cjs/type/record/record.d.ts +16 -1
  23. package/build/cjs/type/record/record.js +53 -23
  24. package/build/cjs/type/remap/index.d.ts +1 -0
  25. package/build/cjs/type/remap/index.js +18 -0
  26. package/build/cjs/type/remap/remap.d.ts +30 -0
  27. package/build/cjs/type/remap/remap.js +47 -0
  28. package/build/cjs/type/type/javascript.d.ts +6 -0
  29. package/build/cjs/type/type/javascript.js +44 -34
  30. package/build/cjs/type/type/type.d.ts +3 -0
  31. package/build/cjs/type/type/type.js +123 -117
  32. package/build/cjs/value/check/check.js +5 -0
  33. package/build/cjs/value/create/create.js +5 -0
  34. package/build/esm/compiler/compiler.mjs +5 -0
  35. package/build/esm/errors/errors.mjs +3 -0
  36. package/build/esm/index.d.mts +3 -0
  37. package/build/esm/index.mjs +3 -0
  38. package/build/esm/syntax/runtime.d.mts +3 -1
  39. package/build/esm/syntax/runtime.mjs +33 -3
  40. package/build/esm/syntax/static.d.mts +22 -2
  41. package/build/esm/type/argument/argument.d.mts +9 -0
  42. package/build/esm/type/argument/argument.mjs +6 -0
  43. package/build/esm/type/argument/index.d.mts +1 -0
  44. package/build/esm/type/argument/index.mjs +1 -0
  45. package/build/esm/type/guard/kind.d.mts +9 -6
  46. package/build/esm/type/guard/kind.mjs +5 -0
  47. package/build/esm/type/guard/type.d.mts +9 -6
  48. package/build/esm/type/guard/type.mjs +8 -0
  49. package/build/esm/type/index.d.mts +2 -0
  50. package/build/esm/type/index.mjs +2 -0
  51. package/build/esm/type/instantiate/index.d.mts +1 -0
  52. package/build/esm/type/instantiate/index.mjs +1 -0
  53. package/build/esm/type/instantiate/instantiate.d.mts +25 -0
  54. package/build/esm/type/instantiate/instantiate.mjs +28 -0
  55. package/build/esm/type/record/record.d.mts +16 -1
  56. package/build/esm/type/record/record.mjs +35 -8
  57. package/build/esm/type/remap/index.d.mts +1 -0
  58. package/build/esm/type/remap/index.mjs +1 -0
  59. package/build/esm/type/remap/remap.d.mts +30 -0
  60. package/build/esm/type/remap/remap.mjs +43 -0
  61. package/build/esm/type/type/javascript.d.mts +6 -0
  62. package/build/esm/type/type/javascript.mjs +10 -0
  63. package/build/esm/type/type/type.d.mts +3 -0
  64. package/build/esm/type/type/type.mjs +3 -0
  65. package/build/esm/value/check/check.mjs +5 -0
  66. package/build/esm/value/create/create.mjs +5 -0
  67. package/package.json +1 -1
  68. package/readme.md +36 -39
package/readme.md CHANGED
@@ -731,46 +731,42 @@ Object properties can be modified with Readonly and Optional. The following tabl
731
731
 
732
732
  ### Generic Types
733
733
 
734
- 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.
734
+ Generic types can be created with generic functions
735
735
 
736
736
  ```typescript
737
- import { Type, type Static, type TSchema } from '@sinclair/typebox'
738
-
739
- const Vector = <T extends TSchema>(T: T) =>
740
- Type.Object({ // type Vector<T> = {
741
- x: T, // x: T,
742
- y: T, // y: T,
743
- z: T // z: T
744
- }) // }
737
+ const Nullable = <T extends TSchema>(T: T) => { // type Nullable<T> = T | null
738
+ return Type.Union([T, Type.Null())
739
+ }
745
740
 
746
- const NumberVector = Vector(Type.Number()) // type NumberVector = Vector<number>
741
+ const T = Nullable(Type.String()) // type T = Nullable<string>
747
742
  ```
748
743
 
749
- Generic types are often used to create aliases for complex types. The following creates a Nullable generic type.
744
+ Generic types can also be created with Argument types
750
745
 
751
746
  ```typescript
752
- const Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
753
-
754
- const T = Nullable(Type.String()) // const T = {
755
- // anyOf: [
756
- // { type: 'string' },
757
- // { type: 'null' }
758
- // ]
759
- // }
760
-
761
- type T = Static<typeof T> // type T = string | null
747
+ const Vector = Type.Object({ // type Vector<A_0, A_1, A_2> = {
748
+ x: Type.Argument(0), // x: A_0,
749
+ y: Type.Argument(1), // y: A_1,
750
+ z: Type.Argument(2), // z: A_2
751
+ }) // }
752
+
753
+ const T = Type.Instantiate(Vector, [ // type T = Vector<
754
+ Type.Boolean(), // boolean,
755
+ Type.Number(), // number,
756
+ Type.String() // string
757
+ ]) // >
762
758
  ```
763
759
 
764
760
  <a name='types-recursive'></a>
765
761
 
766
762
  ### Recursive Types
767
763
 
768
- Use the Recursive function to create a singular recursive type.
764
+ Use the Recursive function to create recursive types
769
765
 
770
766
  ```typescript
771
- const Node = Type.Recursive(Self => Type.Object({ // const Node = {
767
+ const Node = Type.Recursive(This => Type.Object({ // const Node = {
772
768
  id: Type.String(), // $id: 'Node',
773
- nodes: Type.Array(Self) // type: 'object',
769
+ nodes: Type.Array(This) // type: 'object',
774
770
  }), { $id: 'Node' }) // properties: {
775
771
  // id: {
776
772
  // type: 'string'
@@ -1394,28 +1390,27 @@ const S = Syntax({ T }, `{ x: T, y: T, z: T }`) // const S: TObject<{
1394
1390
 
1395
1391
  ### Generics
1396
1392
 
1397
- Generic syntax types can be created using parameterized types.
1393
+ Generic types can be created by passing Argument types as parameters.
1398
1394
 
1399
1395
  ```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
1396
+ // Generic Vector Type
1397
+
1398
+ const Vector = Syntax({ // type Vector<X, Y, Z> = {
1399
+ X: Type.Argument(0), // x: X
1400
+ Y: Type.Argument(1), // y: Y,
1401
+ Z: Type.Argument(2) // z: Z
1402
+ }, // }
1403
+ `{
1404
+ x: X,
1405
+ y: Y,
1406
+ z: Z
1406
1407
  }`)
1407
1408
 
1409
+ // Instanced Vector Type
1408
1410
 
1409
- // Instanced Generic Syntax Type
1410
-
1411
- const NumberVector = Vector('number') // const NumberVector: TObject<{
1412
- // x: TNumber,
1413
- // y: TNumber,
1414
- // z: TNumber
1415
- // }>
1411
+ const Up = Syntax({ Vector }, `Vector<0, 1, 0>`) // type Up = Vector<0, 1, 0>
1416
1412
  ```
1417
1413
 
1418
-
1419
1414
  <a name='typeregistry'></a>
1420
1415
 
1421
1416
  ## TypeRegistry
@@ -1704,8 +1699,10 @@ The following is a list of community packages that offer general tooling, extend
1704
1699
  | [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas |
1705
1700
  | [sveltekit-superforms](https://github.com/ciscoheat/sveltekit-superforms) | A comprehensive SvelteKit form library for server and client validation |
1706
1701
  | [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
1702
+ | [typebox-cli](https://github.com/gsuess/typebox-cli) | Generate Schema with typebox from the CLI |
1707
1703
  | [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
1708
1704
 
1705
+
1709
1706
  <a name='benchmark'></a>
1710
1707
 
1711
1708
  ## Benchmark