@sinclair/typebox 0.33.21 → 0.34.0

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 (142) 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 -3
  5. package/build/cjs/index.js +1 -6
  6. package/build/cjs/syntax/parse.d.ts +15 -0
  7. package/build/cjs/{parse → syntax}/parse.js +6 -5
  8. package/build/cjs/{parse → syntax}/runtime.d.ts +17 -4
  9. package/build/cjs/{parse → syntax}/runtime.js +180 -27
  10. package/build/cjs/{parse → syntax}/static.d.ts +104 -20
  11. package/build/cjs/type/guard/kind.d.ts +3 -0
  12. package/build/cjs/type/guard/kind.js +5 -0
  13. package/build/cjs/type/guard/type.d.ts +3 -0
  14. package/build/cjs/type/guard/type.js +13 -0
  15. package/build/cjs/type/index.d.ts +1 -2
  16. package/build/cjs/type/index.js +1 -2
  17. package/build/cjs/type/module/index.d.ts +1 -0
  18. package/build/cjs/type/{deref → module}/index.js +1 -1
  19. package/build/cjs/type/module/module.d.ts +71 -0
  20. package/build/cjs/type/module/module.js +34 -0
  21. package/build/cjs/type/ref/ref.d.ts +4 -7
  22. package/build/cjs/type/ref/ref.js +3 -11
  23. package/build/cjs/type/static/static.d.ts +1 -1
  24. package/build/cjs/type/type/json.d.ts +4 -17
  25. package/build/cjs/type/type/json.js +52 -68
  26. package/build/cjs/type/type/type.d.ts +1 -2
  27. package/build/cjs/type/type/type.js +55 -57
  28. package/build/cjs/value/cast/cast.js +8 -1
  29. package/build/cjs/value/check/check.js +8 -1
  30. package/build/cjs/value/clean/clean.js +8 -1
  31. package/build/cjs/value/convert/convert.js +7 -0
  32. package/build/cjs/value/create/create.js +7 -0
  33. package/build/cjs/value/default/default.js +7 -0
  34. package/build/cjs/value/transform/decode.js +11 -0
  35. package/build/cjs/value/transform/encode.js +11 -0
  36. package/build/esm/compiler/compiler.d.mts +2 -2
  37. package/build/esm/compiler/compiler.mjs +13 -2
  38. package/build/esm/errors/errors.mjs +7 -0
  39. package/build/esm/index.d.mts +1 -3
  40. package/build/esm/index.mjs +1 -6
  41. package/build/esm/syntax/parse.d.mts +15 -0
  42. package/build/esm/syntax/parse.mjs +14 -0
  43. package/build/esm/{parse → syntax}/runtime.d.mts +17 -4
  44. package/build/esm/{parse → syntax}/runtime.mjs +180 -27
  45. package/build/esm/{parse → syntax}/static.d.mts +104 -20
  46. package/build/esm/type/guard/kind.d.mts +3 -0
  47. package/build/esm/type/guard/kind.mjs +4 -0
  48. package/build/esm/type/guard/type.d.mts +3 -0
  49. package/build/esm/type/guard/type.mjs +12 -0
  50. package/build/esm/type/index.d.mts +1 -2
  51. package/build/esm/type/index.mjs +1 -2
  52. package/build/esm/type/module/index.d.mts +1 -0
  53. package/build/esm/type/module/index.mjs +1 -0
  54. package/build/esm/type/module/module.d.mts +71 -0
  55. package/build/esm/type/module/module.mjs +28 -0
  56. package/build/esm/type/ref/ref.d.mts +4 -7
  57. package/build/esm/type/ref/ref.mjs +3 -11
  58. package/build/esm/type/static/static.d.mts +1 -1
  59. package/build/esm/type/type/json.d.mts +4 -17
  60. package/build/esm/type/type/json.mjs +7 -23
  61. package/build/esm/type/type/type.d.mts +1 -2
  62. package/build/esm/type/type/type.mjs +1 -2
  63. package/build/esm/value/cast/cast.mjs +9 -2
  64. package/build/esm/value/check/check.mjs +9 -2
  65. package/build/esm/value/clean/clean.mjs +9 -2
  66. package/build/esm/value/convert/convert.mjs +7 -0
  67. package/build/esm/value/create/create.mjs +7 -0
  68. package/build/esm/value/default/default.mjs +7 -0
  69. package/build/esm/value/transform/decode.mjs +11 -0
  70. package/build/esm/value/transform/encode.mjs +11 -0
  71. package/package.json +6 -6
  72. package/readme.md +219 -97
  73. package/syntax/package.json +4 -0
  74. package/build/cjs/parse/parse.d.ts +0 -11
  75. package/build/cjs/type/deref/deref.d.ts +0 -22
  76. package/build/cjs/type/deref/deref.js +0 -106
  77. package/build/cjs/type/deref/index.d.ts +0 -1
  78. package/build/cjs/type/strict/index.d.ts +0 -1
  79. package/build/cjs/type/strict/index.js +0 -18
  80. package/build/cjs/type/strict/strict.d.ts +0 -12
  81. package/build/cjs/type/strict/strict.js +0 -16
  82. package/build/esm/parse/parse.d.mts +0 -11
  83. package/build/esm/parse/parse.mjs +0 -13
  84. package/build/esm/type/deref/deref.d.mts +0 -22
  85. package/build/esm/type/deref/deref.mjs +0 -102
  86. package/build/esm/type/deref/index.d.mts +0 -1
  87. package/build/esm/type/deref/index.mjs +0 -1
  88. package/build/esm/type/strict/index.d.mts +0 -1
  89. package/build/esm/type/strict/index.mjs +0 -1
  90. package/build/esm/type/strict/strict.d.mts +0 -12
  91. package/build/esm/type/strict/strict.mjs +0 -12
  92. package/parse/package.json +0 -4
  93. /package/build/cjs/{parse → syntax}/index.d.ts +0 -0
  94. /package/build/cjs/{parse → syntax}/index.js +0 -0
  95. /package/build/cjs/{parse → syntax}/parsebox/index.d.ts +0 -0
  96. /package/build/cjs/{parse → syntax}/parsebox/index.js +0 -0
  97. /package/build/cjs/{parse → syntax}/parsebox/runtime/guard.d.ts +0 -0
  98. /package/build/cjs/{parse → syntax}/parsebox/runtime/guard.js +0 -0
  99. /package/build/cjs/{parse → syntax}/parsebox/runtime/index.d.ts +0 -0
  100. /package/build/cjs/{parse → syntax}/parsebox/runtime/index.js +0 -0
  101. /package/build/cjs/{parse → syntax}/parsebox/runtime/module.d.ts +0 -0
  102. /package/build/cjs/{parse → syntax}/parsebox/runtime/module.js +0 -0
  103. /package/build/cjs/{parse → syntax}/parsebox/runtime/parse.d.ts +0 -0
  104. /package/build/cjs/{parse → syntax}/parsebox/runtime/parse.js +0 -0
  105. /package/build/cjs/{parse → syntax}/parsebox/runtime/token.d.ts +0 -0
  106. /package/build/cjs/{parse → syntax}/parsebox/runtime/token.js +0 -0
  107. /package/build/cjs/{parse → syntax}/parsebox/runtime/types.d.ts +0 -0
  108. /package/build/cjs/{parse → syntax}/parsebox/runtime/types.js +0 -0
  109. /package/build/cjs/{parse → syntax}/parsebox/static/index.d.ts +0 -0
  110. /package/build/cjs/{parse → syntax}/parsebox/static/index.js +0 -0
  111. /package/build/cjs/{parse → syntax}/parsebox/static/parse.d.ts +0 -0
  112. /package/build/cjs/{parse → syntax}/parsebox/static/parse.js +0 -0
  113. /package/build/cjs/{parse → syntax}/parsebox/static/token.d.ts +0 -0
  114. /package/build/cjs/{parse → syntax}/parsebox/static/token.js +0 -0
  115. /package/build/cjs/{parse → syntax}/parsebox/static/types.d.ts +0 -0
  116. /package/build/cjs/{parse → syntax}/parsebox/static/types.js +0 -0
  117. /package/build/cjs/{parse → syntax}/static.js +0 -0
  118. /package/build/esm/{parse → syntax}/index.d.mts +0 -0
  119. /package/build/esm/{parse → syntax}/index.mjs +0 -0
  120. /package/build/esm/{parse → syntax}/parsebox/index.d.mts +0 -0
  121. /package/build/esm/{parse → syntax}/parsebox/index.mjs +0 -0
  122. /package/build/esm/{parse → syntax}/parsebox/runtime/guard.d.mts +0 -0
  123. /package/build/esm/{parse → syntax}/parsebox/runtime/guard.mjs +0 -0
  124. /package/build/esm/{parse → syntax}/parsebox/runtime/index.d.mts +0 -0
  125. /package/build/esm/{parse → syntax}/parsebox/runtime/index.mjs +0 -0
  126. /package/build/esm/{parse → syntax}/parsebox/runtime/module.d.mts +0 -0
  127. /package/build/esm/{parse → syntax}/parsebox/runtime/module.mjs +0 -0
  128. /package/build/esm/{parse → syntax}/parsebox/runtime/parse.d.mts +0 -0
  129. /package/build/esm/{parse → syntax}/parsebox/runtime/parse.mjs +0 -0
  130. /package/build/esm/{parse → syntax}/parsebox/runtime/token.d.mts +0 -0
  131. /package/build/esm/{parse → syntax}/parsebox/runtime/token.mjs +0 -0
  132. /package/build/esm/{parse → syntax}/parsebox/runtime/types.d.mts +0 -0
  133. /package/build/esm/{parse → syntax}/parsebox/runtime/types.mjs +0 -0
  134. /package/build/esm/{parse → syntax}/parsebox/static/index.d.mts +0 -0
  135. /package/build/esm/{parse → syntax}/parsebox/static/index.mjs +0 -0
  136. /package/build/esm/{parse → syntax}/parsebox/static/parse.d.mts +0 -0
  137. /package/build/esm/{parse → syntax}/parsebox/static/parse.mjs +0 -0
  138. /package/build/esm/{parse → syntax}/parsebox/static/token.d.mts +0 -0
  139. /package/build/esm/{parse → syntax}/parsebox/static/token.mjs +0 -0
  140. /package/build/esm/{parse → syntax}/parsebox/static/types.d.mts +0 -0
  141. /package/build/esm/{parse → syntax}/parsebox/static/types.mjs +0 -0
  142. /package/build/esm/{parse → syntax}/static.mjs +0 -0
