@sinclair/typebox 0.30.4 → 0.31.0-dev-2

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.
package/typebox.js CHANGED
@@ -27,10 +27,11 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.Intrinsic = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.ValueGuard = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Optional = exports.Readonly = void 0;
30
+ exports.Type = exports.JsonType = exports.JavaScriptTypeBuilder = exports.JsonTypeBuilder = exports.TypeBuilder = exports.TypeBuilderError = exports.TransformEncodeBuilder = exports.TransformDecodeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralGeneratorError = exports.TemplateLiteralFinite = exports.TemplateLiteralFiniteError = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.TemplateLiteralPatternError = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyArrayResolverError = exports.KeyResolver = exports.ObjectMap = exports.Intrinsic = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.TypeExtendsError = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.ValueGuard = exports.FormatRegistry = exports.TypeBoxError = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Optional = exports.Readonly = exports.Transform = void 0;
31
31
  // --------------------------------------------------------------------------
32
32
  // Symbols
33
33
  // --------------------------------------------------------------------------
34
+ exports.Transform = Symbol.for('TypeBox.Transform');
34
35
  exports.Readonly = Symbol.for('TypeBox.Readonly');
35
36
  exports.Optional = Symbol.for('TypeBox.Optional');
36
37
  exports.Hint = Symbol.for('TypeBox.Hint');
@@ -79,6 +80,15 @@ var TypeRegistry;
79
80
  }
80
81
  TypeRegistry.Get = Get;
81
82
  })(TypeRegistry || (exports.TypeRegistry = TypeRegistry = {}));
83
+ // --------------------------------------------------------------------------
84
+ // TypeBoxError
85
+ // --------------------------------------------------------------------------
86
+ class TypeBoxError extends Error {
87
+ constructor(message) {
88
+ super(message);
89
+ }
90
+ }
91
+ exports.TypeBoxError = TypeBoxError;
82
92
  /** A registry for user defined string formats */
83
93
  var FormatRegistry;
84
94
  (function (FormatRegistry) {
@@ -117,49 +127,54 @@ var FormatRegistry;
117
127
  // --------------------------------------------------------------------------
118
128
  // ValueGuard
119
129
  // --------------------------------------------------------------------------
130
+ /** Provides functions to type guard raw JavaScript values */
120
131
  var ValueGuard;
121
132
  (function (ValueGuard) {
122
- function IsObject(value) {
123
- return typeof value === 'object' && value !== null;
124
- }
125
- ValueGuard.IsObject = IsObject;
133
+ /** Returns true if this value is an array */
126
134
  function IsArray(value) {
127
135
  return Array.isArray(value);
128
136
  }
129
137
  ValueGuard.IsArray = IsArray;
138
+ /** Returns true if this value is bigint */
139
+ function IsBigInt(value) {
140
+ return typeof value === 'bigint';
141
+ }
142
+ ValueGuard.IsBigInt = IsBigInt;
143
+ /** Returns true if this value is a boolean */
130
144
  function IsBoolean(value) {
131
145
  return typeof value === 'boolean';
132
146
  }
133
147
  ValueGuard.IsBoolean = IsBoolean;
148
+ /** Returns true if this value is null */
134
149
  function IsNull(value) {
135
150
  return value === null;
136
151
  }
137
152
  ValueGuard.IsNull = IsNull;
138
- function IsUndefined(value) {
139
- return value === undefined;
140
- }
141
- ValueGuard.IsUndefined = IsUndefined;
142
- function IsBigInt(value) {
143
- return typeof value === 'bigint';
144
- }
145
- ValueGuard.IsBigInt = IsBigInt;
153
+ /** Returns true if this value is number */
146
154
  function IsNumber(value) {
147
155
  return typeof value === 'number';
148
156
  }
149
157
  ValueGuard.IsNumber = IsNumber;
158
+ /** Returns true if this value is an object */
159
+ function IsObject(value) {
160
+ return typeof value === 'object' && value !== null;
161
+ }
162
+ ValueGuard.IsObject = IsObject;
163
+ /** Returns true if this value is string */
150
164
  function IsString(value) {
151
165
  return typeof value === 'string';
152
166
  }
153
167
  ValueGuard.IsString = IsString;
168
+ /** Returns true if this value is undefined */
169
+ function IsUndefined(value) {
170
+ return value === undefined;
171
+ }
172
+ ValueGuard.IsUndefined = IsUndefined;
154
173
  })(ValueGuard || (exports.ValueGuard = ValueGuard = {}));
155
174
  // --------------------------------------------------------------------------
156
175
  // TypeGuard
157
176
  // --------------------------------------------------------------------------
158
- class TypeGuardUnknownTypeError extends Error {
159
- constructor(schema) {
160
- super('TypeGuard: Unknown type');
161
- this.schema = schema;
162
- }
177
+ class TypeGuardUnknownTypeError extends TypeBoxError {
163
178
  }
164
179
  exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
165
180
  /** Provides functions to test if JavaScript values are TypeBox types */
@@ -245,11 +260,11 @@ var TypeGuard;
245
260
  return (TKindOf(schema, 'BigInt') &&
246
261
  schema.type === 'bigint' &&
247
262
  IsOptionalString(schema.$id) &&
248
- IsOptionalBigInt(schema.multipleOf) &&
249
- IsOptionalBigInt(schema.minimum) &&
250
- IsOptionalBigInt(schema.maximum) &&
263
+ IsOptionalBigInt(schema.exclusiveMaximum) &&
251
264
  IsOptionalBigInt(schema.exclusiveMinimum) &&
252
- IsOptionalBigInt(schema.exclusiveMaximum));
265
+ IsOptionalBigInt(schema.maximum) &&
266
+ IsOptionalBigInt(schema.minimum) &&
267
+ IsOptionalBigInt(schema.multipleOf));
253
268
  }
254
269
  TypeGuard.TBigInt = TBigInt;
255
270
  /** Returns true if the given schema is TBoolean */
@@ -263,18 +278,12 @@ var TypeGuard;
263
278
  /** Returns true if the given schema is TConstructor */
264
279
  function TConstructor(schema) {
265
280
  // prettier-ignore
266
- if (!(TKindOf(schema, 'Constructor') &&
281
+ return (TKindOf(schema, 'Constructor') &&
267
282
  schema.type === 'constructor' &&
268
283
  IsOptionalString(schema.$id) &&
269
284
  ValueGuard.IsArray(schema.parameters) &&
270
- TSchema(schema.returns))) {
271
- return false;
272
- }
273
- for (const parameter of schema.parameters) {
274
- if (!TSchema(parameter))
275
- return false;
276
- }
277
- return true;
285
+ schema.parameters.every(schema => TSchema(schema)) &&
286
+ TSchema(schema.returns));
278
287
  }
279
288
  TypeGuard.TConstructor = TConstructor;
280
289
  /** Returns true if the given schema is TDate */
@@ -282,27 +291,22 @@ var TypeGuard;
282
291
  return (TKindOf(schema, 'Date') &&
283
292
  schema.type === 'Date' &&
284
293
  IsOptionalString(schema.$id) &&
285
- IsOptionalNumber(schema.minimumTimestamp) &&
286
- IsOptionalNumber(schema.maximumTimestamp) &&
294
+ IsOptionalNumber(schema.exclusiveMaximumTimestamp) &&
287
295
  IsOptionalNumber(schema.exclusiveMinimumTimestamp) &&
288
- IsOptionalNumber(schema.exclusiveMaximumTimestamp));
296
+ IsOptionalNumber(schema.maximumTimestamp) &&
297
+ IsOptionalNumber(schema.minimumTimestamp) &&
298
+ IsOptionalNumber(schema.multipleOfTimestamp));
289
299
  }
290
300
  TypeGuard.TDate = TDate;
291
301
  /** Returns true if the given schema is TFunction */
292
302
  function TFunction(schema) {
293
303
  // prettier-ignore
294
- if (!(TKindOf(schema, 'Function') &&
304
+ return (TKindOf(schema, 'Function') &&
295
305
  schema.type === 'function' &&
296
306
  IsOptionalString(schema.$id) &&
297
307
  ValueGuard.IsArray(schema.parameters) &&
298
- TSchema(schema.returns))) {
299
- return false;
300
- }
301
- for (const parameter of schema.parameters) {
302
- if (!TSchema(parameter))
303
- return false;
304
- }
305
- return true;
308
+ schema.parameters.every(schema => TSchema(schema)) &&
309
+ TSchema(schema.returns));
306
310
  }
307
311
  TypeGuard.TFunction = TFunction;
308
312
  /** Returns true if the given schema is TInteger */
@@ -310,31 +314,23 @@ var TypeGuard;
310
314
  return (TKindOf(schema, 'Integer') &&
311
315
  schema.type === 'integer' &&
312
316
  IsOptionalString(schema.$id) &&
313
- IsOptionalNumber(schema.multipleOf) &&
314
- IsOptionalNumber(schema.minimum) &&
315
- IsOptionalNumber(schema.maximum) &&
317
+ IsOptionalNumber(schema.exclusiveMaximum) &&
316
318
  IsOptionalNumber(schema.exclusiveMinimum) &&
317
- IsOptionalNumber(schema.exclusiveMaximum));
319
+ IsOptionalNumber(schema.maximum) &&
320
+ IsOptionalNumber(schema.minimum) &&
321
+ IsOptionalNumber(schema.multipleOf));
318
322
  }
319
323
  TypeGuard.TInteger = TInteger;
320
324
  /** Returns true if the given schema is TIntersect */
321
325
  function TIntersect(schema) {
322
326
  // prettier-ignore
323
- if (!(TKindOf(schema, 'Intersect') &&
327
+ return (TKindOf(schema, 'Intersect') &&
328
+ (ValueGuard.IsString(schema.type) && schema.type !== 'object' ? false : true) &&
324
329
  ValueGuard.IsArray(schema.allOf) &&
330
+ schema.allOf.every(schema => TSchema(schema) && !TTransform(schema)) &&
325
331
  IsOptionalString(schema.type) &&
326
332
  (IsOptionalBoolean(schema.unevaluatedProperties) || IsOptionalSchema(schema.unevaluatedProperties)) &&
327
- IsOptionalString(schema.$id))) {
328
- return false;
329
- }
330
- if ('type' in schema && schema.type !== 'object') {
331
- return false;
332
- }
333
- for (const inner of schema.allOf) {
334
- if (!TSchema(inner))
335
- return false;
336
- }
337
- return true;
333
+ IsOptionalString(schema.$id));
338
334
  }
339
335
  TypeGuard.TIntersect = TIntersect;
340
336
  /** Returns true if the given schema is TIterator */
@@ -408,31 +404,24 @@ var TypeGuard;
408
404
  return (TKindOf(schema, 'Number') &&
409
405
  schema.type === 'number' &&
410
406
  IsOptionalString(schema.$id) &&
411
- IsOptionalNumber(schema.multipleOf) &&
412
- IsOptionalNumber(schema.minimum) &&
413
- IsOptionalNumber(schema.maximum) &&
407
+ IsOptionalNumber(schema.exclusiveMaximum) &&
414
408
  IsOptionalNumber(schema.exclusiveMinimum) &&
415
- IsOptionalNumber(schema.exclusiveMaximum));
409
+ IsOptionalNumber(schema.maximum) &&
410
+ IsOptionalNumber(schema.minimum) &&
411
+ IsOptionalNumber(schema.multipleOf));
416
412
  }
417
413
  TypeGuard.TNumber = TNumber;
418
414
  /** Returns true if the given schema is TObject */
419
415
  function TObject(schema) {
420
- if (!(TKindOf(schema, 'Object') &&
416
+ // prettier-ignore
417
+ return (TKindOf(schema, 'Object') &&
421
418
  schema.type === 'object' &&
422
419
  IsOptionalString(schema.$id) &&
423
420
  ValueGuard.IsObject(schema.properties) &&
424
421
  IsAdditionalProperties(schema.additionalProperties) &&
425
422
  IsOptionalNumber(schema.minProperties) &&
426
- IsOptionalNumber(schema.maxProperties))) {
427
- return false;
428
- }
429
- for (const [key, value] of Object.entries(schema.properties)) {
430
- if (!IsControlCharacterFree(key))
431
- return false;
432
- if (!TSchema(value))
433
- return false;
434
- }
435
- return true;
423
+ IsOptionalNumber(schema.maxProperties) &&
424
+ Object.entries(schema.properties).every(([key, schema]) => IsControlCharacterFree(key) && TSchema(schema)));
436
425
  }
437
426
  TypeGuard.TObject = TObject;
438
427
  /** Returns true if the given schema is TPromise */
@@ -447,26 +436,25 @@ var TypeGuard;
447
436
  /** Returns true if the given schema is TRecord */
448
437
  function TRecord(schema) {
449
438
  // prettier-ignore
450
- if (!(TKindOf(schema, 'Record') &&
439
+ return (TKindOf(schema, 'Record') &&
451
440
  schema.type === 'object' &&
452
441
  IsOptionalString(schema.$id) &&
453
442
  IsAdditionalProperties(schema.additionalProperties) &&
454
- ValueGuard.IsObject(schema.patternProperties))) {
455
- return false;
456
- }
457
- const keys = Object.getOwnPropertyNames(schema.patternProperties);
458
- if (keys.length !== 1) {
459
- return false;
460
- }
461
- if (!IsPattern(keys[0])) {
462
- return false;
463
- }
464
- if (!TSchema(schema.patternProperties[keys[0]])) {
465
- return false;
466
- }
467
- return true;
443
+ ValueGuard.IsObject(schema.patternProperties) &&
444
+ ((schema) => {
445
+ const keys = Object.getOwnPropertyNames(schema.patternProperties);
446
+ return (keys.length === 1 &&
447
+ IsPattern(keys[0]) &&
448
+ ValueGuard.IsObject(schema.patternProperties) &&
449
+ TSchema(schema.patternProperties[keys[0]]));
450
+ })(schema));
468
451
  }
469
452
  TypeGuard.TRecord = TRecord;
453
+ /** Returns true if this schema is TRecursive */
454
+ function TRecursive(schema) {
455
+ return ValueGuard.IsObject(schema) && exports.Hint in schema && schema[exports.Hint] === 'Recursive';
456
+ }
457
+ TypeGuard.TRecursive = TRecursive;
470
458
  /** Returns true if the given schema is TRef */
471
459
  function TRef(schema) {
472
460
  // prettier-ignore
@@ -513,28 +501,25 @@ var TypeGuard;
513
501
  ValueGuard.IsString(schema.$ref));
514
502
  }
515
503
  TypeGuard.TThis = TThis;
504
+ /** Returns true of this schema is TTransform */
505
+ function TTransform(schema) {
506
+ return ValueGuard.IsObject(schema) && exports.Transform in schema;
507
+ }
508
+ TypeGuard.TTransform = TTransform;
516
509
  /** Returns true if the given schema is TTuple */
