@sinclair/typebox 0.32.0-dev-19 → 0.32.0-dev-21

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 (98) hide show
  1. package/build/import/compiler/compiler.mjs +62 -62
  2. package/build/import/errors/errors.mjs +62 -62
  3. package/build/import/index.d.mts +6 -6
  4. package/build/import/index.mjs +3 -3
  5. package/build/import/type/indexed/indexed-property-keys.d.mts +2 -2
  6. package/build/import/type/indexed/indexed-property-keys.mjs +6 -11
  7. package/build/import/type/intrinsic/intrinsic.mjs +3 -3
  8. package/build/import/type/optional/index.d.mts +1 -0
  9. package/build/import/type/optional/index.mjs +1 -0
  10. package/build/import/type/optional/optional-from-mapped-result.d.mts +12 -0
  11. package/build/import/type/optional/optional-from-mapped-result.mjs +17 -0
  12. package/build/import/type/optional/optional.d.mts +19 -2
  13. package/build/import/type/optional/optional.mjs +18 -2
  14. package/build/import/type/readonly/index.d.mts +1 -0
  15. package/build/import/type/readonly/index.mjs +1 -0
  16. package/build/import/type/readonly/readonly-from-mapped-result.d.mts +12 -0
  17. package/build/import/type/readonly/readonly-from-mapped-result.mjs +17 -0
  18. package/build/import/type/readonly/readonly.d.mts +18 -1
  19. package/build/import/type/readonly/readonly.mjs +18 -2
  20. package/build/import/type/record/record.d.mts +2 -2
  21. package/build/import/type/record/record.mjs +2 -3
  22. package/build/import/type/schema/schema.d.mts +1 -1
  23. package/build/import/type/symbol/symbol.d.mts +1 -1
  24. package/build/import/type/template-literal/finite.d.mts +7 -5
  25. package/build/import/type/template-literal/finite.mjs +14 -5
  26. package/build/import/type/template-literal/generate.d.mts +14 -8
  27. package/build/import/type/template-literal/generate.mjs +19 -9
  28. package/build/import/type/template-literal/index.d.mts +1 -1
  29. package/build/import/type/template-literal/index.mjs +1 -1
  30. package/build/import/type/template-literal/{parser.mjs → parse.mjs} +7 -2
  31. package/build/import/type/template-literal/syntax.d.mts +1 -1
  32. package/build/import/type/template-literal/syntax.mjs +0 -1
  33. package/build/import/type/template-literal/template-literal.d.mts +2 -2
  34. package/build/import/type/template-literal/template-literal.mjs +1 -1
  35. package/build/import/type/template-literal/union.d.mts +2 -2
  36. package/build/import/type/template-literal/union.mjs +5 -10
  37. package/build/import/type/type/json.d.mts +19 -7
  38. package/build/import/type/type/json.mjs +5 -5
  39. package/build/import/value/cast/cast.mjs +20 -20
  40. package/build/import/value/check/check.mjs +62 -62
  41. package/build/import/value/clean/clean.mjs +16 -16
  42. package/build/import/value/convert/convert.mjs +36 -36
  43. package/build/import/value/create/create.mjs +66 -67
  44. package/build/import/value/default/default.mjs +16 -16
  45. package/build/import/value/transform/decode.mjs +18 -18
  46. package/build/import/value/transform/encode.mjs +18 -18
  47. package/build/import/value/transform/has.mjs +28 -28
  48. package/build/require/compiler/compiler.js +62 -62
  49. package/build/require/errors/errors.js +62 -62
  50. package/build/require/index.d.ts +6 -6
  51. package/build/require/index.js +6 -2
  52. package/build/require/type/indexed/indexed-property-keys.d.ts +2 -2
  53. package/build/require/type/indexed/indexed-property-keys.js +4 -9
  54. package/build/require/type/intrinsic/intrinsic.js +2 -2
  55. package/build/require/type/optional/index.d.ts +1 -0
  56. package/build/require/type/optional/index.js +1 -0
  57. package/build/require/type/optional/optional-from-mapped-result.d.ts +12 -0
  58. package/build/require/type/optional/optional-from-mapped-result.js +22 -0
  59. package/build/require/type/optional/optional.d.ts +19 -2
  60. package/build/require/type/optional/optional.js +18 -2
  61. package/build/require/type/readonly/index.d.ts +1 -0
  62. package/build/require/type/readonly/index.js +1 -0
  63. package/build/require/type/readonly/readonly-from-mapped-result.d.ts +12 -0
  64. package/build/require/type/readonly/readonly-from-mapped-result.js +22 -0
  65. package/build/require/type/readonly/readonly.d.ts +18 -1
  66. package/build/require/type/readonly/readonly.js +18 -2
  67. package/build/require/type/record/record.d.ts +2 -2
  68. package/build/require/type/record/record.js +1 -2
  69. package/build/require/type/schema/schema.d.ts +1 -1
  70. package/build/require/type/symbol/symbol.d.ts +1 -1
  71. package/build/require/type/template-literal/finite.d.ts +7 -5
  72. package/build/require/type/template-literal/finite.js +16 -6
  73. package/build/require/type/template-literal/generate.d.ts +14 -8
  74. package/build/require/type/template-literal/generate.js +22 -11
  75. package/build/require/type/template-literal/index.d.ts +1 -1
  76. package/build/require/type/template-literal/index.js +1 -1
  77. package/build/require/type/template-literal/{parser.js → parse.js} +7 -2
  78. package/build/require/type/template-literal/syntax.d.ts +1 -1
  79. package/build/require/type/template-literal/syntax.js +0 -1
  80. package/build/require/type/template-literal/template-literal.d.ts +2 -2
  81. package/build/require/type/template-literal/template-literal.js +1 -1
  82. package/build/require/type/template-literal/union.d.ts +2 -2
  83. package/build/require/type/template-literal/union.js +5 -10
  84. package/build/require/type/type/json.d.ts +19 -7
  85. package/build/require/type/type/json.js +5 -5
  86. package/build/require/value/cast/cast.js +20 -20
  87. package/build/require/value/check/check.js +62 -62
  88. package/build/require/value/clean/clean.js +16 -16
  89. package/build/require/value/convert/convert.js +36 -36
  90. package/build/require/value/create/create.js +65 -66
  91. package/build/require/value/default/default.js +16 -16
  92. package/build/require/value/transform/decode.js +18 -18
  93. package/build/require/value/transform/encode.js +18 -18
  94. package/build/require/value/transform/has.js +28 -28
  95. package/package.json +1 -1
  96. package/readme.md +52 -52
  97. /package/build/import/type/template-literal/{parser.d.mts → parse.d.mts} +0 -0
  98. /package/build/require/type/template-literal/{parser.d.ts → parse.d.ts} +0 -0