@@ -255,6 +255,11 @@ export var TypeCompiler;
255
255
  function* FromFunction(schema, references, value) {
256
256
  yield `(typeof ${value} === 'function')`;
257
257
  }
258
+ function* FromImport(schema, references, value) {
259
+ const definitions = globalThis.Object.values(schema.$defs);
260
+ const target = schema.$defs[schema.$ref];
261
+ yield* Visit(target, [...references, ...definitions], value);
262
+ }
258
263
  function* FromInteger(schema, references, value) {
259
264
  yield `Number.isInteger(${value})`;
260
265
  if (IsNumber(schema.exclusiveMaximum))
@@ -372,9 +377,13 @@ export var TypeCompiler;
372
377
  function* FromRef(schema, references, value) {
373
378
  const target = Deref(schema, references);
374
379
  // Reference: If we have seen this reference before we can just yield and return the function call.
375
- // If this isn't the case we defer to visit to generate and set the function for subsequent passes.
376
- if (state.functions.has(schema.$ref))
380
+ // If this isn't the case we defer to visit to generate and set the _recursion_end_for_ for subsequent
381
+ // passes. This operation is very awkward as we are using the functions state to store values to
382
+ // enable self referential types to terminate. This needs to be refactored.
383
+ const recursiveEnd = `_recursion_end_for_${schema.$ref}`;
384
+ if (state.functions.has(recursiveEnd))
377
385
  return yield `${CreateFunctionName(schema.$ref)}(${value})`;
386
+ state.functions.set(recursiveEnd, ''); // terminate recursion here by setting the name.
378
387
  yield* Visit(target, references, value);
379
388
  }
380
389
  function* FromRegExp(schema, references, value) {
@@ -481,6 +490,8 @@ export var TypeCompiler;
481
490
  return yield* FromDate(schema_, references_, value);
482
491
  case 'Function':
483
492
  return yield* FromFunction(schema_, references_, value);
493
+ case 'Import':
494
+ return yield* FromImport(schema_, references_, value);
484
495
  case 'Integer':
485
496
  return yield* FromInteger(schema_, references_, value);
486
497
  case 'Intersect':
@@ -232,6 +232,11 @@ function* FromFunction(schema, references, path, value) {
232
232
  if (!IsFunction(value))
233
233
  yield Create(ValueErrorType.Function, schema, path, value);
234
234
  }
235
+ function* FromImport(schema, references, path, value) {
236
+ const definitions = globalThis.Object.values(schema.$defs);
237
+ const target = schema.$defs[schema.$ref];
238
+ yield* Visit(target, [...references, ...definitions], path, value);
239
+ }
235
240
  function* FromInteger(schema, references, path, value) {
236
241
  if (!IsInteger(value))
237
242
  return yield Create(ValueErrorType.Integer, schema, path, value);
@@ -523,6 +528,8 @@ function* Visit(schema, references, path, value) {
523
528
  return yield* FromDate(schema_, references_, path, value);
524
529
  case 'Function':
525
530
  return yield* FromFunction(schema_, references_, path, value);
531
+ case 'Import':
532
+ return yield* FromImport(schema_, references_, path, value);
526
533
  case 'Integer':
527
534
  return yield* FromInteger(schema_, references_, path, value);
528
535
  case 'Intersect':
@@ -7,7 +7,6 @@ export * from './type/patterns/index.mjs';
7
7
  export * from './type/registry/index.mjs';
8
8
  export * from './type/sets/index.mjs';
9
9
  export * from './type/symbols/index.mjs';
10
- export * from './parse/index.mjs';
11
10
  export * from './type/any/index.mjs';
12
11
  export * from './type/array/index.mjs';
13
12
  export * from './type/async-iterator/index.mjs';
@@ -19,7 +18,6 @@ export * from './type/const/index.mjs';
19
18
  export * from './type/constructor/index.mjs';
20
19
  export * from './type/constructor-parameters/index.mjs';
21
20
  export * from './type/date/index.mjs';
22
- export * from './type/deref/index.mjs';
23
21
  export * from './type/enum/index.mjs';
24
22
  export * from './type/exclude/index.mjs';
25
23
  export * from './type/extends/index.mjs';
@@ -33,6 +31,7 @@ export * from './type/iterator/index.mjs';
33
31
  export * from './type/intrinsic/index.mjs';
34
32
  export * from './type/keyof/index.mjs';
35
33
  export * from './type/literal/index.mjs';
34
+ export * from './type/module/index.mjs';
36
35
  export * from './type/mapped/index.mjs';
37
36
  export * from './type/never/index.mjs';
38
37
  export * from './type/not/index.mjs';
@@ -56,7 +55,6 @@ export * from './type/rest/index.mjs';
56
55
  export * from './type/return-type/index.mjs';
57
56
  export * from './type/schema/index.mjs';
58
57
  export * from './type/static/index.mjs';
59
- export * from './type/strict/index.mjs';
60
58
  export * from './type/string/index.mjs';
61
59
  export * from './type/symbol/index.mjs';
62
60
  export * from './type/template-literal/index.mjs';
@@ -11,10 +11,6 @@ export * from './type/registry/index.mjs';
11
11
  export * from './type/sets/index.mjs';
12
12
  export * from './type/symbols/index.mjs';
13
13
  // ------------------------------------------------------------------
14
- // Parse
15
- // ------------------------------------------------------------------
16
- export * from './parse/index.mjs';
17
- // ------------------------------------------------------------------
18
14
  // Types
19
15
  // ------------------------------------------------------------------
20
16
  export * from './type/any/index.mjs';
@@ -28,7 +24,6 @@ export * from './type/const/index.mjs';
28
24
  export * from './type/constructor/index.mjs';
29
25
  export * from './type/constructor-parameters/index.mjs';
30
26
  export * from './type/date/index.mjs';
31
- export * from './type/deref/index.mjs';
32
27
  export * from './type/enum/index.mjs';
33
28
  export * from './type/exclude/index.mjs';
34
29
  export * from './type/extends/index.mjs';
@@ -42,6 +37,7 @@ export * from './type/iterator/index.mjs';
42
37
  export * from './type/intrinsic/index.mjs';
43
38
  export * from './type/keyof/index.mjs';
44
39
  export * from './type/literal/index.mjs';
40
+ export * from './type/module/index.mjs';
45
41
  export * from './type/mapped/index.mjs';
46
42
  export * from './type/never/index.mjs';
47
43
  export * from './type/not/index.mjs';
@@ -65,7 +61,6 @@ export * from './type/rest/index.mjs';
65
61
  export * from './type/return-type/index.mjs';
66
62
  export * from './type/schema/index.mjs';
67
63
  export * from './type/static/index.mjs';
68
- export * from './type/strict/index.mjs';
69
64
  export * from './type/string/index.mjs';
70
65
  export * from './type/symbol/index.mjs';
71
66
  export * from './type/template-literal/index.mjs';
@@ -0,0 +1,15 @@
1
+ import * as Types from '../type/index.mjs';
2
+ import { Static } from './parsebox/index.mjs';
3
+ import { Main } from './static.mjs';
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;
@@ -0,0 +1,14 @@
1
+ import * as Types from '../type/index.mjs';
2
+ import { Module } from './runtime.mjs';
3
+ /** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
4
+ export function Parse(...args) {
5
+ return ParseOnly.apply(null, args);
6
+ }
7
+ /** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
8
+ export function ParseOnly(...args) {
9
+ const withContext = typeof args[0] === 'string' ? false : true;
10
+ const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
11
+ const type = Module.Parse('Main', code, context)[0];
12
+ // Note: Parsing may return either a ModuleInstance or Type. We only apply options on the Type.
13
+ return Types.KindGuard.IsSchema(type) ? Types.CloneType(type, options) : type;
14
+ }
@@ -1,6 +1,18 @@
1
1
  import { Runtime } from './parsebox/index.mjs';
2
2
  import * as Types from '../type/index.mjs';
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>>;
4
16
  Literal: Runtime.IUnion<Types.TLiteral<string> | Types.TLiteral<number> | Types.TLiteral<boolean>>;
5
17
  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
18
  KeyOf: Runtime.IUnion<boolean>;
@@ -14,14 +26,14 @@ export declare const Module: Runtime.Module<{
14
26
  Expr: Runtime.ITuple<Types.TSchema>;
15
27
  Type: Runtime.IRef<unknown>;
16
28
  PropertyKey: Runtime.IUnion<string>;
17
- PropertyReadonly: Runtime.IUnion<boolean>;
18
- PropertyOptional: Runtime.IUnion<boolean>;
29
+ Readonly: Runtime.IUnion<boolean>;
30
+ Optional: Runtime.IUnion<boolean>;
19
31
  Property: Runtime.ITuple<{
20
32
  [x: string]: Types.TSchema;
21
33
  }>;
22
34
  PropertyDelimiter: Runtime.IUnion<[","] | [",", "\n"] | [";"] | [";", "\n"] | ["\n"]>;
23
- Properties: Runtime.IUnion<unknown[]>;
24
- Object: Runtime.ITuple<Types.TObject<Record<string, Types.TSchema>>>;
35
+ Properties: Runtime.IUnion<Types.TProperties>;
36
+ Object: Runtime.ITuple<Types.TObject<Types.TProperties>>;
25
37
  Elements: Runtime.IUnion<unknown[]>;
26
38
  Tuple: Runtime.ITuple<Types.TTuple<Types.TSchema[]>>;
27
39
  Parameter: Runtime.ITuple<Types.TSchema>;
@@ -52,4 +64,5 @@ export declare const Module: Runtime.Module<{
52
64
  Date: Runtime.IConst<Types.TDate>;
53
65
  Uint8Array: Runtime.IConst<Types.TUint8Array>;
54
66
  Reference: Runtime.IIdent<Types.TSchema>;
67
+ Main: Runtime.IUnion<unknown>;
55
68
  }>;
@@ -19,6 +19,7 @@ const SemiColon = ';';
19
19
  const SingleQuote = "'";
20
20
  const DoubleQuote = '"';
21
21
  const Tilde = '`';
22
+ const Equals = '=';
22
23
  // ------------------------------------------------------------------
23
24
  // DestructureRight
24
25
  // ------------------------------------------------------------------
@@ -29,12 +30,133 @@ function DestructureRight(values) {
29
30
  : [values, undefined];
30
31
  }
31
32
  // ------------------------------------------------------------------
33
+ // Deref
34
+ // ------------------------------------------------------------------
35
+ const Deref = (context, key) => {
36
+ return key in context ? context[key] : Types.Ref(key);
37
+ };
38
+ // ------------------------------------------------------------------
39
+ // ExportModifier
40
+ // ------------------------------------------------------------------
41
+ // prettier-ignore
42
+ const ExportModifierMapping = (values) => {
43
+ return values.length === 1;
44
+ };
45
+ // prettier-ignore
46
+ const ExportModifier = Runtime.Union([
47
+ Runtime.Tuple([Runtime.Const('export')]), Runtime.Tuple([])
48
+ ], ExportModifierMapping);
49
+ // ------------------------------------------------------------------
50
+ // TypeAliasDeclaration
51
+ // ------------------------------------------------------------------
52
+ // prettier-ignore
53
+ const TypeAliasDeclarationMapping = (_Export, _Keyword, Ident, _Equals, Type) => {
54
+ return { [Ident]: Type };
55
+ };
56
+ // prettier-ignore
57
+ const TypeAliasDeclaration = Runtime.Tuple([
58
+ Runtime.Ref('ExportModifier'),
59
+ Runtime.Const('type'),
60
+ Runtime.Ident(),
61
+ Runtime.Const(Equals),
62
+ Runtime.Ref('Type')
63
+ ], value => TypeAliasDeclarationMapping(...value));
64
+ // ------------------------------------------------------------------
65
+ // HeritageList
66
+ // ------------------------------------------------------------------
67
+ // prettier-ignore (note, heritage list should disallow trailing comma)
68
+ const HeritageListDelimiter = Runtime.Union([Runtime.Tuple([Runtime.Const(Comma), Runtime.Const(Newline)]), Runtime.Tuple([Runtime.Const(Comma)])]);
69
+ // prettier-ignore
70
+ const HeritageListMapping = (values, context) => {
71
+ return (values.length === 3 ? [Deref(context, values[0]), ...values[2]] :
72
+ values.length === 1 ? [Deref(context, values[0])] :
73
+ []);
74
+ };
75
+ // prettier-ignore
76
+ const HeritageList = Runtime.Union([
77
+ Runtime.Tuple([Runtime.Ident(), HeritageListDelimiter, Runtime.Ref('HeritageList')]),
78
+ Runtime.Tuple([Runtime.Ident()]),
79
+ Runtime.Tuple([])
80
+ ], HeritageListMapping);
81
+ // ------------------------------------------------------------------
82
+ // Heritage
83
+ // ------------------------------------------------------------------
84
+ // prettier-ignore
85
+ const HeritageMapping = (values) => {
86
+ return (values.length === 2 ? values[1] : []);
87
+ };
88
+ // prettier-ignore
89
+ const Heritage = Runtime.Union([
90
+ Runtime.Tuple([Runtime.Const('extends'), Runtime.Ref('HeritageList')]),
91
+ Runtime.Tuple([])
92
+ ], HeritageMapping);
93
+ // ------------------------------------------------------------------
94
+ // InterfaceDeclaration
95
+ // ------------------------------------------------------------------
96
+ // prettier-ignore
97
+ const InterfaceDeclarationMapping = (_0, _1, Ident, Heritage, _4, Properties, _6) => {
98
+ return { [Ident]: Types.Intersect([...Heritage, Types.Object(Properties)]) };
99
+ };
100
+ // prettier-ignore
101
+ const InterfaceDeclaration = Runtime.Tuple([
102
+ Runtime.Ref('ExportModifier'),
103
+ Runtime.Const('interface'),
104
+ Runtime.Ident(),
105
+ Runtime.Ref('Heritage'),
106
+ Runtime.Const(LBrace),
107
+ Runtime.Ref('Properties'),
108
+ Runtime.Const(RBrace),
109
+ ], values => InterfaceDeclarationMapping(...values));
110
+ // ------------------------------------------------------------------
111
+ // ModuleType
112
+ // ------------------------------------------------------------------
113
+ // prettier-ignore
114
+ const ModuleType = Runtime.Union([
115
+ Runtime.Ref('InterfaceDeclaration'),
116
+ Runtime.Ref('TypeAliasDeclaration')
117
+ ]);
118
+ // ------------------------------------------------------------------
119
+ // ModuleProperties
120
+ // ------------------------------------------------------------------
121
+ // prettier-ignore
122
+ const ModulePropertiesDelimiter = Runtime.Union([
123
+ Runtime.Tuple([Runtime.Const(SemiColon), Runtime.Const(Newline)]),
124
+ Runtime.Tuple([Runtime.Const(SemiColon)]),
125
+ Runtime.Tuple([Runtime.Const(Newline)]),
126
+ ]);
127
+ // prettier-ignore
128
+ const ModulePropertiesMapping = (values) => {
129
+ return (values.length === 3 ? { ...values[0], ...values[2] } :
130
+ values.length === 1 ? values[0] :
131
+ {});
132
+ };
133
+ // prettier-ignore
134
+ const ModuleProperties = Runtime.Union([
135
+ Runtime.Tuple([Runtime.Ref('ModuleType'), ModulePropertiesDelimiter, Runtime.Ref('ModuleProperties')]),
136
+ Runtime.Tuple([Runtime.Ref('ModuleType')]),
137
+ Runtime.Tuple([]),
138
+ ], ModulePropertiesMapping);
139
+ // ------------------------------------------------------------------
140
+ // ModuleDeclaration
141
+ // ------------------------------------------------------------------
142
+ // prettier-ignore
143
+ const ModuleIdentifier = Runtime.Union([
144
+ Runtime.Tuple([Runtime.Ident()]),
145
+ Runtime.Tuple([])
146
+ ]);
147
+ // prettier-ignore
148
+ const ModuleDeclarationMapping = (_1, _2, _Ident, _3, Properties, _5) => {
149
+ return Types.Module(Properties);
150
+ };
151
+ // prettier-ignore
152
+ const ModuleDeclaration = Runtime.Tuple([
153
+ Runtime.Ref('ExportModifier'), Runtime.Const('module'), ModuleIdentifier, Runtime.Const(LBrace), Runtime.Ref('ModuleProperties'), Runtime.Const(RBrace)
154
+ ], values => ModuleDeclarationMapping(...values));
155
+ // ------------------------------------------------------------------
32
156
  // Reference
33
157
  // ------------------------------------------------------------------
34
158
  // prettier-ignore
35
- const Reference = Runtime.Ident((value, context) => {
36
- return value in context ? context[value] : Types.Ref(value);
37
- });
159
+ const Reference = Runtime.Ident((value, context) => Deref(context, value));
38
160
  // ------------------------------------------------------------------
39
161
  // Literal
40
162
  // ------------------------------------------------------------------
@@ -220,24 +342,21 @@ const Type = Runtime.Ref('Expr');
220
342
  // ------------------------------------------------------------------
221
343
  // Properties
222
344
  // ------------------------------------------------------------------
223
- // prettier-ignore
224
345
  const PropertyKey = Runtime.Union([Runtime.Ident(), Runtime.String([SingleQuote, DoubleQuote])]);
346
+ const Readonly = Runtime.Union([Runtime.Tuple([Runtime.Const('readonly')]), Runtime.Tuple([])], (value) => value.length > 0);
347
+ const Optional = Runtime.Union([Runtime.Tuple([Runtime.Const(Question)]), Runtime.Tuple([])], (value) => value.length > 0);
225
348
  // prettier-ignore
226
- const PropertyReadonly = Runtime.Union([Runtime.Tuple([Runtime.Const('readonly')]), Runtime.Tuple([])], value => value.length > 0);
227
- // prettier-ignore
228
- const PropertyOptional = Runtime.Union([Runtime.Tuple([Runtime.Const(Question)]), Runtime.Tuple([])], value => value.length > 0);
229
- // prettier-ignore
230
- const PropertyMapping = (Readonly, Key, Optional, _, Type) => ({
231
- [Key]: (Readonly && Optional ? Types.ReadonlyOptional(Type) :
232
- Readonly && !Optional ? Types.Readonly(Type) :
233
- !Readonly && Optional ? Types.Optional(Type) :
349
+ const PropertyMapping = (IsReadonly, Key, IsOptional, _, Type) => ({
350
+ [Key]: (IsReadonly && IsOptional ? Types.ReadonlyOptional(Type) :
351
+ IsReadonly && !IsOptional ? Types.Readonly(Type) :
352
+ !IsReadonly && IsOptional ? Types.Optional(Type) :
234
353
  Type)
235
354
  });
236
355
  // prettier-ignore
237
356
  const Property = Runtime.Tuple([
238
- Runtime.Ref('PropertyReadonly'),
357
+ Runtime.Ref('Readonly'),
239
358
  Runtime.Ref('PropertyKey'),
240
- Runtime.Ref('PropertyOptional'),
359
+ Runtime.Ref('Optional'),
241
360
  Runtime.Const(Colon),
242
361
  Runtime.Ref('Type'),
243
362
  ], value => PropertyMapping(...value));
@@ -255,23 +374,21 @@ const Properties = Runtime.Union([
255
374
  Runtime.Tuple([Runtime.Ref('Property'), Runtime.Ref('PropertyDelimiter')]),
256
375
  Runtime.Tuple([Runtime.Ref('Property')]),
257
376
  Runtime.Tuple([])
258
- ], values => (values.length === 3 ? [values[0], ...values[2]] :
259
- values.length === 2 ? [values[0]] :
260
- values.length === 1 ? [values[0]] :
261
- []));
377
+ ], values => (values.length === 3 ? { ...values[0], ...values[2] } :
378
+ values.length === 2 ? values[0] :
379
+ values.length === 1 ? values[0] :
380
+ {}));
262
381
  // ------------------------------------------------------------------
263
382
  // Object
264
383
  // ------------------------------------------------------------------
265
384
  // prettier-ignore
266
- const ObjectMapping = (values) => Types.Object(values.reduce((properties, record) => {
267
- return { ...properties, ...record };
268
- }, {}));
385
+ const ObjectMapping = (_0, Properties, _2) => Types.Object(Properties);
269
386
  // prettier-ignore
270
387
  const _Object = Runtime.Tuple([
271
388
  Runtime.Const(LBrace),
272
389
  Runtime.Ref('Properties'),
273
390
  Runtime.Const(RBrace)
274
- ], values => ObjectMapping(values[1]));
391
+ ], values => ObjectMapping(...values));
275
392
  // ------------------------------------------------------------------
276
393
  // Tuple
277
394
  // ------------------------------------------------------------------
@@ -340,7 +457,15 @@ const MappedMapping = (values) => {
340
457
  };
341
458
  // prettier-ignore
342
459
  const Mapped = Runtime.Tuple([
343
- Runtime.Const(LBrace), Runtime.Const(LBracket), Runtime.Ident(), Runtime.Const('in'), Runtime.Ref('Type'), Runtime.Const(RBracket), Runtime.Const(Colon), Runtime.Ref('Type'), Runtime.Const(RBrace)
460
+ Runtime.Const(LBrace),
461
+ Runtime.Const(LBracket),
462
+ Runtime.Ident(),
463
+ Runtime.Const('in'),
464
+ Runtime.Ref('Type'),
465
+ Runtime.Const(RBracket),
466
+ Runtime.Const(Colon),
467
+ Runtime.Ref('Type'),
468
+ Runtime.Const(RBrace)
344
469
  ], MappedMapping);
345
470
  // ------------------------------------------------------------------
346
471
  // AsyncIterator
@@ -561,10 +686,34 @@ const Date = Runtime.Const('Date', Runtime.As(Types.Date()));
561
686
  // ------------------------------------------------------------------
562
687
  const Uint8Array = Runtime.Const('Uint8Array', Runtime.As(Types.Uint8Array()));
563
688
  // ------------------------------------------------------------------
689
+ // Main
690
+ // ------------------------------------------------------------------
691
+ // prettier-ignore
692
+ const Main = Runtime.Union([
693
+ ModuleDeclaration,
694
+ TypeAliasDeclaration,
695
+ InterfaceDeclaration,
696
+ Type
697
+ ]);
698
+ // ------------------------------------------------------------------
564
699
  // Module
565
700
  // ------------------------------------------------------------------
566
701
  // prettier-ignore
567
702
  export const Module = new Runtime.Module({
703
+ // ----------------------------------------------------------------
704
+ // Modules, Interfaces and Type Aliases
705
+ // ----------------------------------------------------------------
706
+ ExportModifier,
707
+ HeritageList,
708
+ Heritage,
709
+ InterfaceDeclaration,
710
+ TypeAliasDeclaration,
711
+ ModuleType,
712
+ ModuleProperties,
713
+ ModuleDeclaration,
714
+ // ----------------------------------------------------------------
715
+ // Type Expressions
716
+ // ----------------------------------------------------------------
568
717
  Literal,
569
718
  Keyword,
570
719
  KeyOf,
@@ -576,10 +725,10 @@ export const Module = new Runtime.Module({
576
725
  ExprTerm,
577
726
  ExprTail,
578
727
  Expr,
579
- Type,
728
+ Type, // Alias for Expr
580
729
  PropertyKey,
581
- PropertyReadonly,
582
- PropertyOptional,
730
+ Readonly,
731
+ Optional,
583
732
  Property,
584
733
  PropertyDelimiter,
585
734
  Properties,
@@ -613,5 +762,9 @@ export const Module = new Runtime.Module({
613
762
  Uncapitalize,
614
763
  Date,
615
764
  Uint8Array,
616
- Reference
765
+ Reference,
766
+ // ----------------------------------------------------------------
767
+ // Main
768
+ // ----------------------------------------------------------------
769
+ Main
617
770
  });