517
510
  function TTuple(schema) {
518
511
  // prettier-ignore
519
- if (!(TKindOf(schema, 'Tuple') &&
512
+ return (TKindOf(schema, 'Tuple') &&
520
513
  schema.type === 'array' &&
521
514
  IsOptionalString(schema.$id) &&
522
515
  ValueGuard.IsNumber(schema.minItems) &&
523
516
  ValueGuard.IsNumber(schema.maxItems) &&
524
- schema.minItems === schema.maxItems)) {
525
- return false;
526
- }
527
- if (ValueGuard.IsUndefined(schema.items) && ValueGuard.IsUndefined(schema.additionalItems) && schema.minItems === 0) {
528
- return true;
529
- }
530
- if (!ValueGuard.IsArray(schema.items)) {
531
- return false;
532
- }
533
- for (const inner of schema.items) {
534
- if (!TSchema(inner))
535
- return false;
536
- }
537
- return true;
517
+ schema.minItems === schema.maxItems &&
518
+ (( // empty
519
+ ValueGuard.IsUndefined(schema.items) &&
520
+ ValueGuard.IsUndefined(schema.additionalItems) &&
521
+ schema.minItems === 0) || (ValueGuard.IsArray(schema.items) &&
522
+ schema.items.every(schema => TSchema(schema)))));
538
523
  }
539
524
  TypeGuard.TTuple = TTuple;
540
525
  /** Returns true if the given schema is TUndefined */
@@ -553,16 +538,11 @@ var TypeGuard;
553
538
  /** Returns true if the given schema is TUnion */
554
539
  function TUnion(schema) {
555
540
  // prettier-ignore
556
- if (!(TKindOf(schema, 'Union') &&
541
+ return (TKindOf(schema, 'Union') &&
542
+ IsOptionalString(schema.$id) &&
543
+ ValueGuard.IsObject(schema) &&
557
544
  ValueGuard.IsArray(schema.anyOf) &&
558
- IsOptionalString(schema.$id))) {
559
- return false;
560
- }
561
- for (const inner of schema.anyOf) {
562
- if (!TSchema(inner))
563
- return false;
564
- }
565
- return true;
545
+ schema.anyOf.every(schema => TSchema(schema)));
566
546
  }
567
547
  TypeGuard.TUnion = TUnion;
568
548
  /** Returns true if the given schema is TUint8Array */
@@ -650,26 +630,24 @@ var TypeGuard;
650
630
  var ExtendsUndefined;
651
631
  (function (ExtendsUndefined) {
652
632
  function Check(schema) {
653
- if (schema[exports.Kind] === 'Undefined')
654
- return true;
655
- if (schema[exports.Kind] === 'Not') {
656
- return !Check(schema.not);
657
- }
658
- if (schema[exports.Kind] === 'Intersect') {
659
- const intersect = schema;
660
- return intersect.allOf.every((schema) => Check(schema));
661
- }
662
- if (schema[exports.Kind] === 'Union') {
663
- const union = schema;
664
- return union.anyOf.some((schema) => Check(schema));
665
- }
666
- return false;
633
+ return schema[exports.Kind] === 'Intersect'
634
+ ? schema.allOf.every((schema) => Check(schema))
635
+ : schema[exports.Kind] === 'Union'
636
+ ? schema.anyOf.some((schema) => Check(schema))
637
+ : schema[exports.Kind] === 'Undefined'
638
+ ? true
639
+ : schema[exports.Kind] === 'Not'
640
+ ? !Check(schema.not)
641
+ : false;
667
642
  }
668
643
  ExtendsUndefined.Check = Check;
669
644
  })(ExtendsUndefined || (exports.ExtendsUndefined = ExtendsUndefined = {}));
670
645
  // --------------------------------------------------------------------------
671
646
  // TypeExtends
672
647
  // --------------------------------------------------------------------------
648
+ class TypeExtendsError extends TypeBoxError {
649
+ }
650
+ exports.TypeExtendsError = TypeExtendsError;
673
651
  var TypeExtendsResult;
674
652
  (function (TypeExtendsResult) {
675
653
  TypeExtendsResult[TypeExtendsResult["Union"] = 0] = "Union";
@@ -682,7 +660,13 @@ var TypeExtends;
682
660
  // IntoBooleanResult
683
661
  // --------------------------------------------------------------------------
684
662
  function IntoBooleanResult(result) {
685
- return result === TypeExtendsResult.False ? TypeExtendsResult.False : TypeExtendsResult.True;
663
+ return result === TypeExtendsResult.False ? result : TypeExtendsResult.True;
664
+ }
665
+ // --------------------------------------------------------------------------
666
+ // Throw
667
+ // --------------------------------------------------------------------------
668
+ function Throw(message) {
669
+ throw new TypeExtendsError(message);
686
670
  }
687
671
  // --------------------------------------------------------------------------
688
672
  // StructuralRight
@@ -696,17 +680,13 @@ var TypeExtends;
696
680
  TypeGuard.TAny(right));
697
681
  }
698
682
  function StructuralRight(left, right) {
699
- if (TypeGuard.TNever(right))
700
- return TNeverRight(left, right);
701
- if (TypeGuard.TIntersect(right))
702
- return TIntersectRight(left, right);
703
- if (TypeGuard.TUnion(right))
704
- return TUnionRight(left, right);
705
- if (TypeGuard.TUnknown(right))
706
- return TUnknownRight(left, right);
707
- if (TypeGuard.TAny(right))
708
- return TAnyRight(left, right);
709
- throw Error('TypeExtends: StructuralRight');
683
+ // prettier-ignore
684
+ return (TypeGuard.TNever(right) ? TNeverRight(left, right) :
685
+ TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
686
+ TypeGuard.TUnion(right) ? TUnionRight(left, right) :
687
+ TypeGuard.TUnknown(right) ? TUnknownRight(left, right) :
688
+ TypeGuard.TAny(right) ? TAnyRight(left, right) :
689
+ Throw('StructuralRight'));
710
690
  }
711
691
  // --------------------------------------------------------------------------
712
692
  // Any
@@ -715,179 +695,155 @@ var TypeExtends;
715
695
  return TypeExtendsResult.True;
716
696
  }
717
697
  function TAny(left, right) {
718
- if (TypeGuard.TIntersect(right))
719
- return TIntersectRight(left, right);
720
- if (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema)))
721
- return TypeExtendsResult.True;
722
- if (TypeGuard.TUnion(right))
723
- return TypeExtendsResult.Union;
724
- if (TypeGuard.TUnknown(right))
725
- return TypeExtendsResult.True;
726
- if (TypeGuard.TAny(right))
727
- return TypeExtendsResult.True;
728
- return TypeExtendsResult.Union;
698
+ // prettier-ignore
699
+ return (TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
700
+ (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema))) ? TypeExtendsResult.True :
701
+ TypeGuard.TUnion(right) ? TypeExtendsResult.Union :
702
+ TypeGuard.TUnknown(right) ? TypeExtendsResult.True :
703
+ TypeGuard.TAny(right) ? TypeExtendsResult.True :
704
+ TypeExtendsResult.Union);
729
705
  }
730
706
  // --------------------------------------------------------------------------
731
707
  // Array
732
708
  // --------------------------------------------------------------------------
733
709
  function TArrayRight(left, right) {
734
- if (TypeGuard.TUnknown(left))
735
- return TypeExtendsResult.False;
736
- if (TypeGuard.TAny(left))
737
- return TypeExtendsResult.Union;
738
- if (TypeGuard.TNever(left))
739
- return TypeExtendsResult.True;
740
- return TypeExtendsResult.False;
710
+ // prettier-ignore
711
+ return (TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
712
+ TypeGuard.TAny(left) ? TypeExtendsResult.Union :
713
+ TypeGuard.TNever(left) ? TypeExtendsResult.True :
714
+ TypeExtendsResult.False);
741
715
  }
742
716
  function TArray(left, right) {
743
- if (IsStructuralRight(right))
744
- return StructuralRight(left, right);
745
- if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
746
- return TypeExtendsResult.True;
747
- if (!TypeGuard.TArray(right))
748
- return TypeExtendsResult.False;
749
- return IntoBooleanResult(Visit(left.items, right.items));
717
+ // prettier-ignore
718
+ return (TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True :
719
+ IsStructuralRight(right) ? StructuralRight(left, right) :
720
+ !TypeGuard.TArray(right) ? TypeExtendsResult.False :
721
+ IntoBooleanResult(Visit(left.items, right.items)));
750
722
  }
751
723
  // --------------------------------------------------------------------------
752
724
  // AsyncIterator
753
725
  // --------------------------------------------------------------------------
754
726
  function TAsyncIterator(left, right) {
755
- if (IsStructuralRight(right))
756
- return StructuralRight(left, right);
757
- if (!TypeGuard.TAsyncIterator(right))
758
- return TypeExtendsResult.False;
759
- return IntoBooleanResult(Visit(left.items, right.items));
727
+ // prettier-ignore
728
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
729
+ !TypeGuard.TAsyncIterator(right) ? TypeExtendsResult.False :
730
+ IntoBooleanResult(Visit(left.items, right.items)));
760
731
  }
761
732
  // --------------------------------------------------------------------------
762
733
  // BigInt
763
734
  // --------------------------------------------------------------------------
764
735
  function TBigInt(left, right) {
765
- if (IsStructuralRight(right))
766
- return StructuralRight(left, right);
767
- if (TypeGuard.TObject(right))
768
- return TObjectRight(left, right);
769
- if (TypeGuard.TRecord(right))
770
- return TRecordRight(left, right);
771
- return TypeGuard.TBigInt(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
736
+ // prettier-ignore
737
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
738
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
739
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
740
+ TypeGuard.TBigInt(right) ? TypeExtendsResult.True :
741
+ TypeExtendsResult.False);
772
742
  }
773
743
  // --------------------------------------------------------------------------
774
744
  // Boolean
775
745
  // --------------------------------------------------------------------------
776
746
  function TBooleanRight(left, right) {
777
- if (TypeGuard.TLiteral(left) && ValueGuard.IsBoolean(left.const))
778
- return TypeExtendsResult.True;
779
- return TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
747
+ return TypeGuard.TLiteral(left) && ValueGuard.IsBoolean(left.const) ? TypeExtendsResult.True : TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
780
748
  }
781
749
  function TBoolean(left, right) {
782
- if (IsStructuralRight(right))
783
- return StructuralRight(left, right);
784
- if (TypeGuard.TObject(right))
785
- return TObjectRight(left, right);
786
- if (TypeGuard.TRecord(right))
787
- return TRecordRight(left, right);
788
- return TypeGuard.TBoolean(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
750
+ // prettier-ignore
751
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
752
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
753
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
754
+ TypeGuard.TBoolean(right) ? TypeExtendsResult.True :
755
+ TypeExtendsResult.False);
789
756
  }
790
757
  // --------------------------------------------------------------------------
791
758
  // Constructor
792
759
  // --------------------------------------------------------------------------
793
760
  function TConstructor(left, right) {
794
- if (IsStructuralRight(right))
795
- return StructuralRight(left, right);
796
- if (TypeGuard.TObject(right))
797
- return TObjectRight(left, right);
798
- if (!TypeGuard.TConstructor(right))
799
- return TypeExtendsResult.False;
800
- if (left.parameters.length > right.parameters.length)
801
- return TypeExtendsResult.False;
802
- if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
803
- return TypeExtendsResult.False;
804
- }
805
- return IntoBooleanResult(Visit(left.returns, right.returns));
761
+ // prettier-ignore
762
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
763
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
764
+ !TypeGuard.TConstructor(right) ? TypeExtendsResult.False :
765
+ left.parameters.length > right.parameters.length ? TypeExtendsResult.False :
766
+ (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False :
767
+ IntoBooleanResult(Visit(left.returns, right.returns)));
806
768
  }
807
769
  // --------------------------------------------------------------------------
808
770
  // Date
809
771
  // --------------------------------------------------------------------------
810
772
  function TDate(left, right) {
811
- if (IsStructuralRight(right))
812
- return StructuralRight(left, right);
813
- if (TypeGuard.TObject(right))
814
- return TObjectRight(left, right);
815
- if (TypeGuard.TRecord(right))
816
- return TRecordRight(left, right);
817
- return TypeGuard.TDate(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
773
+ // prettier-ignore
774
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
775
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
776
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
777
+ TypeGuard.TDate(right) ? TypeExtendsResult.True :
778
+ TypeExtendsResult.False);
818
779
  }
819
780
  // --------------------------------------------------------------------------
820
781
  // Function
821
782
  // --------------------------------------------------------------------------
822
783
  function TFunction(left, right) {
823
- if (IsStructuralRight(right))
824
- return StructuralRight(left, right);
825
- if (TypeGuard.TObject(right))
826
- return TObjectRight(left, right);
827
- if (!TypeGuard.TFunction(right))
828
- return TypeExtendsResult.False;
829
- if (left.parameters.length > right.parameters.length)
830
- return TypeExtendsResult.False;
831
- if (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) {
832
- return TypeExtendsResult.False;
833
- }
834
- return IntoBooleanResult(Visit(left.returns, right.returns));
784
+ // prettier-ignore
785
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
786
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
787
+ !TypeGuard.TFunction(right) ? TypeExtendsResult.False :
788
+ left.parameters.length > right.parameters.length ? TypeExtendsResult.False :
789
+ (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False :
790
+ IntoBooleanResult(Visit(left.returns, right.returns)));
835
791
  }
836
792
  // --------------------------------------------------------------------------
837
793
  // Integer
838
794
  // --------------------------------------------------------------------------
839
795
  function TIntegerRight(left, right) {
840
- if (TypeGuard.TLiteral(left) && ValueGuard.IsNumber(left.const))
841
- return TypeExtendsResult.True;
842
- return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
796
+ // prettier-ignore
797
+ return (TypeGuard.TLiteral(left) && ValueGuard.IsNumber(left.const) ? TypeExtendsResult.True :
798
+ TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True :
799
+ TypeExtendsResult.False);
843
800
  }
844
801
  function TInteger(left, right) {
845
- if (IsStructuralRight(right))
846
- return StructuralRight(left, right);
847
- if (TypeGuard.TObject(right))
848
- return TObjectRight(left, right);
849
- if (TypeGuard.TRecord(right))
850
- return TRecordRight(left, right);
851
- return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
802
+ // prettier-ignore
803
+ return (TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True :
804
+ IsStructuralRight(right) ? StructuralRight(left, right) :
805
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
806
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
807
+ TypeExtendsResult.False);
852
808
  }
853
809
  // --------------------------------------------------------------------------
854
810
  // Intersect
855
811
  // --------------------------------------------------------------------------
856
812
  function TIntersectRight(left, right) {
857
- return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
813
+ // prettier-ignore
814
+ return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True)
815
+ ? TypeExtendsResult.True
816
+ : TypeExtendsResult.False;
858
817
  }
859
818
  function TIntersect(left, right) {
860
- return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
819
+ // prettier-ignore
820
+ return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True)
821
+ ? TypeExtendsResult.True
822
+ : TypeExtendsResult.False;
861
823
  }
862
824
  // --------------------------------------------------------------------------
863
825
  // Iterator
864
826
  // --------------------------------------------------------------------------
865
827
  function TIterator(left, right) {
866
- if (IsStructuralRight(right))
867
- return StructuralRight(left, right);
868
- if (!TypeGuard.TIterator(right))
869
- return TypeExtendsResult.False;
870
- return IntoBooleanResult(Visit(left.items, right.items));
828
+ // prettier-ignore
829
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
830
+ !TypeGuard.TIterator(right) ? TypeExtendsResult.False :
831
+ IntoBooleanResult(Visit(left.items, right.items)));
871
832
  }
872
833
  // --------------------------------------------------------------------------
873
834
  // Literal
874
835
  // --------------------------------------------------------------------------
