@sinclair/typebox 0.33.22 → 0.34.1

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 (202) hide show
  1. package/build/cjs/compiler/compiler.d.ts +2 -2
  2. package/build/cjs/compiler/compiler.js +13 -2
  3. package/build/cjs/errors/errors.js +7 -0
  4. package/build/cjs/index.d.ts +1 -2
  5. package/build/cjs/index.js +1 -2
  6. package/build/cjs/syntax/parse.d.ts +14 -15
  7. package/build/cjs/syntax/parse.js +6 -5
  8. package/build/cjs/syntax/runtime.d.ts +20 -4
  9. package/build/cjs/syntax/runtime.js +183 -28
  10. package/build/cjs/syntax/static.d.ts +107 -23
  11. package/build/cjs/type/array/array.d.ts +1 -1
  12. package/build/cjs/type/awaited/awaited.d.ts +8 -3
  13. package/build/cjs/type/awaited/awaited.js +20 -20
  14. package/build/cjs/type/computed/computed.d.ts +9 -0
  15. package/build/cjs/type/computed/computed.js +10 -0
  16. package/build/cjs/type/computed/index.d.ts +1 -0
  17. package/build/cjs/type/{deref → computed}/index.js +1 -1
  18. package/build/cjs/type/guard/kind.d.ts +12 -4
  19. package/build/cjs/type/guard/kind.js +15 -0
  20. package/build/cjs/type/guard/type.d.ts +9 -3
  21. package/build/cjs/type/guard/type.js +19 -0
  22. package/build/cjs/type/index.d.ts +1 -2
  23. package/build/cjs/type/index.js +1 -2
  24. package/build/cjs/type/indexed/indexed-from-mapped-key.d.ts +6 -6
  25. package/build/cjs/type/indexed/indexed-from-mapped-key.js +10 -10
  26. package/build/cjs/type/indexed/indexed-from-mapped-result.d.ts +5 -5
  27. package/build/cjs/type/indexed/indexed-from-mapped-result.js +11 -10
  28. package/build/cjs/type/indexed/indexed-property-keys.d.ts +6 -6
  29. package/build/cjs/type/indexed/indexed-property-keys.js +14 -14
  30. package/build/cjs/type/indexed/indexed.d.ts +34 -20
  31. package/build/cjs/type/indexed/indexed.js +59 -46
  32. package/build/cjs/type/intersect/intersect-evaluated.d.ts +6 -6
  33. package/build/cjs/type/intersect/intersect-evaluated.js +16 -16
  34. package/build/cjs/type/intersect/intersect.d.ts +2 -2
  35. package/build/cjs/type/intersect/intersect.js +6 -6
  36. package/build/cjs/type/keyof/keyof-from-mapped-result.d.ts +6 -6
  37. package/build/cjs/type/keyof/keyof-from-mapped-result.js +10 -10
  38. package/build/cjs/type/keyof/keyof-property-keys.d.ts +7 -7
  39. package/build/cjs/type/keyof/keyof-property-keys.js +22 -22
  40. package/build/cjs/type/keyof/keyof.d.ts +11 -6
  41. package/build/cjs/type/keyof/keyof.js +22 -13
  42. package/build/cjs/type/module/compute.d.ts +54 -0
  43. package/build/cjs/type/module/compute.js +154 -0
  44. package/build/cjs/type/module/index.d.ts +1 -0
  45. package/build/cjs/type/{strict → module}/index.js +1 -1
  46. package/build/cjs/type/module/infer.d.ts +43 -0
  47. package/build/cjs/type/module/infer.js +3 -0
  48. package/build/cjs/type/module/module.d.ts +27 -0
  49. package/build/cjs/type/module/module.js +37 -0
  50. package/build/cjs/type/not/not.d.ts +1 -1
  51. package/build/cjs/type/not/not.js +2 -2
  52. package/build/cjs/type/omit/omit-from-mapped-key.d.ts +6 -6
  53. package/build/cjs/type/omit/omit-from-mapped-key.js +10 -12
  54. package/build/cjs/type/omit/omit-from-mapped-result.d.ts +5 -5
  55. package/build/cjs/type/omit/omit-from-mapped-result.js +10 -10
  56. package/build/cjs/type/omit/omit.d.ts +26 -14
  57. package/build/cjs/type/omit/omit.js +46 -35
  58. package/build/cjs/type/partial/partial.d.ts +11 -7
  59. package/build/cjs/type/partial/partial.js +36 -24
  60. package/build/cjs/type/pick/pick-from-mapped-key.d.ts +6 -6
  61. package/build/cjs/type/pick/pick-from-mapped-key.js +10 -10
  62. package/build/cjs/type/pick/pick-from-mapped-result.d.ts +5 -5
  63. package/build/cjs/type/pick/pick-from-mapped-result.js +10 -10
  64. package/build/cjs/type/pick/pick.d.ts +25 -15
  65. package/build/cjs/type/pick/pick.js +47 -36
  66. package/build/cjs/type/record/record.d.ts +4 -2
  67. package/build/cjs/type/record/record.js +31 -28
  68. package/build/cjs/type/ref/ref.d.ts +4 -7
  69. package/build/cjs/type/ref/ref.js +3 -11
  70. package/build/cjs/type/required/required.d.ts +12 -8
  71. package/build/cjs/type/required/required.js +37 -25
  72. package/build/cjs/type/static/static.d.ts +1 -1
  73. package/build/cjs/type/tuple/tuple.d.ts +1 -1
  74. package/build/cjs/type/tuple/tuple.js +4 -4
  75. package/build/cjs/type/type/javascript.d.ts +10 -10
  76. package/build/cjs/type/type/javascript.js +6 -6
  77. package/build/cjs/type/type/json.d.ts +45 -68
  78. package/build/cjs/type/type/json.js +82 -98
  79. package/build/cjs/type/type/type.d.ts +1 -2
  80. package/build/cjs/type/type/type.js +55 -57
  81. package/build/cjs/type/union/union-evaluated.d.ts +6 -6
  82. package/build/cjs/type/union/union-evaluated.js +11 -10
  83. package/build/cjs/type/union/union.d.ts +1 -1
  84. package/build/cjs/type/union/union.js +4 -4
  85. package/build/cjs/value/cast/cast.js +8 -1
  86. package/build/cjs/value/check/check.js +11 -4
  87. package/build/cjs/value/clean/clean.js +8 -1
  88. package/build/cjs/value/convert/convert.js +7 -0
  89. package/build/cjs/value/create/create.js +7 -0
  90. package/build/cjs/value/default/default.js +7 -0
  91. package/build/cjs/value/transform/decode.js +18 -7
  92. package/build/cjs/value/transform/encode.js +18 -7
  93. package/build/cjs/value/transform/has.js +18 -18
  94. package/build/esm/compiler/compiler.d.mts +2 -2
  95. package/build/esm/compiler/compiler.mjs +13 -2
  96. package/build/esm/errors/errors.mjs +7 -0
  97. package/build/esm/index.d.mts +1 -2
  98. package/build/esm/index.mjs +1 -2
  99. package/build/esm/syntax/parse.d.mts +14 -15
  100. package/build/esm/syntax/parse.mjs +6 -5
  101. package/build/esm/syntax/runtime.d.mts +20 -4
  102. package/build/esm/syntax/runtime.mjs +183 -28
  103. package/build/esm/syntax/static.d.mts +107 -23
  104. package/build/esm/type/array/array.d.mts +1 -1
  105. package/build/esm/type/awaited/awaited.d.mts +8 -3
  106. package/build/esm/type/awaited/awaited.mjs +20 -20
  107. package/build/esm/type/computed/computed.d.mts +9 -0
  108. package/build/esm/type/computed/computed.mjs +6 -0
  109. package/build/esm/type/computed/index.d.mts +1 -0
  110. package/build/esm/type/computed/index.mjs +1 -0
  111. package/build/esm/type/guard/kind.d.mts +12 -4
  112. package/build/esm/type/guard/kind.mjs +12 -0
  113. package/build/esm/type/guard/type.d.mts +9 -3
  114. package/build/esm/type/guard/type.mjs +17 -0
  115. package/build/esm/type/index.d.mts +1 -2
  116. package/build/esm/type/index.mjs +1 -2
  117. package/build/esm/type/indexed/indexed-from-mapped-key.d.mts +6 -6
  118. package/build/esm/type/indexed/indexed-from-mapped-key.mjs +10 -10
  119. package/build/esm/type/indexed/indexed-from-mapped-result.d.mts +5 -5
  120. package/build/esm/type/indexed/indexed-from-mapped-result.mjs +11 -10
  121. package/build/esm/type/indexed/indexed-property-keys.d.mts +6 -6
  122. package/build/esm/type/indexed/indexed-property-keys.mjs +14 -14
  123. package/build/esm/type/indexed/indexed.d.mts +34 -20
  124. package/build/esm/type/indexed/indexed.mjs +55 -42
  125. package/build/esm/type/intersect/intersect-evaluated.d.mts +6 -6
  126. package/build/esm/type/intersect/intersect-evaluated.mjs +16 -16
  127. package/build/esm/type/intersect/intersect.d.mts +2 -2
  128. package/build/esm/type/intersect/intersect.mjs +6 -6
  129. package/build/esm/type/keyof/keyof-from-mapped-result.d.mts +6 -6
  130. package/build/esm/type/keyof/keyof-from-mapped-result.mjs +10 -10
  131. package/build/esm/type/keyof/keyof-property-keys.d.mts +7 -7
  132. package/build/esm/type/keyof/keyof-property-keys.mjs +22 -22
  133. package/build/esm/type/keyof/keyof.d.mts +11 -6
  134. package/build/esm/type/keyof/keyof.mjs +22 -13
  135. package/build/esm/type/module/compute.d.mts +54 -0
  136. package/build/esm/type/module/compute.mjs +148 -0
  137. package/build/esm/type/module/index.d.mts +1 -0
  138. package/build/esm/type/module/index.mjs +1 -0
  139. package/build/esm/type/module/infer.d.mts +43 -0
  140. package/build/esm/type/module/infer.mjs +1 -0
  141. package/build/esm/type/module/module.d.mts +27 -0
  142. package/build/esm/type/module/module.mjs +31 -0
  143. package/build/esm/type/not/not.d.mts +1 -1
  144. package/build/esm/type/not/not.mjs +2 -2
  145. package/build/esm/type/omit/omit-from-mapped-key.d.mts +6 -6
  146. package/build/esm/type/omit/omit-from-mapped-key.mjs +10 -12
  147. package/build/esm/type/omit/omit-from-mapped-result.d.mts +5 -5
  148. package/build/esm/type/omit/omit-from-mapped-result.mjs +10 -10
  149. package/build/esm/type/omit/omit.d.mts +26 -14
  150. package/build/esm/type/omit/omit.mjs +43 -32
  151. package/build/esm/type/partial/partial.d.mts +11 -7
  152. package/build/esm/type/partial/partial.mjs +29 -17
  153. package/build/esm/type/pick/pick-from-mapped-key.d.mts +6 -6
  154. package/build/esm/type/pick/pick-from-mapped-key.mjs +10 -10
  155. package/build/esm/type/pick/pick-from-mapped-result.d.mts +5 -5
  156. package/build/esm/type/pick/pick-from-mapped-result.mjs +10 -10
  157. package/build/esm/type/pick/pick.d.mts +25 -15
  158. package/build/esm/type/pick/pick.mjs +43 -32
  159. package/build/esm/type/record/record.d.mts +4 -2
  160. package/build/esm/type/record/record.mjs +15 -12
  161. package/build/esm/type/ref/ref.d.mts +4 -7
  162. package/build/esm/type/ref/ref.mjs +3 -11
  163. package/build/esm/type/required/required.d.mts +12 -8
  164. package/build/esm/type/required/required.mjs +33 -21
  165. package/build/esm/type/static/static.d.mts +1 -1
  166. package/build/esm/type/tuple/tuple.d.mts +1 -1
  167. package/build/esm/type/tuple/tuple.mjs +4 -4
  168. package/build/esm/type/type/javascript.d.mts +10 -10
  169. package/build/esm/type/type/javascript.mjs +6 -6
  170. package/build/esm/type/type/json.d.mts +45 -68
  171. package/build/esm/type/type/json.mjs +47 -63
  172. package/build/esm/type/type/type.d.mts +1 -2
  173. package/build/esm/type/type/type.mjs +1 -2
  174. package/build/esm/type/union/union-evaluated.d.mts +6 -6
  175. package/build/esm/type/union/union-evaluated.mjs +11 -10
  176. package/build/esm/type/union/union.d.mts +1 -1
  177. package/build/esm/type/union/union.mjs +4 -4
  178. package/build/esm/value/cast/cast.mjs +9 -2
  179. package/build/esm/value/check/check.mjs +11 -4
  180. package/build/esm/value/clean/clean.mjs +9 -2
  181. package/build/esm/value/convert/convert.mjs +7 -0
  182. package/build/esm/value/create/create.mjs +7 -0
  183. package/build/esm/value/default/default.mjs +7 -0
  184. package/build/esm/value/transform/decode.mjs +13 -2
  185. package/build/esm/value/transform/encode.mjs +13 -2
  186. package/build/esm/value/transform/has.mjs +2 -2
  187. package/package.json +1 -1
  188. package/readme.md +230 -97
  189. package/build/cjs/type/deref/deref.d.ts +0 -22
  190. package/build/cjs/type/deref/deref.js +0 -106
  191. package/build/cjs/type/deref/index.d.ts +0 -1
  192. package/build/cjs/type/strict/index.d.ts +0 -1
  193. package/build/cjs/type/strict/strict.d.ts +0 -12
  194. package/build/cjs/type/strict/strict.js +0 -16
  195. package/build/esm/type/deref/deref.d.mts +0 -22
  196. package/build/esm/type/deref/deref.mjs +0 -102
  197. package/build/esm/type/deref/index.d.mts +0 -1
  198. package/build/esm/type/deref/index.mjs +0 -1
  199. package/build/esm/type/strict/index.d.mts +0 -1
  200. package/build/esm/type/strict/index.mjs +0 -1
  201. package/build/esm/type/strict/strict.d.mts +0 -12
  202. package/build/esm/type/strict/strict.mjs +0 -12