@@ -13,67 +13,67 @@ const type_1 = require("../../type/guard/type");
13
13
  // ------------------------------------------------------------------
14
14
  const index_3 = require("../guard/index");
15
15
  // prettier-ignore
16
- function TArray(schema, references) {
16
+ function FromArray(schema, references) {
17
17
  return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
18
18
  }
19
19
  // prettier-ignore
20
- function TAsyncIterator(schema, references) {
20
+ function FromAsyncIterator(schema, references) {
21
21
  return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
22
22
  }
23
23
  // prettier-ignore
24
- function TConstructor(schema, references) {
24
+ function FromConstructor(schema, references) {
25
25
  return (0, type_1.IsTransform)(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
26
26
  }
27
27
  // prettier-ignore
28
- function TFunction(schema, references) {
28
+ function FromFunction(schema, references) {
29
29
  return (0, type_1.IsTransform)(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
30
30
  }
31
31
  // prettier-ignore
32
- function TIntersect(schema, references) {
32
+ function FromIntersect(schema, references) {
33
33
  return (0, type_1.IsTransform)(schema) || (0, type_1.IsTransform)(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references));
34
34
  }
35
35
  // prettier-ignore
36
- function TIterator(schema, references) {
36
+ function FromIterator(schema, references) {
37
37
  return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
38
38
  }
39
39
  // prettier-ignore
40
- function TNot(schema, references) {
40
+ function FromNot(schema, references) {
41
41
  return (0, type_1.IsTransform)(schema) || Visit(schema.not, references);
42
42
  }
43
43
  // prettier-ignore
44
- function TObject(schema, references) {
44
+ function FromObject(schema, references) {
45
45
  return ((0, type_1.IsTransform)(schema) ||
46
46
  Object.values(schema.properties).some((schema) => Visit(schema, references)) ||
47
47
  ((0, type_1.IsSchema)(schema.additionalProperties) && Visit(schema.additionalProperties, references)));
48
48
  }
49
49
  // prettier-ignore
50
- function TPromise(schema, references) {
50
+ function FromPromise(schema, references) {
51
51
  return (0, type_1.IsTransform)(schema) || Visit(schema.item, references);
52
52
  }
53
53
  // prettier-ignore
54
- function TRecord(schema, references) {
54
+ function FromRecord(schema, references) {
55
55
  const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
56
56
  const property = schema.patternProperties[pattern];
57
57
  return (0, type_1.IsTransform)(schema) || Visit(property, references) || ((0, type_1.IsSchema)(schema.additionalProperties) && (0, type_1.IsTransform)(schema.additionalProperties));
58
58
  }
59
59
  // prettier-ignore
60
- function TRef(schema, references) {
60
+ function FromRef(schema, references) {
61
61
  if ((0, type_1.IsTransform)(schema))
62
62
  return true;
63
63
  return Visit((0, index_1.Deref)(schema, references), references);
64
64
  }
65
65
  // prettier-ignore
66
- function TThis(schema, references) {
66
+ function FromThis(schema, references) {
67
67
  if ((0, type_1.IsTransform)(schema))
68
68
  return true;
69
69
  return Visit((0, index_1.Deref)(schema, references), references);
70
70
  }
71
71
  // prettier-ignore
72
- function TTuple(schema, references) {
72
+ function FromTuple(schema, references) {
73
73
  return (0, type_1.IsTransform)(schema) || (!(0, index_3.IsUndefined)(schema.items) && schema.items.some((schema) => Visit(schema, references)));
74
74
  }
75
75
  // prettier-ignore
76
- function TUnion(schema, references) {
76
+ function FromUnion(schema, references) {
77
77
  return (0, type_1.IsTransform)(schema) || schema.anyOf.some((schema) => Visit(schema, references));
78
78
  }
79
79
  // prettier-ignore
@@ -86,33 +86,33 @@ function Visit(schema, references) {
86
86
  visited.add(schema.$id);
87
87
  switch (schema[index_2.Kind]) {
88
88
  case 'Array':
89
- return TArray(schema_, references_);
89
+ return FromArray(schema_, references_);
90
90
  case 'AsyncIterator':
91
- return TAsyncIterator(schema_, references_);
91
+ return FromAsyncIterator(schema_, references_);
92
92
  case 'Constructor':
93
- return TConstructor(schema_, references_);
93
+ return FromConstructor(schema_, references_);
94
94
  case 'Function':
95
- return TFunction(schema_, references_);
95
+ return FromFunction(schema_, references_);
96
96
  case 'Intersect':
97
- return TIntersect(schema_, references_);
97
+ return FromIntersect(schema_, references_);
98
98
  case 'Iterator':
99
- return TIterator(schema_, references_);
99
+ return FromIterator(schema_, references_);
100
100
  case 'Not':
101
- return TNot(schema_, references_);
101
+ return FromNot(schema_, references_);
102
102
  case 'Object':
103
- return TObject(schema_, references_);
103
+ return FromObject(schema_, references_);
104
104
  case 'Promise':
105
- return TPromise(schema_, references_);
105
+ return FromPromise(schema_, references_);
106
106
  case 'Record':
107
- return TRecord(schema_, references_);
107
+ return FromRecord(schema_, references_);
108
108
  case 'Ref':
109
- return TRef(schema_, references_);
109
+ return FromRef(schema_, references_);
110
110
  case 'This':
111
- return TThis(schema_, references_);
111
+ return FromThis(schema_, references_);
112
112
  case 'Tuple':
113
- return TTuple(schema_, references_);
113
+ return FromTuple(schema_, references_);
114
114
  case 'Union':
115
- return TUnion(schema_, references_);
115
+ return FromUnion(schema_, references_);
116
116
  default:
117
117
  return (0, type_1.IsTransform)(schema);
118
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.32.0-dev-19",
3
+ "version": "0.32.0-dev-21",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -51,9 +51,9 @@ type T = Static<typeof T> // type T = {
51
51
 
52
52
  ## Overview
53
53
 
54
- TypeBox is a runtime type builder that creates in-memory Json Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation.
54
+ TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The Json Schema produced by this library is designed to match the static type checking rules of the TypeScript compiler. TypeBox provides a unified type that can be statically checked by the TypeScript compiler and runtime asserted using standard Json Schema validation.
55
55
 
56
- This library enables Json Schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
56
+ This library enables Json Schema to be created with the same expressiveness as TypeScript's type system. It can be used either as a simple tool to build up complex schematics or integrated into REST or RPC services to help validate data received over the wire.
57
57
 
58
58
  License MIT
59
59
 
@@ -70,9 +70,9 @@ License MIT
70
70
  - [Generics](#types-generics)
71
71
  - [References](#types-references)
72
72
  - [Recursive](#types-recursive)
73
+ - [Template Literal](#types-template-literal)
73
74
  - [Conditional](#types-conditional)
74
75
  - [Mapped](#types-mapped)
75
- - [Template Literal](#types-templateliteral)
76
76
  - [Indexed](#types-indexed)
77
77
  - [Intrinsic](#types-intrinsic)
78
78
  - [Transform](#types-transform)
@@ -647,7 +647,7 @@ TypeBox provides an extended type set that can be used to create schematics for
647
647
 
648
648
  ### Modules
649
649
 
650
- TypeBox has a modular type system that supports selective type imports. Using selective imports can be useful in resource constrained environments as the technique enables modern bundlers to tree shake unused code leading to reduced bundle sizes. The following selectively imports a few types.
650
+ TypeBox supports a modular type system that allows for selective type imports. Using selective imports can be helpful in resource constrained environments as it enables modern bundlers to tree shake unused types. This can lead to reduced bundle sizes.
651
651
 
652
652
  ```typescript
653
653
  import { Object, Number, String, Boolean, type Static } from '@sinclair/typebox'
@@ -882,6 +882,41 @@ function test(node: Node) {
882
882
  }
883
883
  ```
884
884
 
885
+ <a name='types-template-literal'></a>
886
+
887
+ ### Template Literal Types
888
+
889
+ TypeBox supports template literals with Type.TemplateLiteral. 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 expression patterns which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing, enabling template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
890
+
891
+ ```typescript
892
+ // TypeScript
893
+
894
+ type K = `prop${'A'|'B'|'C'}` // type T = 'propA' | 'propB' | 'propC'
895
+
896
+ type R = Record<K, string> // type R = {
897
+ // propA: string
898
+ // propB: string
899
+ // propC: string
900
+ // }
901
+
902
+ // TypeBox
903
+
904
+ const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLiteral<[
905
+ // TLiteral<'prop'>,
906
+ // TUnion<[
907
+ // TLiteral<'A'>,
908
+ // TLiteral<'B'>,
909
+ // TLiteral<'C'>,
910
+ // ]>
911
+ // ]>
912
+
913
+ const R = Type.Record(K, Type.String()) // const R: TObject<{
914
+ // hello1: TString,
915
+ // hello2: TString,
916
+ // hello3: TString,
917
+ // }>
918
+ ```
919
+
885
920
  <a name='types-conditional'></a>
886
921
 
887
922
  ### Conditional Types
@@ -931,7 +966,7 @@ const T = Type.Exclude( // const T: TUnion<[
931
966
 
932
967
  ### Mapped Types
933
968
 
934
- TypeBox supports Mapped types with Type.Mapped. This type accepts a union of property keys as the first argument, and a type mapping function which accepts each key (`K`) as the second. This type can be used with Conditional and Indexed Access types to allow for generalized property remapping irrespective of property type. The following example remaps each property of `T` to be `T[K] | null`.
969
+ TypeBox supports Mapped types with Type.Mapped. This type accepts two arguments, the first a union of literal keys and the second a type mapping function which receives a mapping key `K`. The following remaps the type `T` to be `Partial<T>` using type mapping.
935
970
 
936
971
  ```typescript
937
972
  const T = Type.Object({ // const T: TObject<{
@@ -940,51 +975,16 @@ const T = Type.Object({ // const T: TObject<{
940
975
  z: Type.Boolean() // z: TBoolean
941
976
  }) // }>
942
977
 
943
- const M = Type.Mapped(Type.KeyOf(T), K => { // type M = {
944
- return Type.Union([ // [K in keyof T]: T[K] | null
945
- Type.Index(T, K), // }
946
- Type.Null() //
947
- ]) // ... runtime mapped as
948
- }) //
978
+ const M = Type.Mapped(Type.KeyOf(T), K => { // type M = { [K in keyof T]?: T[K] }
979
+ return Type.Optional(Type.Index(T, K)) //
980
+ }) //
981
+
982
+ // ... runtime mapped as
983
+ //
949
984
  // const M: TObject<{
950
- // x: TUnion<[TNumber, TNull]>,
951
- // y: TUnion<[TString, TNull]>,
952
- // z: TUnion<[TBoolean, TNull]>
953
- // }>
954
- ```
955
-
956
- <a name='types-templateliteral'></a>
957
-
958
- ### Template Literal Types
959
-
960
- TypeBox supports template literals with Type.TemplateLiteral. 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 expression patterns which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing, enabling template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
961
-
962
- ```typescript
963
- // TypeScript
964
-
965
- type K = `prop${'A'|'B'|'C'}` // type T = 'propA' | 'propB' | 'propC'
966
-
967
- type R = Record<K, string> // type R = {
968
- // propA: string
969
- // propB: string
970
- // propC: string
971
- // }
972
-
973
- // TypeBox
974
-
975
- const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLiteral<[
976
- // TLiteral<'prop'>,
977
- // TUnion<[
978
- // TLiteral<'A'>,
979
- // TLiteral<'B'>,
980
- // TLiteral<'C'>,
981
- // ]>
982
- // ]>
983
-
984
- const R = Type.Record(K, Type.String()) // const R: TObject<{
985
- // hello1: TString,
986
- // hello2: TString,
987
- // hello3: TString,
985
+ // x: TOptional<TNumber>,
986
+ // y: TOptional<TString>,
987
+ // z: TOptional<TBoolean>
988
988
  // }>
989
989
  ```
990
990
 
@@ -1157,13 +1157,13 @@ TypeBox types contain various symbol properties that are used for reflection, co
1157
1157
 
1158
1158
  ```typescript
1159
1159
  const T = Type.Object({ // const T = {
1160
- name: Type.Optional(Type.String()) // [Kind]: 'Object',
1160
+ name: Type.Optional(Type.String()) // [Symbol(TypeBox.Kind)]: 'Object',
1161
1161
  }) // type: 'object',
1162
1162
  // properties: {
1163
1163
  // name: {
1164
1164
  // type: 'string',
1165
- // [Kind]: 'String',
1166
- // [Optional]: 'Optional'
1165
+ // [Symbol(TypeBox.Kind)]: 'String',
1166
+ // [Symbol(TypeBox.Optional)]: 'Optional'
1167
1167
  // }
1168
1168
  // }
1169
1169
  // }