875
836
  function TLiteral(left, right) {
876
- if (IsStructuralRight(right))
877
- return StructuralRight(left, right);
878
- if (TypeGuard.TObject(right))
879
- return TObjectRight(left, right);
880
- if (TypeGuard.TRecord(right))
881
- return TRecordRight(left, right);
882
- if (TypeGuard.TString(right))
883
- return TStringRight(left, right);
884
- if (TypeGuard.TNumber(right))
885
- return TNumberRight(left, right);
886
- if (TypeGuard.TInteger(right))
887
- return TIntegerRight(left, right);
888
- if (TypeGuard.TBoolean(right))
889
- return TBooleanRight(left, right);
890
- return TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True : TypeExtendsResult.False;
837
+ // prettier-ignore
838
+ return (TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True :
839
+ IsStructuralRight(right) ? StructuralRight(left, right) :
840
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
841
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
842
+ TypeGuard.TString(right) ? TStringRight(left, right) :
843
+ TypeGuard.TNumber(right) ? TNumberRight(left, right) :
844
+ TypeGuard.TInteger(right) ? TIntegerRight(left, right) :
845
+ TypeGuard.TBoolean(right) ? TBooleanRight(left, right) :
846
+ TypeExtendsResult.False);
891
847
  }
892
848
  // --------------------------------------------------------------------------
893
849
  // Never
@@ -915,40 +871,38 @@ var TypeExtends;
915
871
  // TypeScript has no concept of negated types, and attempts to correctly check the negated
916
872
  // type at runtime would put TypeBox at odds with TypeScripts ability to statically infer
917
873
  // the type. Instead we unwrap to either unknown or T and continue evaluating.
918
- if (TypeGuard.TNot(left))
919
- return Visit(UnwrapTNot(left), right);
920
- if (TypeGuard.TNot(right))
921
- return Visit(left, UnwrapTNot(right));
922
- throw new Error(`TypeExtends: Invalid fallthrough for Not`);
874
+ // prettier-ignore
875
+ return (TypeGuard.TNot(left) ? Visit(UnwrapTNot(left), right) :
876
+ TypeGuard.TNot(right) ? Visit(left, UnwrapTNot(right)) :
877
+ Throw('Invalid fallthrough for Not'));
923
878
  }
924
879
  // --------------------------------------------------------------------------
925
880
  // Null
926
881
  // --------------------------------------------------------------------------
927
882
  function TNull(left, right) {
928
- if (IsStructuralRight(right))
929
- return StructuralRight(left, right);
930
- if (TypeGuard.TObject(right))
931
- return TObjectRight(left, right);
932
- if (TypeGuard.TRecord(right))
933
- return TRecordRight(left, right);
934
- return TypeGuard.TNull(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
883
+ // prettier-ignore
884
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
885
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
886
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
887
+ TypeGuard.TNull(right) ? TypeExtendsResult.True :
888
+ TypeExtendsResult.False);
935
889
  }
936
890
  // --------------------------------------------------------------------------
937
891
  // Number
938
892
  // --------------------------------------------------------------------------
939
893
  function TNumberRight(left, right) {
940
- if (TypeGuard.TLiteralNumber(left))
941
- return TypeExtendsResult.True;
942
- return TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
894
+ // prettier-ignore
895
+ return (TypeGuard.TLiteralNumber(left) ? TypeExtendsResult.True :
896
+ TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True :
897
+ TypeExtendsResult.False);
943
898
  }
944
899
  function TNumber(left, right) {
945
- if (IsStructuralRight(right))
946
- return StructuralRight(left, right);
947
- if (TypeGuard.TObject(right))
948
- return TObjectRight(left, right);
949
- if (TypeGuard.TRecord(right))
950
- return TRecordRight(left, right);
951
- return TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
900
+ // prettier-ignore
901
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
902
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
903
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
904
+ TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True :
905
+ TypeExtendsResult.False);
952
906
  }
953
907
  // --------------------------------------------------------------------------
954
908
  // Object
@@ -999,161 +953,133 @@ var TypeExtends;
999
953
  // Property
1000
954
  // --------------------------------------------------------------------------
1001
955
  function Property(left, right) {
1002
- if (Visit(left, right) === TypeExtendsResult.False)
1003
- return TypeExtendsResult.False;
1004
- if (TypeGuard.TOptional(left) && !TypeGuard.TOptional(right))
1005
- return TypeExtendsResult.False;
1006
- return TypeExtendsResult.True;
956
+ // prettier-ignore
957
+ return (Visit(left, right) === TypeExtendsResult.False ? TypeExtendsResult.False :
958
+ TypeGuard.TOptional(left) && !TypeGuard.TOptional(right) ? TypeExtendsResult.False :
959
+ TypeExtendsResult.True);
1007
960
  }
1008
961
  function TObjectRight(left, right) {
1009
- if (TypeGuard.TUnknown(left))
1010
- return TypeExtendsResult.False;
1011
- if (TypeGuard.TAny(left))
1012
- return TypeExtendsResult.Union;
1013
- if (TypeGuard.TNever(left))
1014
- return TypeExtendsResult.True;
1015
- if (TypeGuard.TLiteralString(left) && IsObjectStringLike(right))
1016
- return TypeExtendsResult.True;
1017
- if (TypeGuard.TLiteralNumber(left) && IsObjectNumberLike(right))
1018
- return TypeExtendsResult.True;
1019
- if (TypeGuard.TLiteralBoolean(left) && IsObjectBooleanLike(right))
1020
- return TypeExtendsResult.True;
1021
- if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
1022
- return TypeExtendsResult.True;
1023
- if (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right))
1024
- return TypeExtendsResult.True;
1025
- if (TypeGuard.TString(left) && IsObjectStringLike(right))
1026
- return TypeExtendsResult.True;
1027
- if (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right))
1028
- return TypeExtendsResult.True;
1029
- if (TypeGuard.TNumber(left) && IsObjectNumberLike(right))
1030
- return TypeExtendsResult.True;
1031
- if (TypeGuard.TInteger(left) && IsObjectNumberLike(right))
1032
- return TypeExtendsResult.True;
1033
- if (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right))
1034
- return TypeExtendsResult.True;
1035
- if (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right))
1036
- return TypeExtendsResult.True;
1037
- if (TypeGuard.TDate(left) && IsObjectDateLike(right))
1038
- return TypeExtendsResult.True;
1039
- if (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right))
1040
- return TypeExtendsResult.True;
1041
- if (TypeGuard.TFunction(left) && IsObjectFunctionLike(right))
1042
- return TypeExtendsResult.True;
1043
- if (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) {
1044
- // When expressing a Record with literal key values, the Record is converted into a Object with
1045
- // the Hint assigned as `Record`. This is used to invert the extends logic.
1046
- return right[exports.Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False;
1047
- }
1048
- if (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) {
1049
- return IsObjectPropertyCount(right, 0) ? TypeExtendsResult.True : TypeExtendsResult.False;
1050
- }
1051
- return TypeExtendsResult.False;
962
+ // prettier-ignore
963
+ return (TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
964
+ TypeGuard.TAny(left) ? TypeExtendsResult.Union : (TypeGuard.TNever(left) ||
965
+ (TypeGuard.TLiteralString(left) && IsObjectStringLike(right)) ||
966
+ (TypeGuard.TLiteralNumber(left) && IsObjectNumberLike(right)) ||
967
+ (TypeGuard.TLiteralBoolean(left) && IsObjectBooleanLike(right)) ||
968
+ (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) ||
969
+ (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right)) ||
970
+ (TypeGuard.TString(left) && IsObjectStringLike(right)) ||
971
+ (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) ||
972
+ (TypeGuard.TNumber(left) && IsObjectNumberLike(right)) ||
973
+ (TypeGuard.TInteger(left) && IsObjectNumberLike(right)) ||
974
+ (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right)) ||
975
+ (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right)) ||
976
+ (TypeGuard.TDate(left) && IsObjectDateLike(right)) ||
977
+ (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right)) ||
978
+ (TypeGuard.TFunction(left) && IsObjectFunctionLike(right))) ? TypeExtendsResult.True :
979
+ (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) ? (() => {
980
+ // When expressing a Record with literal key values, the Record is converted into a Object with
981
+ // the Hint assigned as `Record`. This is used to invert the extends logic.
982
+ return right[exports.Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False;
983
+ })() :
984
+ (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) ? (() => {
985
+ return IsObjectPropertyCount(right, 0)
986
+ ? TypeExtendsResult.True
987
+ : TypeExtendsResult.False;
988
+ })() :
989
+ TypeExtendsResult.False);
1052
990
  }
1053
991
  function TObject(left, right) {
1054
- if (IsStructuralRight(right))
1055
- return StructuralRight(left, right);
1056
- if (TypeGuard.TRecord(right))
1057
- return TRecordRight(left, right);
1058
- if (!TypeGuard.TObject(right))
1059
- return TypeExtendsResult.False;
1060
- for (const key of Object.getOwnPropertyNames(right.properties)) {
1061
- if (!(key in left.properties))
1062
- return TypeExtendsResult.False;
1063
- if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
1064
- return TypeExtendsResult.False;
1065
- }
1066
- }
1067
- return TypeExtendsResult.True;
992
+ // prettier-ignore
993
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
994
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
995
+ !TypeGuard.TObject(right) ? TypeExtendsResult.False :
996
+ (() => {
997
+ for (const key of Object.getOwnPropertyNames(right.properties)) {
998
+ if (!(key in left.properties))
999
+ return TypeExtendsResult.False;
1000
+ if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
1001
+ return TypeExtendsResult.False;
1002
+ }
1003
+ }
1004
+ return TypeExtendsResult.True;
1005
+ })());
1068
1006
  }
1069
1007
  // --------------------------------------------------------------------------
1070
1008
  // Promise
1071
1009
  // --------------------------------------------------------------------------
1072
1010
  function TPromise(left, right) {
1073
- if (IsStructuralRight(right))
1074
- return StructuralRight(left, right);
1075
- if (TypeGuard.TObject(right) && IsObjectPromiseLike(right))
1076
- return TypeExtendsResult.True;
1077
- if (!TypeGuard.TPromise(right))
1078
- return TypeExtendsResult.False;
1079
- return IntoBooleanResult(Visit(left.item, right.item));
1011
+ // prettier-ignore
1012
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
1013
+ TypeGuard.TObject(right) && IsObjectPromiseLike(right) ? TypeExtendsResult.True :
1014
+ !TypeGuard.TPromise(right) ? TypeExtendsResult.False :
1015
+ IntoBooleanResult(Visit(left.item, right.item)));
1080
1016
  }
1081
1017
  // --------------------------------------------------------------------------
1082
1018
  // Record
1083
1019
  // --------------------------------------------------------------------------
1084
1020
  function RecordKey(schema) {
1085
- if (exports.PatternNumberExact in schema.patternProperties)
1086
- return exports.Type.Number();
1087
- if (exports.PatternStringExact in schema.patternProperties)
1088
- return exports.Type.String();
1089
- throw Error('TypeExtends: Cannot get record key');
1021
+ // prettier-ignore
1022
+ return (exports.PatternNumberExact in schema.patternProperties ? exports.Type.Number() :
1023
+ exports.PatternStringExact in schema.patternProperties ? exports.Type.String() :
1024
+ Throw('Unknown record key pattern'));
1090
1025
  }
1091
1026
  function RecordValue(schema) {
1092
- if (exports.PatternNumberExact in schema.patternProperties)
1093
- return schema.patternProperties[exports.PatternNumberExact];
1094
- if (exports.PatternStringExact in schema.patternProperties)
1095
- return schema.patternProperties[exports.PatternStringExact];
1096
- throw Error('TypeExtends: Cannot get record value');
1027
+ // prettier-ignore
1028
+ return (exports.PatternNumberExact in schema.patternProperties ? schema.patternProperties[exports.PatternNumberExact] :
1029
+ exports.PatternStringExact in schema.patternProperties ? schema.patternProperties[exports.PatternStringExact] :
1030
+ Throw('Unable to get record value schema'));
1097
1031
  }
1098
1032
  function TRecordRight(left, right) {
1099
- const Key = RecordKey(right);
1100
- const Value = RecordValue(right);
1101
- if (TypeGuard.TLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True)
1102
- return TypeExtendsResult.True;
1103
- if (TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key))
1104
- return Visit(left, Value);
1105
- if (TypeGuard.TString(left) && TypeGuard.TNumber(Key))
1106
- return Visit(left, Value);
1107
- if (TypeGuard.TArray(left) && TypeGuard.TNumber(Key))
1108
- return Visit(left, Value);
1109
- if (TypeGuard.TObject(left)) {
1110
- for (const key of Object.getOwnPropertyNames(left.properties)) {
1111
- if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
1112
- return TypeExtendsResult.False;
1113
- }
1114
- }
1115
- return TypeExtendsResult.True;
1116
- }
1117
- return TypeExtendsResult.False;
1033
+ const [Key, Value] = [RecordKey(right), RecordValue(right)];
1034
+ // prettier-ignore
1035
+ return ((TypeGuard.TLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True) ? TypeExtendsResult.True :
1036
+ TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
1037
+ TypeGuard.TString(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
1038
+ TypeGuard.TArray(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
1039
+ TypeGuard.TObject(left) ? (() => {
1040
+ for (const key of Object.getOwnPropertyNames(left.properties)) {
1041
+ if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
1042
+ return TypeExtendsResult.False;
1043
+ }
1044
+ }
1045
+ return TypeExtendsResult.True;
1046
+ })() :
1047
+ TypeExtendsResult.False);
1118
1048
  }
1119
1049
  function TRecord(left, right) {
1120
- const Value = RecordValue(left);
1121
- if (IsStructuralRight(right))
1122
- return StructuralRight(left, right);
1123
- if (TypeGuard.TObject(right))
1124
- return TObjectRight(left, right);
1125
- if (!TypeGuard.TRecord(right))
1126
- return TypeExtendsResult.False;
1127
- return Visit(Value, RecordValue(right));
1050
+ // prettier-ignore
1051
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
1052
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
1053
+ !TypeGuard.TRecord(right) ? TypeExtendsResult.False :
1054
+ Visit(RecordValue(left), RecordValue(right)));
1128
1055
  }
1129
1056
  // --------------------------------------------------------------------------
1130
1057
  // String
1131
1058
  // --------------------------------------------------------------------------