@@ -1,7 +1,7 @@
1
1
  import { ValueErrorIterator } from '../errors/index';
2
2
  import { TypeBoxError } from '../type/error/index';
3
3
  import type { TSchema } from '../type/schema/index';
4
- import type { Static, StaticDecode } from '../type/static/index';
4
+ import type { Static, StaticDecode, StaticEncode } from '../type/static/index';
5
5
  export type CheckFunction = (value: unknown) => boolean;
6
6
  export declare class TypeCheck<T extends TSchema> {
7
7
  private readonly schema;
@@ -19,7 +19,7 @@ export declare class TypeCheck<T extends TSchema> {
19
19
  /** Decodes a value or throws if error */
20
20
  Decode<Static = StaticDecode<T>, Result extends Static = Static>(value: unknown): Result;
21
21
  /** Encodes a value or throws if error */
22
- Encode<Static = StaticDecode<T>, Result extends Static = Static>(value: unknown): Result;
22
+ Encode<Static = StaticEncode<T>, Result extends Static = Static>(value: unknown): Result;
23
23
  }
24
24
  export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
25
25
  readonly schema: TSchema;
@@ -262,6 +262,11 @@ var TypeCompiler;
262
262
  function* FromFunction(schema, references, value) {
263
263
  yield `(typeof ${value} === 'function')`;
264
264
  }
265
+ function* FromImport(schema, references, value) {
266
+ const definitions = globalThis.Object.values(schema.$defs);
267
+ const target = schema.$defs[schema.$ref];
268
+ yield* Visit(target, [...references, ...definitions], value);
269
+ }
265
270
  function* FromInteger(schema, references, value) {
266
271
  yield `Number.isInteger(${value})`;
267
272
  if ((0, index_11.IsNumber)(schema.exclusiveMaximum))
@@ -379,9 +384,13 @@ var TypeCompiler;
379
384
  function* FromRef(schema, references, value) {
380
385
  const target = (0, index_5.Deref)(schema, references);
381
386
  // Reference: If we have seen this reference before we can just yield and return the function call.
382
- // If this isn't the case we defer to visit to generate and set the function for subsequent passes.
383
- if (state.functions.has(schema.$ref))
387
+ // If this isn't the case we defer to visit to generate and set the _recursion_end_for_ for subsequent
388
+ // passes. This operation is very awkward as we are using the functions state to store values to
389
+ // enable self referential types to terminate. This needs to be refactored.
390
+ const recursiveEnd = `_recursion_end_for_${schema.$ref}`;
391
+ if (state.functions.has(recursiveEnd))
384
392
  return yield `${CreateFunctionName(schema.$ref)}(${value})`;
393
+ state.functions.set(recursiveEnd, ''); // terminate recursion here by setting the name.
385
394
  yield* Visit(target, references, value);
386
395
  }
387
396
  function* FromRegExp(schema, references, value) {
@@ -488,6 +497,8 @@ var TypeCompiler;
488
497
  return yield* FromDate(schema_, references_, value);
489
498
  case 'Function':
490
499
  return yield* FromFunction(schema_, references_, value);
500
+ case 'Import':
501
+ return yield* FromImport(schema_, references_, value);
491
502
  case 'Integer':
492
503
  return yield* FromInteger(schema_, references_, value);
493
504
  case 'Intersect':
@@ -239,6 +239,11 @@ function* FromFunction(schema, references, path, value) {
239
239
  if (!(0, index_10.IsFunction)(value))
240
240
  yield Create(ValueErrorType.Function, schema, path, value);
241
241
  }
242
+ function* FromImport(schema, references, path, value) {
243
+ const definitions = globalThis.Object.values(schema.$defs);
244
+ const target = schema.$defs[schema.$ref];
245
+ yield* Visit(target, [...references, ...definitions], path, value);
246
+ }
242
247
  function* FromInteger(schema, references, path, value) {
243
248
  if (!(0, index_10.IsInteger)(value))
244
249
  return yield Create(ValueErrorType.Integer, schema, path, value);
@@ -530,6 +535,8 @@ function* Visit(schema, references, path, value) {
530
535
  return yield* FromDate(schema_, references_, path, value);
531
536
  case 'Function':
532
537
  return yield* FromFunction(schema_, references_, path, value);
538
+ case 'Import':
539
+ return yield* FromImport(schema_, references_, path, value);
533
540
  case 'Integer':
534
541
  return yield* FromInteger(schema_, references_, path, value);
535
542
  case 'Intersect':
@@ -18,7 +18,6 @@ export * from './type/const/index';
18
18
  export * from './type/constructor/index';
19
19
  export * from './type/constructor-parameters/index';
20
20
  export * from './type/date/index';
21
- export * from './type/deref/index';
22
21
  export * from './type/enum/index';
23
22
  export * from './type/exclude/index';
24
23
  export * from './type/extends/index';
@@ -32,6 +31,7 @@ export * from './type/iterator/index';
32
31
  export * from './type/intrinsic/index';
33
32
  export * from './type/keyof/index';
34
33
  export * from './type/literal/index';
34
+ export * from './type/module/index';
35
35
  export * from './type/mapped/index';
36
36
  export * from './type/never/index';
37
37
  export * from './type/not/index';
@@ -55,7 +55,6 @@ export * from './type/rest/index';
55
55
  export * from './type/return-type/index';
56
56
  export * from './type/schema/index';
57
57
  export * from './type/static/index';
58
- export * from './type/strict/index';
59
58
  export * from './type/string/index';
60
59
  export * from './type/symbol/index';
61
60
  export * from './type/template-literal/index';
@@ -41,7 +41,6 @@ __exportStar(require("./type/const/index"), exports);
41
41
  __exportStar(require("./type/constructor/index"), exports);
42
42
  __exportStar(require("./type/constructor-parameters/index"), exports);
43
43
  __exportStar(require("./type/date/index"), exports);
44
- __exportStar(require("./type/deref/index"), exports);
45
44
  __exportStar(require("./type/enum/index"), exports);
46
45
  __exportStar(require("./type/exclude/index"), exports);
47
46
  __exportStar(require("./type/extends/index"), exports);
@@ -55,6 +54,7 @@ __exportStar(require("./type/iterator/index"), exports);
55
54
  __exportStar(require("./type/intrinsic/index"), exports);
56
55
  __exportStar(require("./type/keyof/index"), exports);
57
56
  __exportStar(require("./type/literal/index"), exports);
57
+ __exportStar(require("./type/module/index"), exports);
58
58
  __exportStar(require("./type/mapped/index"), exports);
59
59
  __exportStar(require("./type/never/index"), exports);
60
60
  __exportStar(require("./type/not/index"), exports);
@@ -78,7 +78,6 @@ __exportStar(require("./type/rest/index"), exports);
78
78
  __exportStar(require("./type/return-type/index"), exports);
79
79
  __exportStar(require("./type/schema/index"), exports);
80
80
  __exportStar(require("./type/static/index"), exports);
81
- __exportStar(require("./type/strict/index"), exports);
82
81
  __exportStar(require("./type/string/index"), exports);
83
82
  __exportStar(require("./type/symbol/index"), exports);
84
83
  __exportStar(require("./type/template-literal/index"), exports);
@@ -1,16 +1,15 @@
1
+ import * as Types from '../type/index';
1
2
  import { Static } from './parsebox/index';
2
- import { TSchema, SchemaOptions } from '../type/schema/index';
3
- import { StaticDecode } from '../type/static/index';
4
- import { Type } from './static';
5
- /** `[Experimental]` Infers a TypeBox type from TypeScript syntax. */
6
- export type StaticParseAsSchema<Context extends Record<PropertyKey, TSchema>, Code extends string> = Static.Parse<Type, Code, Context>[0];
7
- /** `[Experimental]` Infers a TypeScript type from TypeScript syntax. */
8
- export type StaticParseAsType<Context extends Record<PropertyKey, TSchema>, Code extends string> = StaticParseAsSchema<Context, Code> extends infer Type extends TSchema ? StaticDecode<Type> : undefined;
9
- /** `[Experimental]` Parses a TypeBox type from TypeScript syntax. */
10
- export declare function Parse<Context extends Record<PropertyKey, TSchema>, Code extends string>(context: Context, code: Code, options?: SchemaOptions): StaticParseAsSchema<Context, Code>;
11
- /** `[Experimental]` Parses a TypeBox type from TypeScript syntax. */
12
- export declare function Parse<Code extends string>(code: Code, options?: SchemaOptions): StaticParseAsSchema<{}, Code>;
13
- /** `[Experimental]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
14
- export declare function ParseOnly<Context extends Record<PropertyKey, TSchema>, Code extends string>(context: Context, code: Code, options?: SchemaOptions): TSchema | undefined;
15
- /** `[Experimental]` Parses a TypeBox TSchema from TypeScript syntax */
16
- export declare function ParseOnly<Code extends string>(code: Code, options?: SchemaOptions): TSchema | undefined;
3
+ import { Main } from './static';
4
+ /** `[Syntax]` Infers a TypeBox type from TypeScript syntax. */
5
+ export type StaticParseAsSchema<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = Static.Parse<Main, Code, Context>[0];
6
+ /** `[Syntax]` Infers a TypeScript type from TypeScript syntax. */
7
+ export type StaticParseAsType<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = StaticParseAsSchema<Context, Code> extends infer Type extends Types.TSchema ? Types.StaticDecode<Type> : undefined;
8
+ /** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
9
+ export declare function Parse<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<Context, Code>;
10
+ /** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
11
+ export declare function Parse<Code extends string>(code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<{}, Code>;
12
+ /** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
13
+ export declare function ParseOnly<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined;
14
+ /** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax */
15
+ export declare function ParseOnly<Code extends string>(code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined;
@@ -3,16 +3,17 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Parse = Parse;
5
5
  exports.ParseOnly = ParseOnly;
6
- const type_1 = require("../type/create/type");
6
+ const Types = require("../type/index");
7
7
  const runtime_1 = require("./runtime");
8
- /** `[Experimental]` Parses a TypeBox type from TypeScript syntax. */
8
+ /** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
9
9
  function Parse(...args) {
10
10
  return ParseOnly.apply(null, args);
11
11
  }
12
- /** `[Experimental]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
12
+ /** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
13
13
  function ParseOnly(...args) {
14
14
  const withContext = typeof args[0] === 'string' ? false : true;
15
15
  const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
16
- const type = runtime_1.Module.Parse('Type', code, context)[0];
17
- return (type !== undefined ? (0, type_1.CreateType)(type, options) : undefined);
16
+ const type = runtime_1.Module.Parse('Main', code, context)[0];
17
+ // Note: Parsing may return either a ModuleInstance or Type. We only apply options on the Type.
18
+ return Types.KindGuard.IsSchema(type) ? Types.CloneType(type, options) : type;
18
19
  }
@@ -1,6 +1,21 @@
1
1
  import { Runtime } from './parsebox/index';
2
2
  import * as Types from '../type/index';
3
3
  export declare const Module: Runtime.Module<{
4
+ ExportModifier: Runtime.IUnion<boolean>;
5
+ HeritageList: Runtime.IUnion<Types.TSchema[]>;
6
+ Heritage: Runtime.IUnion<unknown[]>;
7
+ InterfaceDeclaration: Runtime.ITuple<{
8
+ [x: string]: Types.TIntersect<[...Types.TRef<string>[], Types.TObject<Types.TProperties>]>;
9
+ }>;
10
+ TypeAliasDeclaration: Runtime.ITuple<{
11
+ [x: string]: Types.TSchema;
12
+ }>;
13
+ ModuleType: Runtime.IUnion<unknown>;
14
+ ModuleProperties: Runtime.IUnion<Types.TProperties>;
15
+ ModuleDeclaration: Runtime.ITuple<Types.TModule<Types.TProperties, {
16
+ [x: string]: Types.TSchema;
17
+ [x: number]: Types.TSchema;
18
+ }>>;
4
19
  Literal: Runtime.IUnion<Types.TLiteral<string> | Types.TLiteral<number> | Types.TLiteral<boolean>>;
5
20
  Keyword: Runtime.IUnion<Types.TAny | Types.TNever | Types.TString | Types.TBoolean | Types.TNumber | Types.TInteger | Types.TBigInt | Types.TNull | Types.TSymbol | Types.TUndefined | Types.TUnknown | Types.TVoid>;
6
21
  KeyOf: Runtime.IUnion<boolean>;
@@ -14,14 +29,14 @@ export declare const Module: Runtime.Module<{
14
29
  Expr: Runtime.ITuple<Types.TSchema>;
15
30
  Type: Runtime.IRef<unknown>;
16
31
  PropertyKey: Runtime.IUnion<string>;
17
- PropertyReadonly: Runtime.IUnion<boolean>;
18
- PropertyOptional: Runtime.IUnion<boolean>;
32
+ Readonly: Runtime.IUnion<boolean>;
33
+ Optional: Runtime.IUnion<boolean>;
19
34
  Property: Runtime.ITuple<{
20
35
  [x: string]: Types.TSchema;
21
36
  }>;
22
37
  PropertyDelimiter: Runtime.IUnion<[","] | [",", "\n"] | [";"] | [";", "\n"] | ["\n"]>;
23
- Properties: Runtime.IUnion<unknown[]>;
24
- Object: Runtime.ITuple<Types.TObject<Record<string, Types.TSchema>>>;
38
+ Properties: Runtime.IUnion<Types.TProperties>;
39
+ Object: Runtime.ITuple<Types.TObject<Types.TProperties>>;
25
40
  Elements: Runtime.IUnion<unknown[]>;
26
41
  Tuple: Runtime.ITuple<Types.TTuple<Types.TSchema[]>>;
27
42
  Parameter: Runtime.ITuple<Types.TSchema>;
@@ -52,4 +67,5 @@ export declare const Module: Runtime.Module<{
52
67
  Date: Runtime.IConst<Types.TDate>;
53
68
  Uint8Array: Runtime.IConst<Types.TUint8Array>;
54
69
  Reference: Runtime.IIdent<Types.TSchema>;
70
+ Main: Runtime.IUnion<unknown>;
55
71
  }>;
@@ -23,6 +23,7 @@ const SemiColon = ';';
23
23
  const SingleQuote = "'";
24
24
  const DoubleQuote = '"';
25
25
  const Tilde = '`';
26
+ const Equals = '=';
26
27
  // ------------------------------------------------------------------
27
28
  // DestructureRight
28
29
  // ------------------------------------------------------------------
@@ -33,12 +34,133 @@ function DestructureRight(values) {
33
34
  : [values, undefined];
34
35
  }
35
36
  // ------------------------------------------------------------------
37
+ // Deref
38
+ // ------------------------------------------------------------------
39
+ const Deref = (context, key) => {
40
+ return key in context ? context[key] : Types.Ref(key);
41
+ };
42
+ // ------------------------------------------------------------------
43
+ // ExportModifier
44
+ // ------------------------------------------------------------------
45
+ // prettier-ignore
46
+ const ExportModifierMapping = (values) => {
47
+ return values.length === 1;
48
+ };
49
+ // prettier-ignore
50
+ const ExportModifier = index_1.Runtime.Union([
51
+ index_1.Runtime.Tuple([index_1.Runtime.Const('export')]), index_1.Runtime.Tuple([])
52
+ ], ExportModifierMapping);
53
+ // ------------------------------------------------------------------
54
+ // TypeAliasDeclaration
55
+ // ------------------------------------------------------------------
56
+ // prettier-ignore
57
+ const TypeAliasDeclarationMapping = (_Export, _Keyword, Ident, _Equals, Type) => {
58
+ return { [Ident]: Type };
59
+ };
60
+ // prettier-ignore
61
+ const TypeAliasDeclaration = index_1.Runtime.Tuple([
62
+ index_1.Runtime.Ref('ExportModifier'),
63
+ index_1.Runtime.Const('type'),
64
+ index_1.Runtime.Ident(),
65
+ index_1.Runtime.Const(Equals),
66
+ index_1.Runtime.Ref('Type')
67
+ ], value => TypeAliasDeclarationMapping(...value));
68
+ // ------------------------------------------------------------------
69
+ // HeritageList
70
+ // ------------------------------------------------------------------
71
+ // prettier-ignore (note, heritage list should disallow trailing comma)
72
+ const HeritageListDelimiter = index_1.Runtime.Union([index_1.Runtime.Tuple([index_1.Runtime.Const(Comma), index_1.Runtime.Const(Newline)]), index_1.Runtime.Tuple([index_1.Runtime.Const(Comma)])]);
73
+ // prettier-ignore
74
+ const HeritageListMapping = (values, context) => {
75
+ return (values.length === 3 ? [Deref(context, values[0]), ...values[2]] :
76
+ values.length === 1 ? [Deref(context, values[0])] :
77
+ []);
78
+ };
79
+ // prettier-ignore
80
+ const HeritageList = index_1.Runtime.Union([
81
+ index_1.Runtime.Tuple([index_1.Runtime.Ident(), HeritageListDelimiter, index_1.Runtime.Ref('HeritageList')]),
82
+ index_1.Runtime.Tuple([index_1.Runtime.Ident()]),
83
+ index_1.Runtime.Tuple([])
84
+ ], HeritageListMapping);
85
+ // ------------------------------------------------------------------
86
+ // Heritage
87
+ // ------------------------------------------------------------------
88
+ // prettier-ignore
89
+ const HeritageMapping = (values) => {
90
+ return (values.length === 2 ? values[1] : []);
91
+ };
92
+ // prettier-ignore
93
+ const Heritage = index_1.Runtime.Union([
94
+ index_1.Runtime.Tuple([index_1.Runtime.Const('extends'), index_1.Runtime.Ref('HeritageList')]),
95
+ index_1.Runtime.Tuple([])
96
+ ], HeritageMapping);
97
+ // ------------------------------------------------------------------
98
+ // InterfaceDeclaration
99
+ // ------------------------------------------------------------------
100
+ // prettier-ignore
101
+ const InterfaceDeclarationMapping = (_0, _1, Ident, Heritage, _4, Properties, _6) => {
102
+ return { [Ident]: Types.Intersect([...Heritage, Types.Object(Properties)]) };
103
+ };
104
+ // prettier-ignore
105
+ const InterfaceDeclaration = index_1.Runtime.Tuple([
106
+ index_1.Runtime.Ref('ExportModifier'),
107
+ index_1.Runtime.Const('interface'),
108
+ index_1.Runtime.Ident(),
109
+ index_1.Runtime.Ref('Heritage'),
110
+ index_1.Runtime.Const(LBrace),
111
+ index_1.Runtime.Ref('Properties'),
112
+ index_1.Runtime.Const(RBrace),
113
+ ], values => InterfaceDeclarationMapping(...values));
114
+ // ------------------------------------------------------------------
115
+ // ModuleType
116
+ // ------------------------------------------------------------------
117
+ // prettier-ignore
118
+ const ModuleType = index_1.Runtime.Union([
119
+ index_1.Runtime.Ref('InterfaceDeclaration'),
120
+ index_1.Runtime.Ref('TypeAliasDeclaration')
121
+ ]);
122
+ // ------------------------------------------------------------------
123
+ // ModuleProperties
124
+ // ------------------------------------------------------------------
125
+ // prettier-ignore
126
+ const ModulePropertiesDelimiter = index_1.Runtime.Union([
127
+ index_1.Runtime.Tuple([index_1.Runtime.Const(SemiColon), index_1.Runtime.Const(Newline)]),
128
+ index_1.Runtime.Tuple([index_1.Runtime.Const(SemiColon)]),
129
+ index_1.Runtime.Tuple([index_1.Runtime.Const(Newline)]),
130
+ ]);
131
+ // prettier-ignore
132
+ const ModulePropertiesMapping = (values) => {
133
+ return (values.length === 3 ? { ...values[0], ...values[2] } :
134
+ values.length === 1 ? values[0] :
135
+ {});
136
+ };
137
+ // prettier-ignore
138
+ const ModuleProperties = index_1.Runtime.Union([
139
+ index_1.Runtime.Tuple([index_1.Runtime.Ref('ModuleType'), ModulePropertiesDelimiter, index_1.Runtime.Ref('ModuleProperties')]),
140
+ index_1.Runtime.Tuple([index_1.Runtime.Ref('ModuleType')]),
141
+ index_1.Runtime.Tuple([]),
142
+ ], ModulePropertiesMapping);
143
+ // ------------------------------------------------------------------
144
+ // ModuleDeclaration
145
+ // ------------------------------------------------------------------
146
+ // prettier-ignore
147
+ const ModuleIdentifier = index_1.Runtime.Union([
148
+ index_1.Runtime.Tuple([index_1.Runtime.Ident()]),
149
+ index_1.Runtime.Tuple([])
150
+ ]);
151
+ // prettier-ignore
152
+ const ModuleDeclarationMapping = (_1, _2, _Ident, _3, Properties, _5) => {
153
+ return Types.Module(Properties);
154
+ };
155
+ // prettier-ignore
156
+ const ModuleDeclaration = index_1.Runtime.Tuple([
157
+ index_1.Runtime.Ref('ExportModifier'), index_1.Runtime.Const('module'), ModuleIdentifier, index_1.Runtime.Const(LBrace), index_1.Runtime.Ref('ModuleProperties'), index_1.Runtime.Const(RBrace)
158
+ ], values => ModuleDeclarationMapping(...values));
159
+ // ------------------------------------------------------------------
36
160
  // Reference
37
161
  // ------------------------------------------------------------------
38
162
  // prettier-ignore
39
- const Reference = index_1.Runtime.Ident((value, context) => {
40
- return value in context ? context[value] : Types.Ref(value);
41
- });
163
+ const Reference = index_1.Runtime.Ident((value, context) => Deref(context, value));
42
164
  // ------------------------------------------------------------------
43
165
  // Literal
44
166
  // ------------------------------------------------------------------
@@ -161,7 +283,9 @@ const FactorExtends = (Type, Extends) => {
161
283
  // prettier-ignore
162
284
  const FactorIndexArray = (Type, IndexArray) => {
163
285
  const [Left, Right] = DestructureRight(IndexArray);
164
- return (!Types.ValueGuard.IsUndefined(Right) ? (Right.length === 1 ? Types.Index(FactorIndexArray(Type, Left), Right[0]) :
286
+ return (!Types.ValueGuard.IsUndefined(Right) ? (
287
+ // note: Indexed types require reimplementation to replace `[number]` indexers
288
+ Right.length === 1 ? Types.Index(FactorIndexArray(Type, Left), Right[0]) :
165
289
  Right.length === 0 ? Types.Array(FactorIndexArray(Type, Left)) :
166
290
  Types.Never()) : Type);
167
291
  };
@@ -224,24 +348,21 @@ const Type = index_1.Runtime.Ref('Expr');
224
348
  // ------------------------------------------------------------------
225
349
  // Properties
226
350
  // ------------------------------------------------------------------
227
- // prettier-ignore
228
351
  const PropertyKey = index_1.Runtime.Union([index_1.Runtime.Ident(), index_1.Runtime.String([SingleQuote, DoubleQuote])]);
352
+ const Readonly = index_1.Runtime.Union([index_1.Runtime.Tuple([index_1.Runtime.Const('readonly')]), index_1.Runtime.Tuple([])], (value) => value.length > 0);
353
+ const Optional = index_1.Runtime.Union([index_1.Runtime.Tuple([index_1.Runtime.Const(Question)]), index_1.Runtime.Tuple([])], (value) => value.length > 0);
229
354
  // prettier-ignore
230
- const PropertyReadonly = index_1.Runtime.Union([index_1.Runtime.Tuple([index_1.Runtime.Const('readonly')]), index_1.Runtime.Tuple([])], value => value.length > 0);
231
- // prettier-ignore
232
- const PropertyOptional = index_1.Runtime.Union([index_1.Runtime.Tuple([index_1.Runtime.Const(Question)]), index_1.Runtime.Tuple([])], value => value.length > 0);
233
- // prettier-ignore
234
- const PropertyMapping = (Readonly, Key, Optional, _, Type) => ({
235
- [Key]: (Readonly && Optional ? Types.ReadonlyOptional(Type) :
236
- Readonly && !Optional ? Types.Readonly(Type) :
237
- !Readonly && Optional ? Types.Optional(Type) :
355
+ const PropertyMapping = (IsReadonly, Key, IsOptional, _, Type) => ({
356
+ [Key]: (IsReadonly && IsOptional ? Types.ReadonlyOptional(Type) :
357
+ IsReadonly && !IsOptional ? Types.Readonly(Type) :
358
+ !IsReadonly && IsOptional ? Types.Optional(Type) :
238
359
  Type)
239
360
  });
240
361
  // prettier-ignore
241
362
  const Property = index_1.Runtime.Tuple([
242
- index_1.Runtime.Ref('PropertyReadonly'),
363
+ index_1.Runtime.Ref('Readonly'),
243
364
  index_1.Runtime.Ref('PropertyKey'),
244
- index_1.Runtime.Ref('PropertyOptional'),
365
+ index_1.Runtime.Ref('Optional'),
245
366
  index_1.Runtime.Const(Colon),
246
367
  index_1.Runtime.Ref('Type'),
247
368
  ], value => PropertyMapping(...value));
@@ -259,23 +380,21 @@ const Properties = index_1.Runtime.Union([
259
380
  index_1.Runtime.Tuple([index_1.Runtime.Ref('Property'), index_1.Runtime.Ref('PropertyDelimiter')]),
260
381
  index_1.Runtime.Tuple([index_1.Runtime.Ref('Property')]),
261
382
  index_1.Runtime.Tuple([])
262
- ], values => (values.length === 3 ? [values[0], ...values[2]] :
263
- values.length === 2 ? [values[0]] :
264
- values.length === 1 ? [values[0]] :
265
- []));
383
+ ], values => (values.length === 3 ? { ...values[0], ...values[2] } :
384
+ values.length === 2 ? values[0] :
385
+ values.length === 1 ? values[0] :
386
+ {}));
266
387
  // ------------------------------------------------------------------
267
388
  // Object
268
389
  // ------------------------------------------------------------------
269
390
  // prettier-ignore
270
- const ObjectMapping = (values) => Types.Object(values.reduce((properties, record) => {
271
- return { ...properties, ...record };
272
- }, {}));
391
+ const ObjectMapping = (_0, Properties, _2) => Types.Object(Properties);
273
392
  // prettier-ignore
274
393
  const _Object = index_1.Runtime.Tuple([
275
394
  index_1.Runtime.Const(LBrace),
276
395
  index_1.Runtime.Ref('Properties'),
277
396
  index_1.Runtime.Const(RBrace)
278
- ], values => ObjectMapping(values[1]));
397
+ ], values => ObjectMapping(...values));
279
398
  // ------------------------------------------------------------------
280
399
  // Tuple
281
400
  // ------------------------------------------------------------------
@@ -344,7 +463,15 @@ const MappedMapping = (values) => {
344
463
  };
345
464
  // prettier-ignore
346
465
  const Mapped = index_1.Runtime.Tuple([
347
- index_1.Runtime.Const(LBrace), index_1.Runtime.Const(LBracket), index_1.Runtime.Ident(), index_1.Runtime.Const('in'), index_1.Runtime.Ref('Type'), index_1.Runtime.Const(RBracket), index_1.Runtime.Const(Colon), index_1.Runtime.Ref('Type'), index_1.Runtime.Const(RBrace)
466
+ index_1.Runtime.Const(LBrace),
467
+ index_1.Runtime.Const(LBracket),
468
+ index_1.Runtime.Ident(),
469
+ index_1.Runtime.Const('in'),
470
+ index_1.Runtime.Ref('Type'),
471
+ index_1.Runtime.Const(RBracket),
472
+ index_1.Runtime.Const(Colon),
473
+ index_1.Runtime.Ref('Type'),
474
+ index_1.Runtime.Const(RBrace)
348
475
  ], MappedMapping);
349
476
  // ------------------------------------------------------------------
350
477
  // AsyncIterator
@@ -565,10 +692,34 @@ const Date = index_1.Runtime.Const('Date', index_1.Runtime.As(Types.Date()));
565
692
  // ------------------------------------------------------------------
566
693
  const Uint8Array = index_1.Runtime.Const('Uint8Array', index_1.Runtime.As(Types.Uint8Array()));
567
694
  // ------------------------------------------------------------------
695
+ // Main
696
+ // ------------------------------------------------------------------
697
+ // prettier-ignore
698
+ const Main = index_1.Runtime.Union([
699
+ ModuleDeclaration,
700
+ TypeAliasDeclaration,
701
+ InterfaceDeclaration,
702
+ Type
703
+ ]);
704
+ // ------------------------------------------------------------------
568
705
  // Module
569
706
  // ------------------------------------------------------------------
570
707
  // prettier-ignore
571
708
  exports.Module = new index_1.Runtime.Module({
709
+ // ----------------------------------------------------------------
710
+ // Modules, Interfaces and Type Aliases
711
+ // ----------------------------------------------------------------
712
+ ExportModifier,
713
+ HeritageList,
714
+ Heritage,
715
+ InterfaceDeclaration,
716
+ TypeAliasDeclaration,
717
+ ModuleType,
718
+ ModuleProperties,
719
+ ModuleDeclaration,
720
+ // ----------------------------------------------------------------
721
+ // Type Expressions
722
+ // ----------------------------------------------------------------
572
723
  Literal,
573
724
  Keyword,
574
725
  KeyOf,
@@ -580,10 +731,10 @@ exports.Module = new index_1.Runtime.Module({
580
731
  ExprTerm,
581
732
  ExprTail,
582
733
  Expr,
583
- Type,
734
+ Type, // Alias for Expr
584
735
  PropertyKey,
585
- PropertyReadonly,
586
- PropertyOptional,
736
+ Readonly,
737
+ Optional,
587
738
  Property,
588
739
  PropertyDelimiter,
589
740
  Properties,
@@ -617,5 +768,9 @@ exports.Module = new index_1.Runtime.Module({
617
768
  Uncapitalize,
618
769
  Date,
619
770
  Uint8Array,
620
- Reference
771
+ Reference,
772
+ // ----------------------------------------------------------------
773
+ // Main
774
+ // ----------------------------------------------------------------
775
+ Main
621
776
  });