1132
1059
  function TStringRight(left, right) {
1133
- if (TypeGuard.TLiteral(left) && ValueGuard.IsString(left.const))
1134
- return TypeExtendsResult.True;
1135
- return TypeGuard.TString(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
1060
+ // prettier-ignore
1061
+ return (TypeGuard.TLiteral(left) && ValueGuard.IsString(left.const) ? TypeExtendsResult.True :
1062
+ TypeGuard.TString(left) ? TypeExtendsResult.True :
1063
+ TypeExtendsResult.False);
1136
1064
  }
1137
1065
  function TString(left, right) {
1138
- if (IsStructuralRight(right))
1139
- return StructuralRight(left, right);
1140
- if (TypeGuard.TObject(right))
1141
- return TObjectRight(left, right);
1142
- if (TypeGuard.TRecord(right))
1143
- return TRecordRight(left, right);
1144
- return TypeGuard.TString(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1066
+ // prettier-ignore
1067
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
1068
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
1069
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1070
+ TypeGuard.TString(right) ? TypeExtendsResult.True :
1071
+ TypeExtendsResult.False);
1145
1072
  }
1146
1073
  // --------------------------------------------------------------------------
1147
1074
  // Symbol
1148
1075
  // --------------------------------------------------------------------------
1149
1076
  function TSymbol(left, right) {
1150
- if (IsStructuralRight(right))
1151
- return StructuralRight(left, right);
1152
- if (TypeGuard.TObject(right))
1153
- return TObjectRight(left, right);
1154
- if (TypeGuard.TRecord(right))
1155
- return TRecordRight(left, right);
1156
- return TypeGuard.TSymbol(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1077
+ // prettier-ignore
1078
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
1079
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
1080
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1081
+ TypeGuard.TSymbol(right) ? TypeExtendsResult.True :
1082
+ TypeExtendsResult.False);
1157
1083
  }
1158
1084
  // --------------------------------------------------------------------------
1159
1085
  // TemplateLiteral
@@ -1162,11 +1088,10 @@ var TypeExtends;
1162
1088
  // TemplateLiteral types are resolved to either unions for finite expressions or string
1163
1089
  // for infinite expressions. Here we call to TemplateLiteralResolver to resolve for
1164
1090
  // either type and continue evaluating.
1165
- if (TypeGuard.TTemplateLiteral(left))
1166
- return Visit(TemplateLiteralResolver.Resolve(left), right);
1167
- if (TypeGuard.TTemplateLiteral(right))
1168
- return Visit(left, TemplateLiteralResolver.Resolve(right));
1169
- throw new Error(`TypeExtends: Invalid fallthrough for TemplateLiteral`);
1091
+ // prettier-ignore
1092
+ return (TypeGuard.TTemplateLiteral(left) ? Visit(TemplateLiteralResolver.Resolve(left), right) :
1093
+ TypeGuard.TTemplateLiteral(right) ? Visit(left, TemplateLiteralResolver.Resolve(right)) :
1094
+ Throw('Invalid fallthrough for TemplateLiteral'));
1170
1095
  }
1171
1096
  // --------------------------------------------------------------------------
1172
1097
  // Tuple
@@ -1178,63 +1103,60 @@ var TypeExtends;
1178
1103
  left.items.every((schema) => Visit(schema, right.items) === TypeExtendsResult.True));
1179
1104
  }
1180
1105
  function TTupleRight(left, right) {
1181
- if (TypeGuard.TNever(left))
1182
- return TypeExtendsResult.True;
1183
- if (TypeGuard.TUnknown(left))
1184
- return TypeExtendsResult.False;
1185
- if (TypeGuard.TAny(left))
1186
- return TypeExtendsResult.Union;
1187
- return TypeExtendsResult.False;
1106
+ // prettier-ignore
1107
+ return (TypeGuard.TNever(left) ? TypeExtendsResult.True :
1108
+ TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
1109
+ TypeGuard.TAny(left) ? TypeExtendsResult.Union :
1110
+ TypeExtendsResult.False);
1188
1111
  }
1189
1112
  function TTuple(left, right) {
1190
- if (IsStructuralRight(right))
1191
- return StructuralRight(left, right);
1192
- if (TypeGuard.TObject(right) && IsObjectArrayLike(right))
1193
- return TypeExtendsResult.True;
1194
- if (TypeGuard.TArray(right) && IsArrayOfTuple(left, right))
1195
- return TypeExtendsResult.True;
1196
- if (!TypeGuard.TTuple(right))
1197
- return TypeExtendsResult.False;
1198
- if ((ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) || (!ValueGuard.IsUndefined(left.items) && ValueGuard.IsUndefined(right.items)))
1199
- return TypeExtendsResult.False;
1200
- if (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items))
1201
- return TypeExtendsResult.True;
1202
- return left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1113
+ // prettier-ignore
1114
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
1115
+ TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True :
1116
+ TypeGuard.TArray(right) && IsArrayOfTuple(left, right) ? TypeExtendsResult.True :
1117
+ !TypeGuard.TTuple(right) ? TypeExtendsResult.False :
1118
+ (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) || (!ValueGuard.IsUndefined(left.items) && ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.False :
1119
+ (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.True :
1120
+ left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True :
1121
+ TypeExtendsResult.False);
1203
1122
  }
1204
1123
  // --------------------------------------------------------------------------
1205
1124
  // Uint8Array
1206
1125
  // --------------------------------------------------------------------------
1207
1126
  function TUint8Array(left, right) {
1208
- if (IsStructuralRight(right))
1209
- return StructuralRight(left, right);
1210
- if (TypeGuard.TObject(right))
1211
- return TObjectRight(left, right);
1212
- if (TypeGuard.TRecord(right))
1213
- return TRecordRight(left, right);
1214
- return TypeGuard.TUint8Array(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1127
+ // prettier-ignore
1128
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
1129
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
1130
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1131
+ TypeGuard.TUint8Array(right) ? TypeExtendsResult.True :
1132
+ TypeExtendsResult.False);
1215
1133
  }
1216
1134
  // --------------------------------------------------------------------------
1217
1135
  // Undefined
1218
1136
  // --------------------------------------------------------------------------
1219
1137
  function TUndefined(left, right) {
1220
- if (IsStructuralRight(right))
1221
- return StructuralRight(left, right);
1222
- if (TypeGuard.TObject(right))
1223
- return TObjectRight(left, right);
1224
- if (TypeGuard.TRecord(right))
1225
- return TRecordRight(left, right);
1226
- if (TypeGuard.TVoid(right))
1227
- return VoidRight(left, right);
1228
- return TypeGuard.TUndefined(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1138
+ // prettier-ignore
1139
+ return (IsStructuralRight(right) ? StructuralRight(left, right) :
1140
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
1141
+ TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1142
+ TypeGuard.TVoid(right) ? VoidRight(left, right) :
1143
+ TypeGuard.TUndefined(right) ? TypeExtendsResult.True :
1144
+ TypeExtendsResult.False);
1229
1145
  }
1230
1146
  // --------------------------------------------------------------------------
1231
1147
  // Union
1232
1148
  // --------------------------------------------------------------------------
1233
1149
  function TUnionRight(left, right) {
1234
- return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1150
+ // prettier-ignore
1151
+ return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True)
1152
+ ? TypeExtendsResult.True
1153
+ : TypeExtendsResult.False;
1235
1154
  }
1236
1155
  function TUnion(left, right) {
1237
- return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False;
1156
+ // prettier-ignore
1157
+ return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True)
1158
+ ? TypeExtendsResult.True
1159
+ : TypeExtendsResult.False;
1238
1160
  }
1239
1161
  // --------------------------------------------------------------------------
1240
1162
  // Unknown
@@ -1243,111 +1165,74 @@ var TypeExtends;
1243
1165
  return TypeExtendsResult.True;
1244
1166
  }
1245
1167
  function TUnknown(left, right) {
1246
- if (TypeGuard.TNever(right))
1247
- return TNeverRight(left, right);
1248
- if (TypeGuard.TIntersect(right))
1249
- return TIntersectRight(left, right);
1250
- if (TypeGuard.TUnion(right))
1251
- return TUnionRight(left, right);
1252
- if (TypeGuard.TAny(right))
1253
- return TAnyRight(left, right);
1254
- if (TypeGuard.TString(right))
1255
- return TStringRight(left, right);
1256
- if (TypeGuard.TNumber(right))
1257
- return TNumberRight(left, right);
1258
- if (TypeGuard.TInteger(right))
1259
- return TIntegerRight(left, right);
1260
- if (TypeGuard.TBoolean(right))
1261
- return TBooleanRight(left, right);
1262
- if (TypeGuard.TArray(right))
1263
- return TArrayRight(left, right);
1264
- if (TypeGuard.TTuple(right))
1265
- return TTupleRight(left, right);
1266
- if (TypeGuard.TObject(right))
1267
- return TObjectRight(left, right);
1268
- return TypeGuard.TUnknown(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1168
+ // prettier-ignore
1169
+ return (TypeGuard.TNever(right) ? TNeverRight(left, right) :
1170
+ TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
1171
+ TypeGuard.TUnion(right) ? TUnionRight(left, right) :
1172
+ TypeGuard.TAny(right) ? TAnyRight(left, right) :
1173
+ TypeGuard.TString(right) ? TStringRight(left, right) :
1174
+ TypeGuard.TNumber(right) ? TNumberRight(left, right) :
1175
+ TypeGuard.TInteger(right) ? TIntegerRight(left, right) :
1176
+ TypeGuard.TBoolean(right) ? TBooleanRight(left, right) :
1177
+ TypeGuard.TArray(right) ? TArrayRight(left, right) :
1178
+ TypeGuard.TTuple(right) ? TTupleRight(left, right) :
1179
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
1180
+ TypeGuard.TUnknown(right) ? TypeExtendsResult.True :
1181
+ TypeExtendsResult.False);
1269
1182
  }
1270
1183
  // --------------------------------------------------------------------------
1271
1184
  // Void
1272
1185
  // --------------------------------------------------------------------------
1273
1186
  function VoidRight(left, right) {
1274
- if (TypeGuard.TUndefined(left))
1275
- return TypeExtendsResult.True;
1276
- return TypeGuard.TUndefined(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
1187
+ // prettier-ignore
1188
+ return TypeGuard.TUndefined(left) ? TypeExtendsResult.True :
1189
+ TypeGuard.TUndefined(left) ? TypeExtendsResult.True :
1190
+ TypeExtendsResult.False;
1277
1191
  }
1278
1192
  function TVoid(left, right) {
1279
- if (TypeGuard.TIntersect(right))
1280
- return TIntersectRight(left, right);
1281
- if (TypeGuard.TUnion(right))
1282
- return TUnionRight(left, right);
1283
- if (TypeGuard.TUnknown(right))
1284
- return TUnknownRight(left, right);
1285
- if (TypeGuard.TAny(right))
1286
- return TAnyRight(left, right);
1287
- if (TypeGuard.TObject(right))
1288
- return TObjectRight(left, right);
1289
- return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
1193
+ // prettier-ignore
1194
+ return TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
1195
+ TypeGuard.TUnion(right) ? TUnionRight(left, right) :
1196
+ TypeGuard.TUnknown(right) ? TUnknownRight(left, right) :
1197
+ TypeGuard.TAny(right) ? TAnyRight(left, right) :
1198
+ TypeGuard.TObject(right) ? TObjectRight(left, right) :
1199
+ TypeGuard.TVoid(right) ? TypeExtendsResult.True :
1200
+ TypeExtendsResult.False;
1290
1201
  }
1291
1202
  function Visit(left, right) {
1292
- // Resolvable Types
1293
- if (TypeGuard.TTemplateLiteral(left) || TypeGuard.TTemplateLiteral(right))
1294
- return TTemplateLiteral(left, right);
1295
- if (TypeGuard.TNot(left) || TypeGuard.TNot(right))
1296
- return TNot(left, right);
1297
- // Standard Types
1298
- if (TypeGuard.TAny(left))
1299
- return TAny(left, right);
1300
- if (TypeGuard.TArray(left))
1301
- return TArray(left, right);
1302
- if (TypeGuard.TBigInt(left))
1303
- return TBigInt(left, right);
1304
- if (TypeGuard.TBoolean(left))
1305
- return TBoolean(left, right);
1306
- if (TypeGuard.TAsyncIterator(left))
1307
- return TAsyncIterator(left, right);
1308
- if (TypeGuard.TConstructor(left))
1309
- return TConstructor(left, right);
1310
- if (TypeGuard.TDate(left))
1311
- return TDate(left, right);
1312
- if (TypeGuard.TFunction(left))
1313
- return TFunction(left, right);
1314
- if (TypeGuard.TInteger(left))
1315
- return TInteger(left, right);
1316
- if (TypeGuard.TIntersect(left))
1317
- return TIntersect(left, right);
1318
- if (TypeGuard.TIterator(left))
1319
- return TIterator(left, right);
1320
- if (TypeGuard.TLiteral(left))
1321
- return TLiteral(left, right);
1322
- if (TypeGuard.TNever(left))
1323
- return TNever(left, right);
1324
- if (TypeGuard.TNull(left))
1325
- return TNull(left, right);
1326
- if (TypeGuard.TNumber(left))
1327
- return TNumber(left, right);
1328
- if (TypeGuard.TObject(left))
1329
- return TObject(left, right);
1330
- if (TypeGuard.TRecord(left))
1331
- return TRecord(left, right);
1332
- if (TypeGuard.TString(left))
1333
- return TString(left, right);
1334
- if (TypeGuard.TSymbol(left))
1335
- return TSymbol(left, right);
1336
- if (TypeGuard.TTuple(left))
1337
- return TTuple(left, right);
1338
- if (TypeGuard.TPromise(left))
1339
- return TPromise(left, right);
1340
- if (TypeGuard.TUint8Array(left))
1341
- return TUint8Array(left, right);
1342
- if (TypeGuard.TUndefined(left))
1343
- return TUndefined(left, right);
1344
- if (TypeGuard.TUnion(left))
1345
- return TUnion(left, right);
1346
- if (TypeGuard.TUnknown(left))
1347
- return TUnknown(left, right);
1348
- if (TypeGuard.TVoid(left))
1349
- return TVoid(left, right);
1350
- throw Error(`TypeExtends: Unknown left type operand '${left[exports.Kind]}'`);
1203
+ // prettier-ignore
1204
+ return (
1205
+ // resolvable
1206
+ (TypeGuard.TTemplateLiteral(left) || TypeGuard.TTemplateLiteral(right)) ? TTemplateLiteral(left, right) :
1207
+ (TypeGuard.TNot(left) || TypeGuard.TNot(right)) ? TNot(left, right) :
1208
+ // standard
1209
+ TypeGuard.TAny(left) ? TAny(left, right) :
1210
+ TypeGuard.TArray(left) ? TArray(left, right) :
1211
+ TypeGuard.TBigInt(left) ? TBigInt(left, right) :
1212
+ TypeGuard.TBoolean(left) ? TBoolean(left, right) :
1213
+ TypeGuard.TAsyncIterator(left) ? TAsyncIterator(left, right) :
1214
+ TypeGuard.TConstructor(left) ? TConstructor(left, right) :
1215
+ TypeGuard.TDate(left) ? TDate(left, right) :
1216
+ TypeGuard.TFunction(left) ? TFunction(left, right) :
1217
+ TypeGuard.TInteger(left) ? TInteger(left, right) :
1218
+ TypeGuard.TIntersect(left) ? TIntersect(left, right) :
1219
+ TypeGuard.TIterator(left) ? TIterator(left, right) :
1220
+ TypeGuard.TLiteral(left) ? TLiteral(left, right) :
1221
+ TypeGuard.TNever(left) ? TNever(left, right) :
1222
+ TypeGuard.TNull(left) ? TNull(left, right) :
1223
+ TypeGuard.TNumber(left) ? TNumber(left, right) :
1224
+ TypeGuard.TObject(left) ? TObject(left, right) :
1225
+ TypeGuard.TRecord(left) ? TRecord(left, right) :
1226
+ TypeGuard.TString(left) ? TString(left, right) :
1227
+ TypeGuard.TSymbol(left) ? TSymbol(left, right) :
1228
+ TypeGuard.TTuple(left) ? TTuple(left, right) :
1229
+ TypeGuard.TPromise(left) ? TPromise(left, right) :
1230
+ TypeGuard.TUint8Array(left) ? TUint8Array(left, right) :
1231
+ TypeGuard.TUndefined(left) ? TUndefined(left, right) :
1232
+ TypeGuard.TUnion(left) ? TUnion(left, right) :
1233
+ TypeGuard.TUnknown(left) ? TUnknown(left, right) :
1234
+ TypeGuard.TVoid(left) ? TVoid(left, right) :
1235
+ Throw(`Unknown left type operand '${left[exports.Kind]}'`));
1351
1236
  }
1352
1237
  function Extends(left, right) {
1353
1238
  return Visit(left, right);
@@ -1369,17 +1254,21 @@ var TypeClone;
1369
1254
  return value.map((value) => Visit(value));
1370
1255
  }
1371
1256
  function Visit(value) {
1372
- if (ValueGuard.IsArray(value))
1373
- return ArrayType(value);
1374
- if (ValueGuard.IsObject(value))
1375
- return ObjectType(value);
1376
- return value;
1377
- }
1378
- /** Clones a type. */
1379
- function Clone(schema, options = {}) {
1257
+ // prettier-ignore
1258
+ return ValueGuard.IsArray(value) ? ArrayType(value) :
1259
+ ValueGuard.IsObject(value) ? ObjectType(value) :
1260
+ value;
1261
+ }
1262
+ /** Clones a Rest */
1263
+ function Rest(schemas) {
1264
+ return schemas.map((schema) => Type(schema));
1265
+ }
1266
+ TypeClone.Rest = Rest;
1267
+ /** Clones a Type */
1268
+ function Type(schema, options = {}) {
1380
1269
  return { ...Visit(schema), ...options };
1381
1270
  }
1382
- TypeClone.Clone = Clone;
1271
+ TypeClone.Type = Type;
1383
1272
  })(TypeClone || (exports.TypeClone = TypeClone = {}));
1384
1273
  // --------------------------------------------------------------------------
1385
1274
  // IndexedAccessor
@@ -1388,7 +1277,7 @@ var IndexedAccessor;
1388
1277
  (function (IndexedAccessor) {
1389
1278
  function OptionalUnwrap(schema) {
1390
1279
  return schema.map((schema) => {
1391
- const { [exports.Optional]: _, ...clone } = TypeClone.Clone(schema);
1280
+ const { [exports.Optional]: _, ...clone } = TypeClone.Type(schema);
1392
1281
  return clone;
1393
1282
  });
1394
1283
  }
@@ -1399,19 +1288,16 @@ var IndexedAccessor;
1399
1288
  return schema.some((schema) => TypeGuard.TOptional(schema));
1400
1289
  }
1401
1290
  function ResolveIntersect(schema) {
1402
- const optional = IsIntersectOptional(schema.allOf);
1403
- return optional ? exports.Type.Optional(exports.Type.Intersect(OptionalUnwrap(schema.allOf))) : schema;
1291
+ return IsIntersectOptional(schema.allOf) ? exports.Type.Optional(exports.Type.Intersect(OptionalUnwrap(schema.allOf))) : schema;
1404
1292
  }
1405
1293
  function ResolveUnion(schema) {
1406
- const optional = IsUnionOptional(schema.anyOf);
1407
- return optional ? exports.Type.Optional(exports.Type.Union(OptionalUnwrap(schema.anyOf))) : schema;
1294
+ return IsUnionOptional(schema.anyOf) ? exports.Type.Optional(exports.Type.Union(OptionalUnwrap(schema.anyOf))) : schema;
1408
1295
  }
1409
1296
  function ResolveOptional(schema) {
1410
- if (schema[exports.Kind] === 'Intersect')
1411
- return ResolveIntersect(schema);
1412
- if (schema[exports.Kind] === 'Union')
1413
- return ResolveUnion(schema);
1414
- return schema;
1297
+ // prettier-ignore
1298
+ return schema[exports.Kind] === 'Intersect' ? ResolveIntersect(schema) :
1299
+ schema[exports.Kind] === 'Union' ? ResolveUnion(schema) :
1300
+ schema;
1415
1301
  }
1416
1302
  function TIntersect(schema, key) {
1417
1303
  const resolved = schema.allOf.reduce((acc, schema) => {
@@ -1438,15 +1324,12 @@ var IndexedAccessor;
1438
1324
  return element;
1439
1325
  }
1440
1326
  function Visit(schema, key) {
1441
- if (schema[exports.Kind] === 'Intersect')
1442
- return TIntersect(schema, key);
1443
- if (schema[exports.Kind] === 'Union')
1444
- return TUnion(schema, key);
1445
- if (schema[exports.Kind] === 'Object')
1446
- return TObject(schema, key);
1447
- if (schema[exports.Kind] === 'Tuple')
1448
- return TTuple(schema, key);
1449
- return exports.Type.Never();
1327
+ // prettier-ignore
1328
+ return schema[exports.Kind] === 'Intersect' ? TIntersect(schema, key) :
1329
+ schema[exports.Kind] === 'Union' ? TUnion(schema, key) :
1330
+ schema[exports.Kind] === 'Object' ? TObject(schema, key) :
1331
+ schema[exports.Kind] === 'Tuple' ? TTuple(schema, key) :
1332
+ exports.Type.Never();
1450
1333
  }
1451
1334
  function Resolve(schema, keys, options = {}) {
1452
1335
  const resolved = keys.map((key) => Visit(schema, key.toString()));
@@ -1501,13 +1384,11 @@ var Intrinsic;
1501
1384
  return [Map(L, mode), ...IntrinsicRest(R, mode)];
1502
1385
  }
1503
1386
  function Visit(schema, mode) {
1504
- if (TypeGuard.TTemplateLiteral(schema))
1505
- return IntrinsicTemplateLiteral(schema, mode);
1506
- if (TypeGuard.TUnion(schema))
1507
- return exports.Type.Union(IntrinsicRest(schema.anyOf, mode));
1508
- if (TypeGuard.TLiteral(schema))
1509
- return exports.Type.Literal(IntrinsicLiteral(schema.const, mode));
1510
- return schema;
1387
+ // prettier-ignore
1388
+ return TypeGuard.TTemplateLiteral(schema) ? IntrinsicTemplateLiteral(schema, mode) :
1389
+ TypeGuard.TUnion(schema) ? exports.Type.Union(IntrinsicRest(schema.anyOf, mode)) :
1390
+ TypeGuard.TLiteral(schema) ? exports.Type.Literal(IntrinsicLiteral(schema.const, mode)) :
1391
+ schema;
1511
1392
  }
1512
1393
  /** Applies an intrinsic string manipulation to the given type. */
1513
1394
  function Map(schema, mode) {
@@ -1536,16 +1417,14 @@ var ObjectMap;
1536
1417
  // prevent sub schema mapping as unregistered kinds will not pass TSchema checks. This is notable in the
1537
1418
  // case of TObject where unregistered property kinds cause the TObject check to fail. As mapping is only
1538
1419
  // used for composition, we use explicit checks instead.
1539
- if (schema[exports.Kind] === 'Intersect')
1540
- return TIntersect(schema, callback);
1541
- if (schema[exports.Kind] === 'Union')
1542
- return TUnion(schema, callback);
1543
- if (schema[exports.Kind] === 'Object')
1544
- return TObject(schema, callback);
1545
- return schema;
1420
+ // prettier-ignore
1421
+ return (schema[exports.Kind] === 'Intersect' ? TIntersect(schema, callback) :
1422
+ schema[exports.Kind] === 'Union' ? TUnion(schema, callback) :
1423
+ schema[exports.Kind] === 'Object' ? TObject(schema, callback) :
1424
+ schema);
1546
1425
  }
1547
1426
  function Map(schema, callback, options) {
1548
- return { ...Visit(TypeClone.Clone(schema), callback), ...options };
1427
+ return { ...Visit(TypeClone.Type(schema), callback), ...options };
1549
1428
  }
1550
1429
  ObjectMap.Map = Map;
1551
1430
  })(ObjectMap || (exports.ObjectMap = ObjectMap = {}));
@@ -1568,15 +1447,12 @@ var KeyResolver;
1568
1447
  return options.includePatterns ? Object.getOwnPropertyNames(schema.patternProperties) : [];
1569
1448
  }
1570
1449
  function Visit(schema, options) {
1571
- if (TypeGuard.TIntersect(schema))
1572
- return TIntersect(schema, options);
1573
- if (TypeGuard.TUnion(schema))
1574
- return TUnion(schema, options);
1575
- if (TypeGuard.TObject(schema))
1576
- return TObject(schema, options);
1577
- if (TypeGuard.TRecord(schema))
1578
- return TRecord(schema, options);
1579
- return [];
1450
+ // prettier-ignore
1451
+ return (TypeGuard.TIntersect(schema) ? TIntersect(schema, options) :
1452
+ TypeGuard.TUnion(schema) ? TUnion(schema, options) :
1453
+ TypeGuard.TObject(schema) ? TObject(schema, options) :
1454
+ TypeGuard.TRecord(schema) ? TRecord(schema, options) :
1455
+ []);
1580
1456
  }
1581
1457
  /** Resolves an array of keys in this schema */
1582
1458
  function ResolveKeys(schema, options) {
@@ -1594,23 +1470,23 @@ var KeyResolver;
1594
1470
  // --------------------------------------------------------------------------
1595
1471
  // KeyArrayResolver
1596
1472
  // --------------------------------------------------------------------------
1473
+ class KeyArrayResolverError extends TypeBoxError {
1474
+ }
1475
+ exports.KeyArrayResolverError = KeyArrayResolverError;
1597
1476
  var KeyArrayResolver;
1598
1477
  (function (KeyArrayResolver) {
1599
1478
  /** Resolves an array of string[] keys from the given schema or array type. */
1600
1479
  function Resolve(schema) {
1601
- if (Array.isArray(schema))
1602
- return schema;
1603
- if (TypeGuard.TUnionLiteral(schema))
1604
- return schema.anyOf.map((schema) => schema.const.toString());
1605
- if (TypeGuard.TLiteral(schema))
1606
- return [schema.const];
1607
- if (TypeGuard.TTemplateLiteral(schema)) {
1608
- const expression = TemplateLiteralParser.ParseExact(schema.pattern);
1609
- if (!TemplateLiteralFinite.Check(expression))
1610
- throw Error('KeyArrayResolver: Cannot resolve keys from infinite template expression');
1611
- return [...TemplateLiteralGenerator.Generate(expression)];
1612
- }
1613
- return [];
1480
+ // prettier-ignore
1481
+ return Array.isArray(schema) ? schema :
1482
+ TypeGuard.TUnionLiteral(schema) ? schema.anyOf.map((schema) => schema.const.toString()) :
1483
+ TypeGuard.TLiteral(schema) ? [schema.const] :
1484
+ TypeGuard.TTemplateLiteral(schema) ? (() => {
1485
+ const expression = TemplateLiteralParser.ParseExact(schema.pattern);
1486
+ if (!TemplateLiteralFinite.Check(expression))
1487
+ throw new KeyArrayResolverError('Cannot resolve keys from infinite template expression');
1488
+ return [...TemplateLiteralGenerator.Generate(expression)];
1489
+ })() : [];
1614
1490
  }
1615
1491
  KeyArrayResolver.Resolve = Resolve;
1616
1492
  })(KeyArrayResolver || (exports.KeyArrayResolver = KeyArrayResolver = {}));
@@ -1638,42 +1514,28 @@ var UnionResolver;
1638
1514
  // --------------------------------------------------------------------------
1639
1515
  // TemplateLiteralPattern
1640
1516
  // --------------------------------------------------------------------------
1517
+ class TemplateLiteralPatternError extends TypeBoxError {
1518
+ }
1519
+ exports.TemplateLiteralPatternError = TemplateLiteralPatternError;
1641
1520
  var TemplateLiteralPattern;
1642
1521
  (function (TemplateLiteralPattern) {
1522
+ function Throw(message) {
1523
+ throw new TemplateLiteralPatternError(message);
1524
+ }
1643
1525
  function Escape(value) {
1644
1526
  return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1645
1527
  }
1646
1528
  function Visit(schema, acc) {
1647
- if (TypeGuard.TTemplateLiteral(schema)) {
1648
- return schema.pattern.slice(1, schema.pattern.length - 1);
1649
- }
1650
- else if (TypeGuard.TUnion(schema)) {
1651
- return `(${schema.anyOf.map((schema) => Visit(schema, acc)).join('|')})`;
1652
- }
1653
- else if (TypeGuard.TNumber(schema)) {
1654
- return `${acc}${exports.PatternNumber}`;
1655
- }
1656
- else if (TypeGuard.TInteger(schema)) {
1657
- return `${acc}${exports.PatternNumber}`;
1658
- }
1659
- else if (TypeGuard.TBigInt(schema)) {
1660
- return `${acc}${exports.PatternNumber}`;
1661
- }
1662
- else if (TypeGuard.TString(schema)) {
1663
- return `${acc}${exports.PatternString}`;
1664
- }
1665
- else if (TypeGuard.TLiteral(schema)) {
1666
- return `${acc}${Escape(schema.const.toString())}`;
1667
- }
1668
- else if (TypeGuard.TBoolean(schema)) {
1669
- return `${acc}${exports.PatternBoolean}`;
1670
- }
1671
- else if (TypeGuard.TNever(schema)) {
1672
- throw Error('TemplateLiteralPattern: TemplateLiteral cannot operate on types of TNever');
1673
- }
1674
- else {
1675
- throw Error(`TemplateLiteralPattern: Unexpected Kind '${schema[exports.Kind]}'`);
1676
- }
1529
+ // prettier-ignore
1530
+ return (TypeGuard.TTemplateLiteral(schema) ? schema.pattern.slice(1, schema.pattern.length - 1) :
1531
+ TypeGuard.TUnion(schema) ? `(${schema.anyOf.map((schema) => Visit(schema, acc)).join('|')})` :
1532
+ TypeGuard.TNumber(schema) ? `${acc}${exports.PatternNumber}` :
1533
+ TypeGuard.TInteger(schema) ? `${acc}${exports.PatternNumber}` :
1534
+ TypeGuard.TBigInt(schema) ? `${acc}${exports.PatternNumber}` :
1535
+ TypeGuard.TString(schema) ? `${acc}${exports.PatternString}` :
1536
+ TypeGuard.TLiteral(schema) ? `${acc}${Escape(schema.const.toString())}` :
1537
+ TypeGuard.TBoolean(schema) ? `${acc}${exports.PatternBoolean}` :
1538
+ Throw(`Unexpected Kind '${schema[exports.Kind]}'`));
1677
1539
  }
1678
1540
  function Create(kinds) {
1679
1541
  return `^${kinds.map((schema) => Visit(schema, '')).join('')}\$`;
@@ -1698,10 +1560,7 @@ var TemplateLiteralResolver;
1698
1560
  // --------------------------------------------------------------------------------------
1699
1561
  // TemplateLiteralParser
1700
1562
  // --------------------------------------------------------------------------------------
1701
- class TemplateLiteralParserError extends Error {
1702
- constructor(message) {
1703
- super(message);
1704
- }
1563
+ class TemplateLiteralParserError extends TypeBoxError {
1705
1564
  }
1706
1565
  exports.TemplateLiteralParserError = TemplateLiteralParserError;
1707
1566
  var TemplateLiteralParser;
@@ -1816,21 +1675,18 @@ var TemplateLiteralParser;
1816
1675
  index = end - 1;
1817
1676
  }
1818
1677
  }
1819
- if (expressions.length === 0)
1820
- return { type: 'const', const: '' };
1821
- if (expressions.length === 1)
1822
- return expressions[0];
1823
- return { type: 'and', expr: expressions };
1678
+ // prettier-ignore
1679
+ return (expressions.length === 0) ? { type: 'const', const: '' } :
1680
+ (expressions.length === 1) ? expressions[0] :
1681
+ { type: 'and', expr: expressions };
1824
1682
  }
1825
1683
  /** Parses a pattern and returns an expression tree */
1826
1684
  function Parse(pattern) {
1827
- if (IsGroup(pattern))
1828
- return Parse(InGroup(pattern));
1829
- if (IsPrecedenceOr(pattern))
1830
- return Or(pattern);
1831
- if (IsPrecedenceAnd(pattern))
1832
- return And(pattern);
1833
- return { type: 'const', const: pattern };
1685
+ // prettier-ignore
1686
+ return IsGroup(pattern) ? Parse(InGroup(pattern)) :
1687
+ IsPrecedenceOr(pattern) ? Or(pattern) :
1688
+ IsPrecedenceAnd(pattern) ? And(pattern) :
1689
+ { type: 'const', const: pattern };
1834
1690
  }
1835
1691
  TemplateLiteralParser.Parse = Parse;
1836
1692
  /** Parses a pattern and strips forward and trailing ^ and $ */
@@ -1842,8 +1698,14 @@ var TemplateLiteralParser;
1842
1698
  // --------------------------------------------------------------------------------------
1843
1699
  // TemplateLiteralFinite
1844
1700
  // --------------------------------------------------------------------------------------
1701
+ class TemplateLiteralFiniteError extends TypeBoxError {
1702
+ }
1703
+ exports.TemplateLiteralFiniteError = TemplateLiteralFiniteError;
1845
1704
  var TemplateLiteralFinite;
1846
1705
  (function (TemplateLiteralFinite) {
1706
+ function Throw(message) {
1707
+ throw new TemplateLiteralFiniteError(message);
1708
+ }
1847
1709
  function IsNumber(expression) {
1848
1710
  // prettier-ignore
1849
1711
  return (expression.type === 'or' &&
@@ -1866,23 +1728,22 @@ var TemplateLiteralFinite;
1866
1728
  return expression.type === 'const' && expression.const === '.*';
1867
1729
  }
1868
1730
  function Check(expression) {
1869
- if (IsBoolean(expression))
1870
- return true;
1871
- if (IsNumber(expression) || IsString(expression))
1872
- return false;
1873
- if (expression.type === 'and')
1874
- return expression.expr.every((expr) => Check(expr));
1875
- if (expression.type === 'or')
1876
- return expression.expr.every((expr) => Check(expr));
1877
- if (expression.type === 'const')
1878
- return true;
1879
- throw Error(`TemplateLiteralFinite: Unknown expression type`);
1731
+ // prettier-ignore
1732
+ return IsBoolean(expression) ? true :
1733
+ IsNumber(expression) || IsString(expression) ? false :
1734
+ (expression.type === 'and') ? expression.expr.every((expr) => Check(expr)) :
1735
+ (expression.type === 'or') ? expression.expr.every((expr) => Check(expr)) :
1736
+ (expression.type === 'const') ? true :
1737
+ Throw(`Unknown expression type`);
1880
1738
  }
1881
1739
  TemplateLiteralFinite.Check = Check;
1882
1740
  })(TemplateLiteralFinite || (exports.TemplateLiteralFinite = TemplateLiteralFinite = {}));
1883
1741
  // --------------------------------------------------------------------------------------
1884
1742
  // TemplateLiteralGenerator
1885
1743
  // --------------------------------------------------------------------------------------
1744
+ class TemplateLiteralGeneratorError extends TypeBoxError {
1745
+ }
1746
+ exports.TemplateLiteralGeneratorError = TemplateLiteralGeneratorError;
1886
1747
  var TemplateLiteralGenerator;
1887
1748
  (function (TemplateLiteralGenerator) {
1888
1749
  function* Reduce(buffer) {
@@ -1905,13 +1766,11 @@ var TemplateLiteralGenerator;
1905
1766
  return yield expression.const;
1906
1767
  }
1907
1768
  function* Generate(expression) {
1908
- if (expression.type === 'and')
1909
- return yield* And(expression);
1910
- if (expression.type === 'or')
1911
- return yield* Or(expression);
1912
- if (expression.type === 'const')
1913
- return yield* Const(expression);
1914
- throw Error('TemplateLiteralGenerator: Unknown expression');
1769
+ // prettier-ignore
1770
+ return (expression.type === 'and' ? yield* And(expression) :
1771
+ expression.type === 'or' ? yield* Or(expression) :
1772
+ expression.type === 'const' ? yield* Const(expression) :
1773
+ (() => { throw new TemplateLiteralGeneratorError('Unknown expression'); })());
1915
1774
  }
1916
1775
  TemplateLiteralGenerator.Generate = Generate;
1917
1776
  })(TemplateLiteralGenerator || (exports.TemplateLiteralGenerator = TemplateLiteralGenerator = {}));
@@ -1922,16 +1781,17 @@ var TemplateLiteralDslParser;
1922
1781
  (function (TemplateLiteralDslParser) {
1923
1782
  function* ParseUnion(template) {
1924
1783
  const trim = template.trim().replace(/"|'/g, '');
1925
- if (trim === 'boolean')
1926
- return yield exports.Type.Boolean();
1927
- if (trim === 'number')
1928
- return yield exports.Type.Number();
1929
- if (trim === 'bigint')
1930
- return yield exports.Type.BigInt();
1931
- if (trim === 'string')
1932
- return yield exports.Type.String();
1933
- const literals = trim.split('|').map((literal) => exports.Type.Literal(literal.trim()));
1934
- return yield literals.length === 0 ? exports.Type.Never() : literals.length === 1 ? literals[0] : exports.Type.Union(literals);
1784
+ // prettier-ignore
1785
+ return (trim === 'boolean' ? yield exports.Type.Boolean() :
1786
+ trim === 'number' ? yield exports.Type.Number() :
1787
+ trim === 'bigint' ? yield exports.Type.BigInt() :
1788
+ trim === 'string' ? yield exports.Type.String() :
1789
+ yield (() => {
1790
+ const literals = trim.split('|').map((literal) => exports.Type.Literal(literal.trim()));
1791
+ return (literals.length === 0 ? exports.Type.Never() :
1792
+ literals.length === 1 ? literals[0] :
1793
+ exports.Type.Union(literals));
1794
+ })());
1935
1795
  }
1936
1796
  function* ParseTerminal(template) {
1937
1797
  if (template[1] !== '{') {
@@ -1963,6 +1823,38 @@ var TemplateLiteralDslParser;
1963
1823
  }
1964
1824
  TemplateLiteralDslParser.Parse = Parse;
1965
1825
  })(TemplateLiteralDslParser || (exports.TemplateLiteralDslParser = TemplateLiteralDslParser = {}));
1826
+ // ---------------------------------------------------------------------
1827
+ // TransformBuilder
1828
+ // ---------------------------------------------------------------------
1829
+ class TransformDecodeBuilder {
1830
+ constructor(schema) {
1831
+ this.schema = schema;
1832
+ }
1833
+ Decode(decode) {
1834
+ return new TransformEncodeBuilder(this.schema, decode);
1835
+ }
1836
+ }
1837
+ exports.TransformDecodeBuilder = TransformDecodeBuilder;
1838
+ class TransformEncodeBuilder {
1839
+ constructor(schema, decode) {
1840
+ this.schema = schema;
1841
+ this.decode = decode;
1842
+ }
1843
+ Encode(encode) {
1844
+ const schema = TypeClone.Type(this.schema);
1845
+ // prettier-ignore
1846
+ return (TypeGuard.TTransform(schema) ? (() => {
1847
+ const Encode = (value) => schema[exports.Transform].Encode(encode(value));
1848
+ const Decode = (value) => this.decode(schema[exports.Transform].Decode(value));
1849
+ const Codec = { Encode: Encode, Decode: Decode };
1850
+ return { ...schema, [exports.Transform]: Codec };
1851
+ })() : (() => {
1852
+ const Codec = { Decode: this.decode, Encode: encode };
1853
+ return { ...schema, [exports.Transform]: Codec };
1854
+ })());
1855
+ }
1856
+ }
1857
+ exports.TransformEncodeBuilder = TransformEncodeBuilder;
1966
1858
  // --------------------------------------------------------------------------
1967
1859
  // TypeOrdinal: Used for auto $id generation
1968
1860
  // --------------------------------------------------------------------------
@@ -1970,218 +1862,221 @@ let TypeOrdinal = 0;
1970
1862
  // --------------------------------------------------------------------------
1971
1863
  // TypeBuilder
1972
1864
  // --------------------------------------------------------------------------
1865
+ class TypeBuilderError extends TypeBoxError {
1866
+ }
1867
+ exports.TypeBuilderError = TypeBuilderError;
1973
1868
  class TypeBuilder {
1974
- /** `[Utility]` Creates a schema without `static` and `params` types */
1869
+ /** `[Internal]` Creates a schema without `static` and `params` types */
1975
1870
  Create(schema) {
1976
1871
  return schema;
1977
1872
  }
1978
- /** `[Utility]` Discards a property key from the given schema */
1873
+ /** `[Internal]` Throws a TypeBuilder error with the given message */
1874
+ Throw(message) {
1875
+ throw new TypeBuilderError(message);
1876
+ }
1877
+ /** `[Internal]` Discards a property key from the given schema */
1979
1878
  Discard(schema, key) {
1980
1879
  const { [key]: _, ...rest } = schema;
1981
1880
  return rest;
1982
1881
  }
1983
- /** `[Standard]` Omits compositing symbols from this schema */
1882
+ /** `[Json]` Omits compositing symbols from this schema */
1984
1883
  Strict(schema) {
1985
1884
  return JSON.parse(JSON.stringify(schema));
1986
1885
  }
1987
1886
  }
1988
1887
  exports.TypeBuilder = TypeBuilder;
1989
1888
  // --------------------------------------------------------------------------
1990
- // StandardTypeBuilder
1889
+ // JsonTypeBuilder
1991
1890
  // --------------------------------------------------------------------------
1992
- class StandardTypeBuilder extends TypeBuilder {
1891
+ class JsonTypeBuilder extends TypeBuilder {
1993
1892
  // ------------------------------------------------------------------------
1994
1893
  // Modifiers
1995
1894
  // ------------------------------------------------------------------------
1996
- /** `[Standard]` Creates a Readonly and Optional property */
1895
+ /** `[Json]` Creates a Readonly and Optional property */
1997
1896
  ReadonlyOptional(schema) {
1998
1897
  return this.Readonly(this.Optional(schema));
1999
1898
  }
2000
- /** `[Standard]` Creates a Readonly property */
1899
+ /** `[Json]` Creates a Readonly property */
2001
1900
  Readonly(schema) {
2002
- return { ...TypeClone.Clone(schema), [exports.Readonly]: 'Readonly' };
1901
+ return { ...TypeClone.Type(schema), [exports.Readonly]: 'Readonly' };
2003
1902
  }
2004
- /** `[Standard]` Creates an Optional property */
1903
+ /** `[Json]` Creates an Optional property */
2005
1904
  Optional(schema) {
2006
- return { ...TypeClone.Clone(schema), [exports.Optional]: 'Optional' };
1905
+ return { ...TypeClone.Type(schema), [exports.Optional]: 'Optional' };
2007
1906
  }
2008
1907
  // ------------------------------------------------------------------------
2009
1908
  // Types
2010
1909
  // ------------------------------------------------------------------------
2011
- /** `[Standard]` Creates an Any type */
1910
+ /** `[Json]` Creates an Any type */
2012
1911
  Any(options = {}) {
2013
1912
  return this.Create({ ...options, [exports.Kind]: 'Any' });
2014
1913
  }
2015
- /** `[Standard]` Creates an Array type */
1914
+ /** `[Json]` Creates an Array type */
2016
1915
  Array(schema, options = {}) {
2017
- return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Clone(schema) });
1916
+ return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Type(schema) });
2018
1917
  }
2019
- /** `[Standard]` Creates a Boolean type */
1918
+ /** `[Json]` Creates a Boolean type */
2020
1919
  Boolean(options = {}) {
2021
1920
  return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
2022
1921
  }
2023
- /** `[Standard]` Intrinsic function to Capitalize LiteralString types */
1922
+ /** `[Json]` Intrinsic function to Capitalize LiteralString types */
2024
1923
  Capitalize(schema, options = {}) {
2025
- return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Capitalize'), ...options };
1924
+ return { ...Intrinsic.Map(TypeClone.Type(schema), 'Capitalize'), ...options };
2026
1925
  }
2027
- /** `[Standard]` Creates a Composite object type */
1926
+ /** `[Json]` Creates a Composite object type */
2028
1927
  Composite(objects, options) {
2029
1928
  const intersect = exports.Type.Intersect(objects, {});
2030
1929
  const keys = KeyResolver.ResolveKeys(intersect, { includePatterns: false });
2031
1930
  const properties = keys.reduce((acc, key) => ({ ...acc, [key]: exports.Type.Index(intersect, [key]) }), {});
2032
1931
  return exports.Type.Object(properties, options);
2033
1932
  }
2034
- /** `[Standard]` Creates a Enum type */
1933
+ /** `[Json]` Creates a Enum type */
2035
1934
  Enum(item, options = {}) {
2036
1935
  // prettier-ignore
2037
1936
  const values = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
2038
- const anyOf = values.map((value) => (ValueGuard.IsString(value) ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
1937
+ // prettier-ignore
1938
+ const anyOf = values.map((value) => ValueGuard.IsString(value)
1939
+ ? { [exports.Kind]: 'Literal', type: 'string', const: value }
1940
+ : { [exports.Kind]: 'Literal', type: 'number', const: value });
2039
1941
  return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
2040
1942
  }
2041
- /** `[Standard]` Creates a Conditional type */
1943
+ /** `[Json]` Creates a Conditional type */
2042
1944
  Extends(left, right, trueType, falseType, options = {}) {
2043
1945
  switch (TypeExtends.Extends(left, right)) {
2044
1946
  case TypeExtendsResult.Union:
2045
- return this.Union([TypeClone.Clone(trueType, options), TypeClone.Clone(falseType, options)]);
1947
+ return this.Union([TypeClone.Type(trueType, options), TypeClone.Type(falseType, options)]);
2046
1948
  case TypeExtendsResult.True:
2047
- return TypeClone.Clone(trueType, options);
1949
+ return TypeClone.Type(trueType, options);
2048
1950
  case TypeExtendsResult.False:
2049
- return TypeClone.Clone(falseType, options);
1951
+ return TypeClone.Type(falseType, options);
2050
1952
  }
2051
1953
  }
2052
- /** `[Standard]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
1954
+ /** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
2053
1955
  Exclude(unionType, excludedMembers, options = {}) {
2054
- if (TypeGuard.TTemplateLiteral(unionType))
2055
- return this.Exclude(TemplateLiteralResolver.Resolve(unionType), excludedMembers, options);
2056
- if (TypeGuard.TTemplateLiteral(excludedMembers))
2057
- return this.Exclude(unionType, TemplateLiteralResolver.Resolve(excludedMembers), options);
2058
- if (TypeGuard.TUnion(unionType)) {
2059
- const narrowed = unionType.anyOf.filter((inner) => TypeExtends.Extends(inner, excludedMembers) === TypeExtendsResult.False);
2060
- return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
2061
- }
2062
- else {
2063
- return (TypeExtends.Extends(unionType, excludedMembers) !== TypeExtendsResult.False ? this.Never(options) : TypeClone.Clone(unionType, options));
2064
- }
2065
- }
2066
- /** `[Standard]` Constructs a type by extracting from type all union members that are assignable to union */
1956
+ // prettier-ignore
1957
+ return (TypeGuard.TTemplateLiteral(unionType) ? this.Exclude(TemplateLiteralResolver.Resolve(unionType), excludedMembers, options) :
1958
+ TypeGuard.TTemplateLiteral(excludedMembers) ? this.Exclude(unionType, TemplateLiteralResolver.Resolve(excludedMembers), options) :
1959
+ TypeGuard.TUnion(unionType) ? (() => {
1960
+ const narrowed = unionType.anyOf.filter((inner) => TypeExtends.Extends(inner, excludedMembers) === TypeExtendsResult.False);
1961
+ return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options));
1962
+ })() :
1963
+ TypeExtends.Extends(unionType, excludedMembers) !== TypeExtendsResult.False ? this.Never(options) :
1964
+ TypeClone.Type(unionType, options));
1965
+ }
1966
+ /** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
2067
1967
  Extract(type, union, options = {}) {
2068
- if (TypeGuard.TTemplateLiteral(type))
2069
- return this.Extract(TemplateLiteralResolver.Resolve(type), union, options);
2070
- if (TypeGuard.TTemplateLiteral(union))
2071
- return this.Extract(type, TemplateLiteralResolver.Resolve(union), options);
2072
- if (TypeGuard.TUnion(type)) {
2073
- const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False);
2074
- return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
2075
- }
2076
- else {
2077
- return (TypeExtends.Extends(type, union) !== TypeExtendsResult.False ? TypeClone.Clone(type, options) : this.Never(options));
2078
- }
2079
- }
2080
- /** `[Standard]` Returns an Indexed property type for the given keys */
1968
+ // prettier-ignore
1969
+ return (TypeGuard.TTemplateLiteral(type) ? this.Extract(TemplateLiteralResolver.Resolve(type), union, options) :
1970
+ TypeGuard.TTemplateLiteral(union) ? this.Extract(type, TemplateLiteralResolver.Resolve(union), options) :
1971
+ TypeGuard.TUnion(type) ? (() => {
1972
+ const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False);
1973
+ return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options));
1974
+ })() :
1975
+ TypeExtends.Extends(type, union) !== TypeExtendsResult.False ? TypeClone.Type(type, options) :
1976
+ this.Never(options));
1977
+ }
1978
+ /** `[Json]` Returns an Indexed property type for the given keys */
2081
1979
  Index(schema, unresolved, options = {}) {
2082
- if (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved)) {
2083
- return TypeClone.Clone(schema.items, options);
2084
- }
2085
- else if (TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved)) {
2086
- const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
2087
- const cloned = items.map((schema) => TypeClone.Clone(schema));
2088
- return this.Union(cloned, options);
2089
- }
2090
- else {
2091
- const keys = KeyArrayResolver.Resolve(unresolved);
2092
- const clone = TypeClone.Clone(schema);
2093
- return IndexedAccessor.Resolve(clone, keys, options);
2094
- }
2095
- }
2096
- /** `[Standard]` Creates an Integer type */
1980
+ // prettier-ignore
1981
+ return (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved) ? (() => {
1982
+ return TypeClone.Type(schema.items, options);
1983
+ })() :
1984
+ TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved) ? (() => {
1985
+ const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
1986
+ const cloned = items.map((schema) => TypeClone.Type(schema));
1987
+ return this.Union(cloned, options);
1988
+ })() : (() => {
1989
+ const keys = KeyArrayResolver.Resolve(unresolved);
1990
+ const clone = TypeClone.Type(schema);
1991
+ return IndexedAccessor.Resolve(clone, keys, options);
1992
+ })());
1993
+ }
1994
+ /** `[Json]` Creates an Integer type */
2097
1995
  Integer(options = {}) {
2098
1996
  return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
2099
1997
  }
2100
- /** `[Standard]` Creates an Intersect type */
1998
+ /** `[Json]` Creates an Intersect type */
2101
1999
  Intersect(allOf, options = {}) {
2102
2000
  if (allOf.length === 0)
2103
2001
  return exports.Type.Never();
2104
2002
  if (allOf.length === 1)
2105
- return TypeClone.Clone(allOf[0], options);
2003
+ return TypeClone.Type(allOf[0], options);
2004
+ if (allOf.some((schema) => TypeGuard.TTransform(schema)))
2005
+ this.Throw('Cannot intersect transform types');
2106
2006
  const objects = allOf.every((schema) => TypeGuard.TObject(schema));
2107
- const cloned = allOf.map((schema) => TypeClone.Clone(schema));
2108
- const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties) ? { unevaluatedProperties: TypeClone.Clone(options.unevaluatedProperties) } : {};
2109
- if (options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects) {
2110
- return this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned });
2111
- }
2112
- else {
2113
- return this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', allOf: cloned });
2114
- }
2115
- }
2116
- /** `[Standard]` Creates a KeyOf type */
2007
+ const cloned = TypeClone.Rest(allOf);
2008
+ // prettier-ignore
2009
+ const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties)
2010
+ ? { unevaluatedProperties: TypeClone.Type(options.unevaluatedProperties) }
2011
+ : {};
2012
+ return options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects
2013
+ ? this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned })
2014
+ : this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', allOf: cloned });
2015
+ }
2016
+ /** `[Json]` Creates a KeyOf type */
2117
2017
  KeyOf(schema, options = {}) {
2118
- if (TypeGuard.TRecord(schema)) {
2018
+ // prettier-ignore
2019
+ return (TypeGuard.TRecord(schema) ? (() => {
2119
2020
  const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
2120
- if (pattern === exports.PatternNumberExact)
2121
- return this.Number(options);
2122
- if (pattern === exports.PatternStringExact)
2123
- return this.String(options);
2124
- throw Error('StandardTypeBuilder: Unable to resolve key type from Record key pattern');
2125
- }
2126
- else if (TypeGuard.TTuple(schema)) {
2127
- const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
2128
- const literals = items.map((_, index) => exports.Type.Literal(index));
2129
- return this.Union(literals, options);
2130
- }
2131
- else if (TypeGuard.TArray(schema)) {
2132
- return this.Number(options);
2133
- }
2134
- else {
2135
- const keys = KeyResolver.ResolveKeys(schema, { includePatterns: false });
2136
- if (keys.length === 0)
2137
- return this.Never(options);
2138
- const literals = keys.map((key) => this.Literal(key));
2139
- return this.Union(literals, options);
2140
- }
2141
- }
2142
- /** `[Standard]` Creates a Literal type */
2021
+ return (pattern === exports.PatternNumberExact ? this.Number(options) :
2022
+ pattern === exports.PatternStringExact ? this.String(options) :
2023
+ this.Throw('Unable to resolve key type from Record key pattern'));
2024
+ })() :
2025
+ TypeGuard.TTuple(schema) ? (() => {
2026
+ const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
2027
+ const literals = items.map((_, index) => exports.Type.Literal(index.toString()));
2028
+ return this.Union(literals, options);
2029
+ })() :
2030
+ TypeGuard.TArray(schema) ? (() => {
2031
+ return this.Number(options);
2032
+ })() : (() => {
2033
+ const keys = KeyResolver.ResolveKeys(schema, { includePatterns: false });
2034
+ if (keys.length === 0)
2035
+ return this.Never(options);
2036
+ const literals = keys.map((key) => this.Literal(key));
2037
+ return this.Union(literals, options);
2038
+ })());
2039
+ }
2040
+ /** `[Json]` Creates a Literal type */
2143
2041
  Literal(value, options = {}) {
2144
2042
  return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
2145
2043
  }
2146
- /** `[Standard]` Intrinsic function to Lowercase LiteralString types */
2044
+ /** `[Json]` Intrinsic function to Lowercase LiteralString types */
2147
2045
  Lowercase(schema, options = {}) {
2148
- return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Lowercase'), ...options };
2046
+ return { ...Intrinsic.Map(TypeClone.Type(schema), 'Lowercase'), ...options };
2149
2047
  }
2150
- /** `[Standard]` Creates a Never type */
2048
+ /** `[Json]` Creates a Never type */
2151
2049
  Never(options = {}) {
2152
2050
  return this.Create({ ...options, [exports.Kind]: 'Never', not: {} });
2153
2051
  }
2154
- /** `[Standard]` Creates a Not type */
2052
+ /** `[Json]` Creates a Not type */
2155
2053
  Not(schema, options) {
2156
- return this.Create({ ...options, [exports.Kind]: 'Not', not: TypeClone.Clone(schema) });
2054
+ return this.Create({ ...options, [exports.Kind]: 'Not', not: TypeClone.Type(schema) });
2157
2055
  }
2158
- /** `[Standard]` Creates a Null type */
2056
+ /** `[Json]` Creates a Null type */
2159
2057
  Null(options = {}) {
2160
2058
  return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
2161
2059
  }
2162
- /** `[Standard]` Creates a Number type */
2060
+ /** `[Json]` Creates a Number type */
2163
2061
  Number(options = {}) {
2164
2062
  return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
2165
2063
  }
2166
- /** `[Standard]` Creates an Object type */
2064
+ /** `[Json]` Creates an Object type */
2167
2065
  Object(properties, options = {}) {
2168
2066
  const propertyKeys = Object.getOwnPropertyNames(properties);
2169
2067
  const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key]));
2170
2068
  const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name));
2171
- const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Clone(options.additionalProperties) } : {};
2172
- const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(properties[key]) }), {});
2173
- if (requiredKeys.length > 0) {
2174
- return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required: requiredKeys });
2175
- }
2176
- else {
2177
- return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
2178
- }
2069
+ const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Type(options.additionalProperties) } : {};
2070
+ const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(properties[key]) }), {});
2071
+ return requiredKeys.length > 0
2072
+ ? this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required: requiredKeys })
2073
+ : this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
2179
2074
  }
2180
- /** `[Standard]` Constructs a type whose keys are omitted from the given type */
2075
+ /** `[Json]` Constructs a type whose keys are omitted from the given type */
2181
2076
  Omit(schema, unresolved, options = {}) {
2182
2077
  const keys = KeyArrayResolver.Resolve(unresolved);
2183
2078
  // prettier-ignore
2184
- return ObjectMap.Map(TypeClone.Clone(schema), (object) => {
2079
+ return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
2185
2080
  if (ValueGuard.IsArray(object.required)) {
2186
2081
  object.required = object.required.filter((key) => !keys.includes(key));
2187
2082
  if (object.required.length === 0)
@@ -2194,21 +2089,21 @@ class StandardTypeBuilder extends TypeBuilder {
2194
2089
  return this.Create(object);
2195
2090
  }, options);
2196
2091
  }
2197
- /** `[Standard]` Constructs a type where all properties are optional */
2092
+ /** `[Json]` Constructs a type where all properties are optional */
2198
2093
  Partial(schema, options = {}) {
2199
2094
  // prettier-ignore
2200
- return ObjectMap.Map(schema, (object) => {
2095
+ return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
2201
2096
  const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
2202
2097
  return { ...acc, [key]: this.Optional(object.properties[key]) };
2203
2098
  }, {});
2204
2099
  return this.Object(properties, this.Discard(object, 'required') /* object used as options to retain other constraints */);
2205
2100
  }, options);
2206
2101
  }
2207
- /** `[Standard]` Constructs a type whose keys are picked from the given type */
2102
+ /** `[Json]` Constructs a type whose keys are picked from the given type */
2208
2103
  Pick(schema, unresolved, options = {}) {
2209
2104
  const keys = KeyArrayResolver.Resolve(unresolved);
2210
2105
  // prettier-ignore
2211
- return ObjectMap.Map(TypeClone.Clone(schema), (object) => {
2106
+ return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
2212
2107
  if (ValueGuard.IsArray(object.required)) {
2213
2108
  object.required = object.required.filter((key) => keys.includes(key));
2214
2109
  if (object.required.length === 0)
@@ -2221,43 +2116,41 @@ class StandardTypeBuilder extends TypeBuilder {
2221
2116
  return this.Create(object);
2222
2117
  }, options);
2223
2118
  }
2224
- /** `[Standard]` Creates a Record type */
2119
+ /** `[Json]` Creates a Record type */
2225
2120
  Record(key, schema, options = {}) {
2226
- if (TypeGuard.TTemplateLiteral(key)) {
2121
+ // prettier-ignore
2122
+ return (TypeGuard.TTemplateLiteral(key) ? (() => {
2227
2123
  const expression = TemplateLiteralParser.ParseExact(key.pattern);
2228
2124
  // prettier-ignore
2229
2125
  return TemplateLiteralFinite.Check(expression)
2230
- ? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(schema) }), {}), options))
2231
- : this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema) } });
2232
- }
2233
- else if (TypeGuard.TUnion(key)) {
2234
- const union = UnionResolver.Resolve(key);
2235
- if (TypeGuard.TUnionLiteral(union)) {
2236
- const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema) }), {});
2237
- return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
2238
- }
2239
- else
2240
- throw Error('StandardTypeBuilder: Record key of type union contains non-literal types');
2241
- }
2242
- else if (TypeGuard.TLiteral(key)) {
2243
- if (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const)) {
2244
- return this.Object({ [key.const]: TypeClone.Clone(schema) }, options);
2245
- }
2246
- else
2247
- throw Error('StandardTypeBuilder: Record key of type literal is not of type string or number');
2248
- }
2249
- else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
2250
- return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [exports.PatternNumberExact]: TypeClone.Clone(schema) } });
2251
- }
2252
- else if (TypeGuard.TString(key)) {
2253
- const pattern = ValueGuard.IsUndefined(key.pattern) ? exports.PatternStringExact : key.pattern;
2254
- return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema) } });
2255
- }
2256
- else {
2257
- throw Error(`StandardTypeBuilder: Record key is an invalid type`);
2258
- }
2259
- }
2260
- /** `[Standard]` Creates a Recursive type */
2126
+ ? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(schema) }), {}), options))
2127
+ : this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Type(schema) } });
2128
+ })() :
2129
+ TypeGuard.TUnion(key) ? (() => {
2130
+ const union = UnionResolver.Resolve(key);
2131
+ if (TypeGuard.TUnionLiteral(union)) {
2132
+ const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Type(schema) }), {});
2133
+ return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
2134
+ }
2135
+ else
2136
+ this.Throw('Record key of type union contains non-literal types');
2137
+ })() :
2138
+ TypeGuard.TLiteral(key) ? (() => {
2139
+ // prettier-ignore
2140
+ return (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const))
2141
+ ? this.Object({ [key.const]: TypeClone.Type(schema) }, options)
2142
+ : this.Throw('Record key of type literal is not of type string or number');
2143
+ })() :
2144
+ TypeGuard.TInteger(key) || TypeGuard.TNumber(key) ? (() => {
2145
+ return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [exports.PatternNumberExact]: TypeClone.Type(schema) } });
2146
+ })() :
2147
+ TypeGuard.TString(key) ? (() => {
2148
+ const pattern = ValueGuard.IsUndefined(key.pattern) ? exports.PatternStringExact : key.pattern;
2149
+ return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Type(schema) } });
2150
+ })() :
2151
+ this.Throw(`Record key is an invalid type`));
2152
+ }
2153
+ /** `[Json]` Creates a Recursive type */
2261
2154
  Recursive(callback, options = {}) {
2262
2155
  if (ValueGuard.IsUndefined(options.$id))
2263
2156
  options.$id = `T${TypeOrdinal++}`;
@@ -2265,151 +2158,145 @@ class StandardTypeBuilder extends TypeBuilder {
2265
2158
  thisType.$id = options.$id;
2266
2159
  return this.Create({ ...options, [exports.Hint]: 'Recursive', ...thisType });
2267
2160
  }
2268
- /** `[Standard]` Creates a Ref type. */
2161
+ /** `[Json]` Creates a Ref type. */
2269
2162
  Ref(unresolved, options = {}) {
2270
2163
  if (ValueGuard.IsString(unresolved))
2271
2164
  return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved });
2272
2165
  if (ValueGuard.IsUndefined(unresolved.$id))
2273
- throw Error('StandardTypeBuilder.Ref: Target type must specify an $id');
2166
+ this.Throw('Reference target type must specify an $id');
2274
2167
  return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved.$id });
2275
2168
  }
2276
- /** `[Standard]` Constructs a type where all properties are required */
2169
+ /** `[Json]` Constructs a type where all properties are required */
2277
2170
  Required(schema, options = {}) {
2278
2171
  // prettier-ignore
2279
- return ObjectMap.Map(schema, (object) => {
2172
+ return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
2280
2173
  const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
2281
2174
  return { ...acc, [key]: this.Discard(object.properties[key], exports.Optional) };
2282
2175
  }, {});
2283
2176
  return this.Object(properties, object /* object used as options to retain other constraints */);
2284
2177
  }, options);
2285
2178
  }
2286
- /** `[Standard]` Extracts the rest array from a Tuple */
2179
+ /** `[Json]` Extracts interior Rest elements from Tuple, Intersect and Union types */
2287
2180
  Rest(schema) {
2288
- if (TypeGuard.TTuple(schema)) {
2289
- if (ValueGuard.IsUndefined(schema.items))
2290
- return [];
2291
- return schema.items.map((schema) => TypeClone.Clone(schema));
2292
- }
2293
- else {
2294
- return [TypeClone.Clone(schema)];
2295
- }
2181
+ return (TypeGuard.TTuple(schema) && !ValueGuard.IsUndefined(schema.items) ? TypeClone.Rest(schema.items) : TypeGuard.TIntersect(schema) ? TypeClone.Rest(schema.allOf) : TypeGuard.TUnion(schema) ? TypeClone.Rest(schema.anyOf) : []);
2296
2182
  }
2297
- /** `[Standard]` Creates a String type */
2183
+ /** `[Json]` Creates a String type */
2298
2184
  String(options = {}) {
2299
2185
  return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
2300
2186
  }
2301
- /** `[Standard]` Creates a TemplateLiteral type */
2187
+ /** `[Json]` Creates a TemplateLiteral type */
2302
2188
  TemplateLiteral(unresolved, options = {}) {
2303
2189
  // prettier-ignore
2304
- const pattern = (ValueGuard.IsString(unresolved))
2190
+ const pattern = ValueGuard.IsString(unresolved)
2305
2191
  ? TemplateLiteralPattern.Create(TemplateLiteralDslParser.Parse(unresolved))
2306
2192
  : TemplateLiteralPattern.Create(unresolved);
2307
2193
  return this.Create({ ...options, [exports.Kind]: 'TemplateLiteral', type: 'string', pattern });
2308
2194
  }
2309
- /** `[Standard]` Creates a Tuple type */
2195
+ /** `[Json]` Creates a Transform type */
2196
+ Transform(schema) {
2197
+ return new TransformDecodeBuilder(schema);
2198
+ }
2199
+ /** `[Json]` Creates a Tuple type */
2310
2200
  Tuple(items, options = {}) {
2311
2201
  const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
2312
- const clonedItems = items.map((item) => TypeClone.Clone(item));
2202
+ const clonedItems = TypeClone.Rest(items);
2313
2203
  // prettier-ignore
2314
2204
  const schema = (items.length > 0 ?
2315
2205
  { ...options, [exports.Kind]: 'Tuple', type: 'array', items: clonedItems, additionalItems, minItems, maxItems } :
2316
2206
  { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
2317
2207
  return this.Create(schema);
2318
2208
  }
2319
- /** `[Standard]` Intrinsic function to Uncapitalize LiteralString types */
2209
+ /** `[Json]` Intrinsic function to Uncapitalize LiteralString types */
2320
2210
  Uncapitalize(schema, options = {}) {
2321
- return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Uncapitalize'), ...options };
2211
+ return { ...Intrinsic.Map(TypeClone.Type(schema), 'Uncapitalize'), ...options };
2322
2212
  }
2323
- /** `[Standard]` Creates a Union type */
2213
+ /** `[Json]` Creates a Union type */
2324
2214
  Union(union, options = {}) {
2325
- if (TypeGuard.TTemplateLiteral(union)) {
2326
- return TemplateLiteralResolver.Resolve(union);
2327
- }
2328
- else {
2329
- const anyOf = union;
2330
- if (anyOf.length === 0)
2331
- return this.Never(options);
2332
- if (anyOf.length === 1)
2333
- return this.Create(TypeClone.Clone(anyOf[0], options));
2334
- const clonedAnyOf = anyOf.map((schema) => TypeClone.Clone(schema));
2335
- return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: clonedAnyOf });
2336
- }
2337
- }
2338
- /** `[Standard]` Creates an Unknown type */
2215
+ // prettier-ignore
2216
+ return TypeGuard.TTemplateLiteral(union)
2217
+ ? TemplateLiteralResolver.Resolve(union)
2218
+ : (() => {
2219
+ const anyOf = union;
2220
+ if (anyOf.length === 0)
2221
+ return this.Never(options);
2222
+ if (anyOf.length === 1)
2223
+ return this.Create(TypeClone.Type(anyOf[0], options));
2224
+ const clonedAnyOf = TypeClone.Rest(anyOf);
2225
+ return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: clonedAnyOf });
2226
+ })();
2227
+ }
2228
+ /** `[Json]` Creates an Unknown type */
2339
2229
  Unknown(options = {}) {
2340
2230
  return this.Create({ ...options, [exports.Kind]: 'Unknown' });
2341
2231
  }
2342
- /** `[Standard]` Creates a Unsafe type that will infers as the generic argument T */
2232
+ /** `[Json]` Creates a Unsafe type that will infers as the generic argument T */
2343
2233
  Unsafe(options = {}) {
2344
2234
  return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
2345
2235
  }
2346
- /** `[Standard]` Intrinsic function to Uppercase LiteralString types */
2236
+ /** `[Json]` Intrinsic function to Uppercase LiteralString types */
2347
2237
  Uppercase(schema, options = {}) {
2348
- return { ...Intrinsic.Map(TypeClone.Clone(schema), 'Uppercase'), ...options };
2238
+ return { ...Intrinsic.Map(TypeClone.Type(schema), 'Uppercase'), ...options };
2349
2239
  }
2350
2240
  }
2351
- exports.StandardTypeBuilder = StandardTypeBuilder;
2241
+ exports.JsonTypeBuilder = JsonTypeBuilder;
2352
2242
  // --------------------------------------------------------------------------
2353
- // ExtendedTypeBuilder
2243
+ // JavaScriptTypeBuilder
2354
2244
  // --------------------------------------------------------------------------
2355
- class ExtendedTypeBuilder extends StandardTypeBuilder {
2356
- /** `[Extended]` Creates a AsyncIterator type */
2245
+ class JavaScriptTypeBuilder extends JsonTypeBuilder {
2246
+ /** `[JavaScript]` Creates a AsyncIterator type */
2357
2247
  AsyncIterator(items, options = {}) {
2358
- return this.Create({ ...options, [exports.Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Clone(items) });
2248
+ return this.Create({ ...options, [exports.Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Type(items) });
2359
2249
  }
2360
- /** `[Extended]` Constructs a type by recursively unwrapping Promise types */
2250
+ /** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
2361
2251
  Awaited(schema, options = {}) {
2362
- const AwaitedRest = (rest) => {
2363
- if (rest.length === 0)
2364
- return rest;
2252
+ // prettier-ignore
2253
+ const Unwrap = (rest) => rest.length > 0 ? (() => {
2365
2254
  const [L, ...R] = rest;
2366
- return [this.Awaited(L), ...AwaitedRest(R)];
2367
- };
2255
+ return [this.Awaited(L), ...Unwrap(R)];
2256
+ })() : rest;
2368
2257
  // prettier-ignore
2369
- return (TypeGuard.TIntersect(schema) ? exports.Type.Intersect(AwaitedRest(schema.allOf)) :
2370
- TypeGuard.TUnion(schema) ? exports.Type.Union(AwaitedRest(schema.anyOf)) :
2258
+ return (TypeGuard.TIntersect(schema) ? exports.Type.Intersect(Unwrap(schema.allOf)) :
2259
+ TypeGuard.TUnion(schema) ? exports.Type.Union(Unwrap(schema.anyOf)) :
2371
2260
  TypeGuard.TPromise(schema) ? this.Awaited(schema.item) :
2372
- TypeClone.Clone(schema, options));
2261
+ TypeClone.Type(schema, options));
2373
2262
  }
2374
- /** `[Extended]` Creates a BigInt type */
2263
+ /** `[JavaScript]` Creates a BigInt type */
2375
2264
  BigInt(options = {}) {
2376
2265
  return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'bigint' });
2377
2266
  }
2378
- /** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
2267
+ /** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
2379
2268
  ConstructorParameters(schema, options = {}) {
2380
2269
  return this.Tuple([...schema.parameters], { ...options });
2381
2270
  }
2382
- /** `[Extended]` Creates a Constructor type */
2271
+ /** `[JavaScript]` Creates a Constructor type */
2383
2272
  Constructor(parameters, returns, options) {
2384
- const clonedReturns = TypeClone.Clone(returns);
2385
- const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter));
2273
+ const [clonedParameters, clonedReturns] = [TypeClone.Rest(parameters), TypeClone.Type(returns)];
2386
2274
  return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'constructor', parameters: clonedParameters, returns: clonedReturns });
2387
2275
  }
2388
- /** `[Extended]` Creates a Date type */
2276
+ /** `[JavaScript]` Creates a Date type */
2389
2277
  Date(options = {}) {
2390
2278
  return this.Create({ ...options, [exports.Kind]: 'Date', type: 'Date' });
2391
2279
  }
2392
- /** `[Extended]` Creates a Function type */
2280
+ /** `[JavaScript]` Creates a Function type */
2393
2281
  Function(parameters, returns, options) {
2394
- const clonedReturns = TypeClone.Clone(returns, {});
2395
- const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter));
2282
+ const [clonedParameters, clonedReturns] = [TypeClone.Rest(parameters), TypeClone.Type(returns)];
2396
2283
  return this.Create({ ...options, [exports.Kind]: 'Function', type: 'function', parameters: clonedParameters, returns: clonedReturns });
2397
2284
  }
2398
- /** `[Extended]` Extracts the InstanceType from the given Constructor type */
2285
+ /** `[JavaScript]` Extracts the InstanceType from the given Constructor type */
2399
2286
  InstanceType(schema, options = {}) {
2400
- return TypeClone.Clone(schema.returns, options);
2287
+ return TypeClone.Type(schema.returns, options);
2401
2288
  }
2402
- /** `[Extended]` Creates an Iterator type */
2289
+ /** `[JavaScript]` Creates an Iterator type */
2403
2290
  Iterator(items, options = {}) {
2404
- return this.Create({ ...options, [exports.Kind]: 'Iterator', type: 'Iterator', items: TypeClone.Clone(items) });
2291
+ return this.Create({ ...options, [exports.Kind]: 'Iterator', type: 'Iterator', items: TypeClone.Type(items) });
2405
2292
  }
2406
- /** `[Extended]` Extracts the Parameters from the given Function type */
2293
+ /** `[JavaScript]` Extracts the Parameters from the given Function type */
2407
2294
  Parameters(schema, options = {}) {
2408
2295
  return this.Tuple(schema.parameters, { ...options });
2409
2296
  }
2410
- /** `[Extended]` Creates a Promise type */
2297
+ /** `[JavaScript]` Creates a Promise type */
2411
2298
  Promise(item, options = {}) {
2412
- return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'Promise', item: TypeClone.Clone(item) });
2299
+ return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'Promise', item: TypeClone.Type(item) });
2413
2300
  }
2414
2301
  /** `[Extended]` Creates a String type */
2415
2302
  RegExp(unresolved, options = {}) {
@@ -2422,29 +2309,29 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
2422
2309
  RegEx(regex, options = {}) {
2423
2310
  return this.RegExp(regex, options);
2424
2311
  }
2425
- /** `[Extended]` Extracts the ReturnType from the given Function type */
2312
+ /** `[JavaScript]` Extracts the ReturnType from the given Function type */
2426
2313
  ReturnType(schema, options = {}) {
2427
- return TypeClone.Clone(schema.returns, options);
2314
+ return TypeClone.Type(schema.returns, options);
2428
2315
  }
2429
- /** `[Extended]` Creates a Symbol type */
2316
+ /** `[JavaScript]` Creates a Symbol type */
2430
2317
  Symbol(options) {
2431
2318
  return this.Create({ ...options, [exports.Kind]: 'Symbol', type: 'symbol' });
2432
2319
  }
2433
- /** `[Extended]` Creates a Undefined type */
2320
+ /** `[JavaScript]` Creates a Undefined type */
2434
2321
  Undefined(options = {}) {
2435
2322
  return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'undefined' });
2436
2323
  }
2437
- /** `[Extended]` Creates a Uint8Array type */
2324
+ /** `[JavaScript]` Creates a Uint8Array type */
2438
2325
  Uint8Array(options = {}) {
2439
2326
  return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'Uint8Array' });
2440
2327
  }
2441
- /** `[Extended]` Creates a Void type */
2328
+ /** `[JavaScript]` Creates a Void type */
2442
2329
  Void(options = {}) {
2443
2330
  return this.Create({ ...options, [exports.Kind]: 'Void', type: 'void' });
2444
2331
  }
2445
2332
  }
2446
- exports.ExtendedTypeBuilder = ExtendedTypeBuilder;
2447
- /** JSON Schema Type Builder with Static Resolution for TypeScript */
2448
- exports.StandardType = new StandardTypeBuilder();
2449
- /** JSON Schema Type Builder with Static Resolution for TypeScript */
2450
- exports.Type = new ExtendedTypeBuilder();
2333
+ exports.JavaScriptTypeBuilder = JavaScriptTypeBuilder;
2334
+ /** Json Type Builder with Static Resolution for TypeScript */
2335
+ exports.JsonType = new JsonTypeBuilder();
2336
+ /** JavaScript Type Builder with Static Resolution for TypeScript */
2337
+ exports.Type = new JavaScriptTypeBuilder();