diff --git a/src/typebox.ts b/src/typebox.ts index fe556aacd..16673eb35 100644 --- a/src/typebox.ts +++ b/src/typebox.ts @@ -71,7 +71,7 @@ export type TOptional = T & { [Optional]: 'Optional' } // -------------------------------------------------------------------------- // Readonly Unwrap // -------------------------------------------------------------------------- -// prettier-ignore + export type ReadonlyUnwrapType = T extends TReadonly ? ReadonlyUnwrapType : T extends TOptional ? TOptional> : @@ -1174,7 +1174,7 @@ export namespace ValueGuard { // -------------------------------------------------------------------------- // TypeGuard // -------------------------------------------------------------------------- -export class TypeGuardUnknownTypeError extends TypeBoxError { } +export class TypeGuardUnknownTypeError extends TypeBoxError {} /** Provides functions to test if JavaScript values are TypeBox types */ export namespace TypeGuard { function IsPattern(value: unknown): value is string { @@ -1653,13 +1653,13 @@ export namespace TypeGuard { // -------------------------------------------------------------------------- // ExtendsUndefined // -------------------------------------------------------------------------- -/** Fast undefined check */ +/** Fast undefined check used for properties of type undefined */ export namespace ExtendsUndefined { export function TIntersect(schema: TIntersect) { - return schema.allOf.every(schema => Check(schema)) + return schema.allOf.every((schema) => Check(schema)) } export function TUnion(schema: TUnion) { - return schema.anyOf.some(schema => Check(schema)) + return schema.anyOf.some((schema) => Check(schema)) } export function TNot(schema: TNot) { return !Check(schema.not) @@ -1669,24 +1669,25 @@ export namespace ExtendsUndefined { } // prettier-ignore export function Check(schema: TSchema): boolean { - switch (true) { - case schema[Kind] === 'Intersect': return TIntersect(schema as TIntersect) - case schema[Kind] === 'Union': return TUnion(schema as TUnion) - case schema[Kind] === 'Not': return TNot(schema as TNot) - case schema[Kind] === 'Undefined': return TUndefined(schema as TUndefined) - default: return false - } + return ( + schema[Kind] === 'Intersect' ? TIntersect(schema as TIntersect) : + schema[Kind] === 'Union' ? TUnion(schema as TUnion) : + schema[Kind] === 'Not' ? TNot(schema as TNot) : + schema[Kind] === 'Undefined' ? TUndefined(schema as TUndefined) : + false + ) } } // -------------------------------------------------------------------------- // TypeExtends // -------------------------------------------------------------------------- -export class TypeExtendsError extends TypeBoxError { } +export class TypeExtendsError extends TypeBoxError {} export enum TypeExtendsResult { Union, True, False, } +// prettier-ignore export namespace TypeExtends { // -------------------------------------------------------------------------- // IntoBooleanResult @@ -1703,7 +1704,6 @@ export namespace TypeExtends { // -------------------------------------------------------------------------- // StructuralRight // -------------------------------------------------------------------------- - // prettier-ignore function IsStructuralRight(right: TSchema): boolean { return ( TypeGuard.TNever(right) || @@ -1713,16 +1713,15 @@ export namespace TypeExtends { TypeGuard.TAny(right) ) } - // prettier-ignore function StructuralRight(left: TSchema, right: TSchema) { - switch (true) { - case TypeGuard.TNever(right): return TNeverRight(left, right) - case TypeGuard.TIntersect(right): return TIntersectRight(left, right) - case TypeGuard.TUnion(right): return TUnionRight(left, right) - case TypeGuard.TUnknown(right): return TUnknownRight(left, right) - case TypeGuard.TAny(right): return TAnyRight(left, right) - default: Throw('StructuralRight') - } + return ( + TypeGuard.TNever(right) ? TNeverRight(left, right) : + TypeGuard.TIntersect(right) ? TIntersectRight(left, right) : + TypeGuard.TUnion(right) ? TUnionRight(left, right) : + TypeGuard.TUnknown(right) ? TUnknownRight(left, right) : + TypeGuard.TAny(right) ? TAnyRight(left, right) : + Throw('StructuralRight') + ) } // -------------------------------------------------------------------------- // Any @@ -1730,151 +1729,142 @@ export namespace TypeExtends { function TAnyRight(left: TSchema, right: TAny) { return TypeExtendsResult.True } - // prettier-ignore function TAny(left: TAny, right: TSchema) { - switch (true) { - case TypeGuard.TIntersect(right): return TIntersectRight(left, right) - case (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema))): return TypeExtendsResult.True - case TypeGuard.TUnion(right): return TypeExtendsResult.Union - case TypeGuard.TUnknown(right): return TypeExtendsResult.True - case TypeGuard.TAny(right): return TypeExtendsResult.True - default: return TypeExtendsResult.Union - } + return ( + TypeGuard.TIntersect(right) ? TIntersectRight(left, right) : + (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema))) ? TypeExtendsResult.True : + TypeGuard.TUnion(right) ? TypeExtendsResult.Union : + TypeGuard.TUnknown(right) ? TypeExtendsResult.True : + TypeGuard.TAny(right) ? TypeExtendsResult.True : + TypeExtendsResult.Union + ) } // -------------------------------------------------------------------------- // Array // -------------------------------------------------------------------------- - // prettier-ignore function TArrayRight(left: TSchema, right: TArray) { - switch (true) { - case TypeGuard.TUnknown(left): return TypeExtendsResult.False - case TypeGuard.TAny(left): return TypeExtendsResult.Union - case TypeGuard.TNever(left): return TypeExtendsResult.True - default: return TypeExtendsResult.False - } + return ( + TypeGuard.TUnknown(left) ? TypeExtendsResult.False : + TypeGuard.TAny(left) ? TypeExtendsResult.Union : + TypeGuard.TNever(left) ? TypeExtendsResult.True : + TypeExtendsResult.False + ) } - // prettier-ignore function TArray(left: TArray, right: TSchema) { - switch (true) { - case TypeGuard.TObject(right) && IsObjectArrayLike(right): return TypeExtendsResult.True - case IsStructuralRight(right): return StructuralRight(left, right) - case !TypeGuard.TArray(right): return TypeExtendsResult.False - default: return IntoBooleanResult(Visit(left.items, right.items)) - } + return ( + TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True : + IsStructuralRight(right) ? StructuralRight(left, right) : + !TypeGuard.TArray(right) ? TypeExtendsResult.False : + IntoBooleanResult(Visit(left.items, right.items)) + ) } // -------------------------------------------------------------------------- // AsyncIterator // -------------------------------------------------------------------------- function TAsyncIterator(left: TAsyncIterator, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - !TypeGuard.TAsyncIterator(right) ? TypeExtendsResult.False : - IntoBooleanResult(Visit(left.items, right.items)) + !TypeGuard.TAsyncIterator(right) ? TypeExtendsResult.False : + IntoBooleanResult(Visit(left.items, right.items)) ) } // -------------------------------------------------------------------------- // BigInt // -------------------------------------------------------------------------- function TBigInt(left: TBigInt, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TBigInt(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TBigInt(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Boolean // -------------------------------------------------------------------------- function TBooleanRight(left: TSchema, right: TBoolean) { - return TypeGuard.TLiteral(left) && ValueGuard.IsBoolean(left.const) ? TypeExtendsResult.True : TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False + return ( + TypeGuard.TLiteralBoolean(left) ? TypeExtendsResult.True : + TypeGuard.TBoolean(left) ? TypeExtendsResult.True : + TypeExtendsResult.False + ) } function TBoolean(left: TBoolean, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TBoolean(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TBoolean(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Constructor // -------------------------------------------------------------------------- function TConstructor(left: TConstructor, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - !TypeGuard.TConstructor(right) ? TypeExtendsResult.False : - left.parameters.length > right.parameters.length ? TypeExtendsResult.False : - (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False : - IntoBooleanResult(Visit(left.returns, right.returns)) + TypeGuard.TObject(right) ? TObjectRight(left, right) : + !TypeGuard.TConstructor(right) ? TypeExtendsResult.False : + left.parameters.length > right.parameters.length ? TypeExtendsResult.False : + (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False : + IntoBooleanResult(Visit(left.returns, right.returns)) ) } // -------------------------------------------------------------------------- // Date // -------------------------------------------------------------------------- function TDate(left: TDate, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TDate(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TDate(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Function // -------------------------------------------------------------------------- function TFunction(left: TFunction, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - !TypeGuard.TFunction(right) ? TypeExtendsResult.False : - left.parameters.length > right.parameters.length ? TypeExtendsResult.False : - (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False : - IntoBooleanResult(Visit(left.returns, right.returns)) + TypeGuard.TObject(right) ? TObjectRight(left, right) : + !TypeGuard.TFunction(right) ? TypeExtendsResult.False : + left.parameters.length > right.parameters.length ? TypeExtendsResult.False : + (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False : + IntoBooleanResult(Visit(left.returns, right.returns)) ) } // -------------------------------------------------------------------------- // Integer // -------------------------------------------------------------------------- function TIntegerRight(left: TSchema, right: TInteger) { - // prettier-ignore return ( TypeGuard.TLiteral(left) && ValueGuard.IsNumber(left.const) ? TypeExtendsResult.True : - TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } function TInteger(left: TInteger, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : - IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeExtendsResult.False + IsStructuralRight(right) ? StructuralRight(left, right) : + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Intersect // -------------------------------------------------------------------------- function TIntersectRight(left: TSchema, right: TIntersect): TypeExtendsResult { - // prettier-ignore return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False } function TIntersect(left: TIntersect, right: TSchema) { - // prettier-ignore return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False @@ -1883,28 +1873,26 @@ export namespace TypeExtends { // Iterator // -------------------------------------------------------------------------- function TIterator(left: TIterator, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - !TypeGuard.TIterator(right) ? TypeExtendsResult.False : - IntoBooleanResult(Visit(left.items, right.items)) + !TypeGuard.TIterator(right) ? TypeExtendsResult.False : + IntoBooleanResult(Visit(left.items, right.items)) ) } // -------------------------------------------------------------------------- // Literal // -------------------------------------------------------------------------- function TLiteral(left: TLiteral, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True : - IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TString(right) ? TStringRight(left, right) : - TypeGuard.TNumber(right) ? TNumberRight(left, right) : - TypeGuard.TInteger(right) ? TIntegerRight(left, right) : - TypeGuard.TBoolean(right) ? TBooleanRight(left, right) : - TypeExtendsResult.False + IsStructuralRight(right) ? StructuralRight(left, right) : + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TString(right) ? TStringRight(left, right) : + TypeGuard.TNumber(right) ? TNumberRight(left, right) : + TypeGuard.TInteger(right) ? TIntegerRight(left, right) : + TypeGuard.TBoolean(right) ? TBooleanRight(left, right) : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- @@ -1935,42 +1923,39 @@ export namespace TypeExtends { // prettier-ignore return ( TypeGuard.TNot(left) ? Visit(UnwrapTNot(left), right) : - TypeGuard.TNot(right) ? Visit(left, UnwrapTNot(right)) : - Throw('Invalid fallthrough for Not') + TypeGuard.TNot(right) ? Visit(left, UnwrapTNot(right)) : + Throw('Invalid fallthrough for Not') ) } // -------------------------------------------------------------------------- // Null // -------------------------------------------------------------------------- function TNull(left: TNull, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TNull(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TNull(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Number // -------------------------------------------------------------------------- function TNumberRight(left: TSchema, right: TNumber) { - // prettier-ignore return ( TypeGuard.TLiteralNumber(left) ? TypeExtendsResult.True : - TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } function TNumber(left: TNumber, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- @@ -1983,7 +1968,6 @@ export namespace TypeExtends { return IsObjectArrayLike(schema) } function IsObjectSymbolLike(schema: TObject) { - // prettier-ignore return IsObjectPropertyCount(schema, 0) || ( IsObjectPropertyCount(schema, 1) && 'description' in schema.properties && TypeGuard.TUnion(schema.properties.description) && schema.properties.description.anyOf.length === 2 && (( TypeGuard.TString(schema.properties.description.anyOf[0]) && @@ -2028,160 +2012,147 @@ export namespace TypeExtends { // Property // -------------------------------------------------------------------------- function Property(left: TSchema, right: TSchema) { - // prettier-ignore return ( Visit(left, right) === TypeExtendsResult.False ? TypeExtendsResult.False : - TypeGuard.TOptional(left) && !TypeGuard.TOptional(right) ? TypeExtendsResult.False : - TypeExtendsResult.True + TypeGuard.TOptional(left) && !TypeGuard.TOptional(right) ? TypeExtendsResult.False : + TypeExtendsResult.True ) } function TObjectRight(left: TSchema, right: TObject) { - // prettier-ignore return ( TypeGuard.TUnknown(left) ? TypeExtendsResult.False : - TypeGuard.TAny(left) ? TypeExtendsResult.Union : ( - TypeGuard.TNever(left) || - (TypeGuard.TLiteralString(left) && IsObjectStringLike(right)) || - (TypeGuard.TLiteralNumber(left) && IsObjectNumberLike(right)) || - (TypeGuard.TLiteralBoolean(left) && IsObjectBooleanLike(right)) || - (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) || - (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right)) || - (TypeGuard.TString(left) && IsObjectStringLike(right)) || - (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) || - (TypeGuard.TNumber(left) && IsObjectNumberLike(right)) || - (TypeGuard.TInteger(left) && IsObjectNumberLike(right)) || - (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right)) || - (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right)) || - (TypeGuard.TDate(left) && IsObjectDateLike(right)) || - (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right)) || - (TypeGuard.TFunction(left) && IsObjectFunctionLike(right)) - ) ? TypeExtendsResult.True : - (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) ? (() => { - // When expressing a Record with literal key values, the Record is converted into a Object with - // the Hint assigned as `Record`. This is used to invert the extends logic. - return right[Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False - })() : - (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) ? (() => { - return IsObjectPropertyCount(right, 0) - ? TypeExtendsResult.True - : TypeExtendsResult.False - })() : - TypeExtendsResult.False + TypeGuard.TAny(left) ? TypeExtendsResult.Union : ( + TypeGuard.TNever(left) || + (TypeGuard.TLiteralString(left) && IsObjectStringLike(right)) || + (TypeGuard.TLiteralNumber(left) && IsObjectNumberLike(right)) || + (TypeGuard.TLiteralBoolean(left) && IsObjectBooleanLike(right)) || + (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) || + (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right)) || + (TypeGuard.TString(left) && IsObjectStringLike(right)) || + (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) || + (TypeGuard.TNumber(left) && IsObjectNumberLike(right)) || + (TypeGuard.TInteger(left) && IsObjectNumberLike(right)) || + (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right)) || + (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right)) || + (TypeGuard.TDate(left) && IsObjectDateLike(right)) || + (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right)) || + (TypeGuard.TFunction(left) && IsObjectFunctionLike(right)) + ) ? TypeExtendsResult.True : + (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) ? (() => { + // When expressing a Record with literal key values, the Record is converted into a Object with + // the Hint assigned as `Record`. This is used to invert the extends logic. + return right[Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False + })() : + (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) ? (() => { + return IsObjectPropertyCount(right, 0) ? TypeExtendsResult.True : TypeExtendsResult.False + })() : + TypeExtendsResult.False ) } function TObject(left: TObject, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - !TypeGuard.TObject(right) ? TypeExtendsResult.False : - (() => { - for (const key of Object.getOwnPropertyNames(right.properties)) { - if (!(key in left.properties) && !TypeGuard.TOptional(right.properties[key])) { - return TypeExtendsResult.False - } - if (TypeGuard.TOptional(right.properties[key])) { - return TypeExtendsResult.True - } - if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) { - return TypeExtendsResult.False - } - } - return TypeExtendsResult.True - })() + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + !TypeGuard.TObject(right) ? TypeExtendsResult.False : + (() => { + for (const key of Object.getOwnPropertyNames(right.properties)) { + if (!(key in left.properties) && !TypeGuard.TOptional(right.properties[key])) { + return TypeExtendsResult.False + } + if (TypeGuard.TOptional(right.properties[key])) { + return TypeExtendsResult.True + } + if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) { + return TypeExtendsResult.False + } + } + return TypeExtendsResult.True + })() ) } // -------------------------------------------------------------------------- // Promise // -------------------------------------------------------------------------- function TPromise(left: TPromise, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) && IsObjectPromiseLike(right) ? TypeExtendsResult.True : - !TypeGuard.TPromise(right) ? TypeExtendsResult.False : - IntoBooleanResult(Visit(left.item, right.item)) + TypeGuard.TObject(right) && IsObjectPromiseLike(right) ? TypeExtendsResult.True : + !TypeGuard.TPromise(right) ? TypeExtendsResult.False : + IntoBooleanResult(Visit(left.item, right.item)) ) } // -------------------------------------------------------------------------- // Record // -------------------------------------------------------------------------- function RecordKey(schema: TRecord) { - // prettier-ignore return ( PatternNumberExact in schema.patternProperties ? Type.Number() : - PatternStringExact in schema.patternProperties ? Type.String() : - Throw('Unknown record key pattern') + PatternStringExact in schema.patternProperties ? Type.String() : + Throw('Unknown record key pattern') ) } function RecordValue(schema: TRecord) { - // prettier-ignore return ( PatternNumberExact in schema.patternProperties ? schema.patternProperties[PatternNumberExact] : - PatternStringExact in schema.patternProperties ? schema.patternProperties[PatternStringExact] : - Throw('Unable to get record value schema') + PatternStringExact in schema.patternProperties ? schema.patternProperties[PatternStringExact] : + Throw('Unable to get record value schema') ) } function TRecordRight(left: TSchema, right: TRecord) { const [Key, Value] = [RecordKey(right), RecordValue(right)] - // prettier-ignore return ( (TypeGuard.TLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True) ? TypeExtendsResult.True : - TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) : - TypeGuard.TString(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) : - TypeGuard.TArray(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) : - TypeGuard.TObject(left) ? (() => { - for (const key of Object.getOwnPropertyNames(left.properties)) { - if (Property(Value, left.properties[key]) === TypeExtendsResult.False) { - return TypeExtendsResult.False - } - } - return TypeExtendsResult.True - })() : - TypeExtendsResult.False + TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) : + TypeGuard.TString(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) : + TypeGuard.TArray(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) : + TypeGuard.TObject(left) ? (() => { + for (const key of Object.getOwnPropertyNames(left.properties)) { + if (Property(Value, left.properties[key]) === TypeExtendsResult.False) { + return TypeExtendsResult.False + } + } + return TypeExtendsResult.True + })() : + TypeExtendsResult.False ) } function TRecord(left: TRecord, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - !TypeGuard.TRecord(right) ? TypeExtendsResult.False : - Visit(RecordValue(left), RecordValue(right)) + TypeGuard.TObject(right) ? TObjectRight(left, right) : + !TypeGuard.TRecord(right) ? TypeExtendsResult.False : + Visit(RecordValue(left), RecordValue(right)) ) } // -------------------------------------------------------------------------- // String // -------------------------------------------------------------------------- function TStringRight(left: TSchema, right: TString) { - // prettier-ignore return ( TypeGuard.TLiteral(left) && ValueGuard.IsString(left.const) ? TypeExtendsResult.True : - TypeGuard.TString(left) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TString(left) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } function TString(left: TString, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TString(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TString(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Symbol // -------------------------------------------------------------------------- function TSymbol(left: TSymbol, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TSymbol(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TSymbol(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- @@ -2191,18 +2162,16 @@ export namespace TypeExtends { // TemplateLiteral types are resolved to either unions for finite expressions or string // for infinite expressions. Here we call to TemplateLiteralResolver to resolve for // either type and continue evaluating. - // prettier-ignore return ( TypeGuard.TTemplateLiteral(left) ? Visit(TemplateLiteralResolver.Resolve(left), right) : - TypeGuard.TTemplateLiteral(right) ? Visit(left, TemplateLiteralResolver.Resolve(right)) : - Throw('Invalid fallthrough for TemplateLiteral') + TypeGuard.TTemplateLiteral(right) ? Visit(left, TemplateLiteralResolver.Resolve(right)) : + Throw('Invalid fallthrough for TemplateLiteral') ) } // -------------------------------------------------------------------------- // Tuple // -------------------------------------------------------------------------- function IsArrayOfTuple(left: TTuple, right: TSchema) { - // prettier-ignore return ( TypeGuard.TArray(right) && left.items !== undefined && @@ -2210,65 +2179,59 @@ export namespace TypeExtends { ) } function TTupleRight(left: TSchema, right: TTuple) { - // prettier-ignore return ( TypeGuard.TNever(left) ? TypeExtendsResult.True : - TypeGuard.TUnknown(left) ? TypeExtendsResult.False : - TypeGuard.TAny(left) ? TypeExtendsResult.Union : - TypeExtendsResult.False + TypeGuard.TUnknown(left) ? TypeExtendsResult.False : + TypeGuard.TAny(left) ? TypeExtendsResult.Union : + TypeExtendsResult.False ) } function TTuple(left: TTuple, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True : - TypeGuard.TArray(right) && IsArrayOfTuple(left, right) ? TypeExtendsResult.True : - !TypeGuard.TTuple(right) ? TypeExtendsResult.False : - (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) || (!ValueGuard.IsUndefined(left.items) && ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.False : - (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.True : - left.items!.every((schema, index) => Visit(schema, right.items![index]) === TypeExtendsResult.True) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True : + TypeGuard.TArray(right) && IsArrayOfTuple(left, right) ? TypeExtendsResult.True : + !TypeGuard.TTuple(right) ? TypeExtendsResult.False : + (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) || (!ValueGuard.IsUndefined(left.items) && ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.False : + (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.True : + left.items!.every((schema, index) => Visit(schema, right.items![index]) === TypeExtendsResult.True) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Uint8Array // -------------------------------------------------------------------------- function TUint8Array(left: TUint8Array, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TUint8Array(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TUint8Array(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Undefined // -------------------------------------------------------------------------- function TUndefined(left: TUndefined, right: TSchema) { - // prettier-ignore return ( IsStructuralRight(right) ? StructuralRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TRecord(right) ? TRecordRight(left, right) : - TypeGuard.TVoid(right) ? VoidRight(left, right) : - TypeGuard.TUndefined(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TRecord(right) ? TRecordRight(left, right) : + TypeGuard.TVoid(right) ? VoidRight(left, right) : + TypeGuard.TUndefined(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Union // -------------------------------------------------------------------------- function TUnionRight(left: TSchema, right: TUnion): TypeExtendsResult { - // prettier-ignore return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False } function TUnion(left: TUnion, right: TSchema): TypeExtendsResult { - // prettier-ignore return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True) ? TypeExtendsResult.True : TypeExtendsResult.False @@ -2280,76 +2243,76 @@ export namespace TypeExtends { return TypeExtendsResult.True } function TUnknown(left: TUnknown, right: TSchema) { - // prettier-ignore return ( TypeGuard.TNever(right) ? TNeverRight(left, right) : - TypeGuard.TIntersect(right) ? TIntersectRight(left, right) : - TypeGuard.TUnion(right) ? TUnionRight(left, right) : - TypeGuard.TAny(right) ? TAnyRight(left, right) : - TypeGuard.TString(right) ? TStringRight(left, right) : - TypeGuard.TNumber(right) ? TNumberRight(left, right) : - TypeGuard.TInteger(right) ? TIntegerRight(left, right) : - TypeGuard.TBoolean(right) ? TBooleanRight(left, right) : - TypeGuard.TArray(right) ? TArrayRight(left, right) : - TypeGuard.TTuple(right) ? TTupleRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TUnknown(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TIntersect(right) ? TIntersectRight(left, right) : + TypeGuard.TUnion(right) ? TUnionRight(left, right) : + TypeGuard.TAny(right) ? TAnyRight(left, right) : + TypeGuard.TString(right) ? TStringRight(left, right) : + TypeGuard.TNumber(right) ? TNumberRight(left, right) : + TypeGuard.TInteger(right) ? TIntegerRight(left, right) : + TypeGuard.TBoolean(right) ? TBooleanRight(left, right) : + TypeGuard.TArray(right) ? TArrayRight(left, right) : + TypeGuard.TTuple(right) ? TTupleRight(left, right) : + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TUnknown(right) ? TypeExtendsResult.True : + TypeExtendsResult.False ) } // -------------------------------------------------------------------------- // Void // -------------------------------------------------------------------------- function VoidRight(left: TSchema, right: TVoid) { - // prettier-ignore - return TypeGuard.TUndefined(left) ? TypeExtendsResult.True : + return ( + TypeGuard.TUndefined(left) ? TypeExtendsResult.True : TypeGuard.TUndefined(left) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeExtendsResult.False + ) } function TVoid(left: TVoid, right: TSchema) { - // prettier-ignore - return TypeGuard.TIntersect(right) ? TIntersectRight(left, right) : + return ( + TypeGuard.TIntersect(right) ? TIntersectRight(left, right) : TypeGuard.TUnion(right) ? TUnionRight(left, right) : - TypeGuard.TUnknown(right) ? TUnknownRight(left, right) : - TypeGuard.TAny(right) ? TAnyRight(left, right) : - TypeGuard.TObject(right) ? TObjectRight(left, right) : - TypeGuard.TVoid(right) ? TypeExtendsResult.True : - TypeExtendsResult.False + TypeGuard.TUnknown(right) ? TUnknownRight(left, right) : + TypeGuard.TAny(right) ? TAnyRight(left, right) : + TypeGuard.TObject(right) ? TObjectRight(left, right) : + TypeGuard.TVoid(right) ? TypeExtendsResult.True : + TypeExtendsResult.False + ) } function Visit(left: TSchema, right: TSchema): TypeExtendsResult { - // prettier-ignore return ( // resolvable (TypeGuard.TTemplateLiteral(left) || TypeGuard.TTemplateLiteral(right)) ? TTemplateLiteral(left, right) : - (TypeGuard.TNot(left) || TypeGuard.TNot(right)) ? TNot(left, right) : - // standard - TypeGuard.TAny(left) ? TAny(left, right) : - TypeGuard.TArray(left) ? TArray(left, right) : - TypeGuard.TBigInt(left) ? TBigInt(left, right) : - TypeGuard.TBoolean(left) ? TBoolean(left, right) : - TypeGuard.TAsyncIterator(left) ? TAsyncIterator(left, right) : - TypeGuard.TConstructor(left) ? TConstructor(left, right) : - TypeGuard.TDate(left) ? TDate(left, right) : - TypeGuard.TFunction(left) ? TFunction(left, right) : - TypeGuard.TInteger(left) ? TInteger(left, right) : - TypeGuard.TIntersect(left) ? TIntersect(left, right) : - TypeGuard.TIterator(left) ? TIterator(left, right) : - TypeGuard.TLiteral(left) ? TLiteral(left, right) : - TypeGuard.TNever(left) ? TNever(left, right) : - TypeGuard.TNull(left) ? TNull(left, right) : - TypeGuard.TNumber(left) ? TNumber(left, right) : - TypeGuard.TObject(left) ? TObject(left, right) : - TypeGuard.TRecord(left) ? TRecord(left, right) : - TypeGuard.TString(left) ? TString(left, right) : - TypeGuard.TSymbol(left) ? TSymbol(left, right) : - TypeGuard.TTuple(left) ? TTuple(left, right) : - TypeGuard.TPromise(left) ? TPromise(left, right) : - TypeGuard.TUint8Array(left) ? TUint8Array(left, right) : - TypeGuard.TUndefined(left) ? TUndefined(left, right) : - TypeGuard.TUnion(left) ? TUnion(left, right) : - TypeGuard.TUnknown(left) ? TUnknown(left, right) : - TypeGuard.TVoid(left) ? TVoid(left, right) : - Throw(`Unknown left type operand '${left[Kind]}'`) + (TypeGuard.TNot(left) || TypeGuard.TNot(right)) ? TNot(left, right) : + // standard + TypeGuard.TAny(left) ? TAny(left, right) : + TypeGuard.TArray(left) ? TArray(left, right) : + TypeGuard.TBigInt(left) ? TBigInt(left, right) : + TypeGuard.TBoolean(left) ? TBoolean(left, right) : + TypeGuard.TAsyncIterator(left) ? TAsyncIterator(left, right) : + TypeGuard.TConstructor(left) ? TConstructor(left, right) : + TypeGuard.TDate(left) ? TDate(left, right) : + TypeGuard.TFunction(left) ? TFunction(left, right) : + TypeGuard.TInteger(left) ? TInteger(left, right) : + TypeGuard.TIntersect(left) ? TIntersect(left, right) : + TypeGuard.TIterator(left) ? TIterator(left, right) : + TypeGuard.TLiteral(left) ? TLiteral(left, right) : + TypeGuard.TNever(left) ? TNever(left, right) : + TypeGuard.TNull(left) ? TNull(left, right) : + TypeGuard.TNumber(left) ? TNumber(left, right) : + TypeGuard.TObject(left) ? TObject(left, right) : + TypeGuard.TRecord(left) ? TRecord(left, right) : + TypeGuard.TString(left) ? TString(left, right) : + TypeGuard.TSymbol(left) ? TSymbol(left, right) : + TypeGuard.TTuple(left) ? TTuple(left, right) : + TypeGuard.TPromise(left) ? TPromise(left, right) : + TypeGuard.TUint8Array(left) ? TUint8Array(left, right) : + TypeGuard.TUndefined(left) ? TUndefined(left, right) : + TypeGuard.TUnion(left) ? TUnion(left, right) : + TypeGuard.TUnknown(left) ? TUnknown(left, right) : + TypeGuard.TVoid(left) ? TVoid(left, right) : + Throw(`Unknown left type operand '${left[Kind]}'`) ) } export function Extends(left: TSchema, right: TSchema): TypeExtendsResult { @@ -2376,14 +2339,7 @@ export namespace TypeClone { return { ...clonedProperties, ...clonedSymbols } } function Visit(value: unknown): any { - // prettier-ignore - return ( - ValueGuard.IsArray(value) ? ArrayType(value) : - ValueGuard.IsDate(value) ? DateType(value) : - ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) : - ValueGuard.IsObject(value) ? ObjectType(value) : - value - ) + return ValueGuard.IsArray(value) ? ArrayType(value) : ValueGuard.IsDate(value) ? DateType(value) : ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) : ValueGuard.IsObject(value) ? ObjectType(value) : value } /** Clones a Rest */ export function Rest(schemas: [...T]): T { @@ -2415,6 +2371,7 @@ export namespace DistinctRest { // -------------------------------------------------------------------------- // Intrinsic // -------------------------------------------------------------------------- +// prettier-ignore export namespace Intrinsic { function Uncapitalize(value: string): string { const [first, rest] = [value.slice(0, 1), value.slice(1)] @@ -2443,13 +2400,15 @@ export namespace Intrinsic { return Type.TemplateLiteral([union]) } function IntrinsicLiteral(value: TLiteralValue, mode: TIntrinsicMode) { - // prettier-ignore - return typeof value === 'string' ? ( - mode === 'Uncapitalize' ? Uncapitalize(value) : + return ( + typeof value === 'string' ? ( + mode === 'Uncapitalize' ? Uncapitalize(value) : mode === 'Capitalize' ? Capitalize(value) : - mode === 'Uppercase' ? Uppercase(value) : - mode === 'Lowercase' ? Lowercase(value) : - value) : value.toString() + mode === 'Uppercase' ? Uppercase(value) : + mode === 'Lowercase' ? Lowercase(value) : + value + ) : value.toString() + ) } function IntrinsicRest(schema: TSchema[], mode: TIntrinsicMode): TSchema[] { if (schema.length === 0) return [] @@ -2457,11 +2416,12 @@ export namespace Intrinsic { return [Map(L, mode), ...IntrinsicRest(R, mode)] } function Visit(schema: TSchema, mode: TIntrinsicMode) { - // prettier-ignore - return TypeGuard.TTemplateLiteral(schema) ? IntrinsicTemplateLiteral(schema, mode) : + return ( + TypeGuard.TTemplateLiteral(schema) ? IntrinsicTemplateLiteral(schema, mode) : TypeGuard.TUnion(schema) ? Type.Union(IntrinsicRest(schema.anyOf, mode)) : - TypeGuard.TLiteral(schema) ? Type.Literal(IntrinsicLiteral(schema.const, mode)) : - schema + TypeGuard.TLiteral(schema) ? Type.Literal(IntrinsicLiteral(schema.const, mode)) : + schema + ) } /** Applies an intrinsic string manipulation to the given type. */ export function Map(schema: T, mode: M): TIntrinsic { @@ -2487,12 +2447,12 @@ export namespace ObjectMap { // prevent sub schema mapping as unregistered kinds will not pass TSchema checks. This is notable in the // case of TObject where unregistered property kinds cause the TObject check to fail. As mapping is only // used for composition, we use explicit checks instead. - switch (true) { - case schema[Kind] === 'Intersect': return TIntersect(schema as TIntersect, callback) - case schema[Kind] === 'Union': return TUnion(schema as TUnion, callback) - case schema[Kind] === 'Object': return TObject(schema as TObject, callback) - default: return schema - } + return ( + schema[Kind] === 'Intersect' ? TIntersect(schema as TIntersect, callback) : + schema[Kind] === 'Union' ? TUnion(schema as TUnion, callback) : + schema[Kind] === 'Object' ? TObject(schema as TObject, callback) : + schema + ) } export function Map(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T { return { ...Visit(TypeClone.Type(schema), callback), ...options } as unknown as T @@ -2537,16 +2497,16 @@ export namespace Indexer { return ['number'] } function Visit(schema: TSchema, options: IndexerOptions): string[] { - switch (true) { - case TypeGuard.TTemplateLiteral(schema): return TTemplateLiteral(schema, options) - case TypeGuard.TIntersect(schema): return TIntersect(schema, options) - case TypeGuard.TUnion(schema): return TUnion(schema, options) - case TypeGuard.TObject(schema): return TObject(schema, options) - case TypeGuard.TRecord(schema): return TRecord(schema, options) - case TypeGuard.TInteger(schema): return TInteger(schema, options) - case TypeGuard.TNumber(schema): return TNumber(schema, options) - default: return [] - } + return ( + TypeGuard.TTemplateLiteral(schema) ? TTemplateLiteral(schema, options) : + TypeGuard.TIntersect(schema) ? TIntersect(schema, options) : + TypeGuard.TUnion(schema) ? TUnion(schema, options) : + TypeGuard.TObject(schema) ? TObject(schema, options) : + TypeGuard.TRecord(schema) ? TRecord(schema, options) : + TypeGuard.TInteger(schema) ? TInteger(schema, options) : + TypeGuard.TNumber(schema) ? TNumber(schema, options) : + [] + ) } /** Resolves a regular expression pattern matching all keys in this schema */ export function Pattern(schema: TSchema): string { @@ -2583,11 +2543,11 @@ export namespace Accessor { return IsUnionOptional(schema.anyOf) ? Type.Optional(Type.Union(OptionalUnwrap(schema.anyOf))) : schema } function ResolveOptional(schema: TSchema) { - switch (true) { - case schema[Kind] === 'Intersect': return ResolveIntersect(schema as TIntersect) - case schema[Kind] === 'Union': return ResolveUnion(schema as TUnion) - default: return schema - } + return ( + schema[Kind] === 'Intersect' ? ResolveIntersect(schema as TIntersect) : + schema[Kind] === 'Union' ? ResolveUnion(schema as TUnion) : + schema + ) } function TIntersect(schema: TIntersect, key: string): TSchema { const resolved = schema.allOf.reduce((acc, schema) => { @@ -2612,13 +2572,14 @@ export namespace Accessor { return element } function Visit(schema: TSchema, key: string): TSchema { - switch (true) { - case schema[Kind] === 'Intersect': return TIntersect(schema as TIntersect, key) - case schema[Kind] === 'Union': return TUnion(schema as TUnion, key) - case schema[Kind] === 'Object': return TObject(schema as TObject, key) - case schema[Kind] === 'Tuple': return TTuple(schema as TTuple, key) - default: return Type.Never() - } + return ( + schema[Kind] === 'Intersect' ? TIntersect(schema as TIntersect, key) : + schema[Kind] === 'Union' ? TUnion(schema as TUnion, key) : + schema[Kind] === 'Object' ? TObject(schema as TObject, key) : + schema[Kind] === 'Tuple' ? TTuple(schema as TTuple, key) : + Type.Never() + ) + } export function Resolve(schema: TSchema, keys: TPropertyKey[], options: SchemaOptions = {}): TSchema { const resolved = keys.map((key) => Visit(schema, key.toString())) @@ -2628,20 +2589,21 @@ export namespace Accessor { // -------------------------------------------------------------------------- // KeyArrayResolver // -------------------------------------------------------------------------- -export class KeyArrayResolverError extends TypeBoxError { } +export class KeyArrayResolverError extends TypeBoxError {} // prettier-ignore export namespace KeyArrayResolver { /** Resolves an array of string[] keys from the given schema or array type. */ export function Resolve(schema: TSchema | string[]): string[] { - // prettier-ignore - return Array.isArray(schema) ? schema : + return ( + Array.isArray(schema) ? schema : TypeGuard.TUnionLiteral(schema) ? schema.anyOf.map((schema) => schema.const.toString()) : - TypeGuard.TLiteral(schema) ? [schema.const as string] : - TypeGuard.TTemplateLiteral(schema) ? (() => { - const expression = TemplateLiteralParser.ParseExact(schema.pattern) - if (!TemplateLiteralFinite.Check(expression)) throw new KeyArrayResolverError('Cannot resolve keys from infinite template expression') - return [...TemplateLiteralGenerator.Generate(expression)] - })() : [] + TypeGuard.TLiteral(schema) ? [schema.const as string] : + TypeGuard.TTemplateLiteral(schema) ? (() => { + const expression = TemplateLiteralParser.ParseExact(schema.pattern) + if (!TemplateLiteralFinite.Check(expression)) throw new KeyArrayResolverError('Cannot resolve keys from infinite template expression') + return [...TemplateLiteralGenerator.Generate(expression)] + })() : [] + ) } } // -------------------------------------------------------------------------- @@ -2665,7 +2627,8 @@ export namespace UnionResolver { // -------------------------------------------------------------------------- // TemplateLiteralPattern // -------------------------------------------------------------------------- -export class TemplateLiteralPatternError extends TypeBoxError { } +export class TemplateLiteralPatternError extends TypeBoxError {} +// prettier-ignore export namespace TemplateLiteralPattern { function Throw(message: string): never { throw new TemplateLiteralPatternError(message) @@ -2674,17 +2637,16 @@ export namespace TemplateLiteralPattern { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') } function Visit(schema: TSchema, acc: string): string { - // prettier-ignore return ( TypeGuard.TTemplateLiteral(schema) ? schema.pattern.slice(1, schema.pattern.length - 1) : - TypeGuard.TUnion(schema) ? `(${schema.anyOf.map((schema) => Visit(schema, acc)).join('|')})` : - TypeGuard.TNumber(schema) ? `${acc}${PatternNumber}` : - TypeGuard.TInteger(schema) ? `${acc}${PatternNumber}` : - TypeGuard.TBigInt(schema) ? `${acc}${PatternNumber}` : - TypeGuard.TString(schema) ? `${acc}${PatternString}` : - TypeGuard.TLiteral(schema) ? `${acc}${Escape(schema.const.toString())}` : - TypeGuard.TBoolean(schema) ? `${acc}${PatternBoolean}` : - Throw(`Unexpected Kind '${schema[Kind]}'`) + TypeGuard.TUnion(schema) ? `(${schema.anyOf.map((schema) => Visit(schema, acc)).join('|')})` : + TypeGuard.TNumber(schema) ? `${acc}${PatternNumber}` : + TypeGuard.TInteger(schema) ? `${acc}${PatternNumber}` : + TypeGuard.TBigInt(schema) ? `${acc}${PatternNumber}` : + TypeGuard.TString(schema) ? `${acc}${PatternString}` : + TypeGuard.TLiteral(schema) ? `${acc}${Escape(schema.const.toString())}` : + TypeGuard.TBoolean(schema) ? `${acc}${PatternBoolean}` : + Throw(`Unexpected Kind '${schema[Kind]}'`) ) } export function Create(kinds: TTemplateLiteralKind[]): string { @@ -2706,7 +2668,8 @@ export namespace TemplateLiteralResolver { // -------------------------------------------------------------------------------------- // TemplateLiteralParser // -------------------------------------------------------------------------------------- -export class TemplateLiteralParserError extends TypeBoxError { } +export class TemplateLiteralParserError extends TypeBoxError {} +// prettier-ignore export namespace TemplateLiteralParser { export type Expression = And | Or | Const export type Const = { type: 'const'; const: string } @@ -2801,18 +2764,20 @@ export namespace TemplateLiteralParser { index = end - 1 } } - // prettier-ignore - return (expressions.length === 0) ? { type: 'const', const: '' } : + return ( + (expressions.length === 0) ? { type: 'const', const: '' } : (expressions.length === 1) ? expressions[0] : - { type: 'and', expr: expressions } + { type: 'and', expr: expressions } + ) } /** Parses a pattern and returns an expression tree */ export function Parse(pattern: string): Expression { - // prettier-ignore - return IsGroup(pattern) ? Parse(InGroup(pattern)) : + return ( + IsGroup(pattern) ? Parse(InGroup(pattern)) : IsPrecedenceOr(pattern) ? Or(pattern) : - IsPrecedenceAnd(pattern) ? And(pattern) : - { type: 'const', const: pattern } + IsPrecedenceAnd(pattern) ? And(pattern) : + { type: 'const', const: pattern } + ) } /** Parses a pattern and strips forward and trailing ^ and $ */ export function ParseExact(pattern: string): Expression { @@ -2822,13 +2787,13 @@ export namespace TemplateLiteralParser { // -------------------------------------------------------------------------------------- // TemplateLiteralFinite // -------------------------------------------------------------------------------------- -export class TemplateLiteralFiniteError extends TypeBoxError { } +export class TemplateLiteralFiniteError extends TypeBoxError {} +// prettier-ignore export namespace TemplateLiteralFinite { function Throw(message: string): never { throw new TemplateLiteralFiniteError(message) } function IsNumber(expression: TemplateLiteralParser.Expression): boolean { - // prettier-ignore return ( expression.type === 'or' && expression.expr.length === 2 && @@ -2839,7 +2804,6 @@ export namespace TemplateLiteralFinite { ) } function IsBoolean(expression: TemplateLiteralParser.Expression): boolean { - // prettier-ignore return ( expression.type === 'or' && expression.expr.length === 2 && @@ -2853,19 +2817,21 @@ export namespace TemplateLiteralFinite { return expression.type === 'const' && expression.const === '.*' } export function Check(expression: TemplateLiteralParser.Expression): boolean { - // prettier-ignore - return IsBoolean(expression) ? true : + return ( + IsBoolean(expression) ? true : IsNumber(expression) || IsString(expression) ? false : - (expression.type === 'and') ? expression.expr.every((expr) => Check(expr)) : - (expression.type === 'or') ? expression.expr.every((expr) => Check(expr)) : - (expression.type === 'const') ? true : - Throw(`Unknown expression type`) + (expression.type === 'and') ? expression.expr.every((expr) => Check(expr)) : + (expression.type === 'or') ? expression.expr.every((expr) => Check(expr)) : + (expression.type === 'const') ? true : + Throw(`Unknown expression type`) + ) } } // -------------------------------------------------------------------------------------- // TemplateLiteralGenerator // -------------------------------------------------------------------------------------- -export class TemplateLiteralGeneratorError extends TypeBoxError { } +export class TemplateLiteralGeneratorError extends TypeBoxError {} +// prettier-ignore export namespace TemplateLiteralGenerator { function* Reduce(buffer: string[][]): IterableIterator { if (buffer.length === 1) return yield* buffer[0] @@ -2885,35 +2851,34 @@ export namespace TemplateLiteralGenerator { return yield expression.const } export function* Generate(expression: TemplateLiteralParser.Expression): IterableIterator { - // prettier-ignore return ( expression.type === 'and' ? yield* And(expression) : - expression.type === 'or' ? yield* Or(expression) : - expression.type === 'const' ? yield* Const(expression) : - (() => { throw new TemplateLiteralGeneratorError('Unknown expression') })() + expression.type === 'or' ? yield* Or(expression) : + expression.type === 'const' ? yield* Const(expression) : + (() => { throw new TemplateLiteralGeneratorError('Unknown expression') })() ) } } // --------------------------------------------------------------------- // TemplateLiteralDslParser // --------------------------------------------------------------------- +// prettier-ignore export namespace TemplateLiteralDslParser { function* ParseUnion(template: string): IterableIterator { const trim = template.trim().replace(/"|'/g, '') - // prettier-ignore return ( trim === 'boolean' ? yield Type.Boolean() : - trim === 'number' ? yield Type.Number() : - trim === 'bigint' ? yield Type.BigInt() : - trim === 'string' ? yield Type.String() : - yield (() => { - const literals = trim.split('|').map((literal) => Type.Literal(literal.trim())) - return ( - literals.length === 0 ? Type.Never() : - literals.length === 1 ? literals[0] : - Type.Union(literals) - ) - })() + trim === 'number' ? yield Type.Number() : + trim === 'bigint' ? yield Type.BigInt() : + trim === 'string' ? yield Type.String() : + yield (() => { + const literals = trim.split('|').map((literal) => Type.Literal(literal.trim())) + return ( + literals.length === 0 ? Type.Never() : + literals.length === 1 ? literals[0] : + Type.Union(literals) + ) + })() ) } function* ParseTerminal(template: string): IterableIterator { @@ -2949,16 +2914,16 @@ export namespace TemplateLiteralDslParser { // TransformBuilder // --------------------------------------------------------------------- export class TransformDecodeBuilder { - constructor(private readonly schema: T) { } + constructor(private readonly schema: T) {} public Decode, U>>(decode: D): TransformEncodeBuilder { return new TransformEncodeBuilder(this.schema, decode) } } +// prettier-ignore export class TransformEncodeBuilder { - constructor(private readonly schema: T, private readonly decode: D) { } + constructor(private readonly schema: T, private readonly decode: D) {} public Encode, StaticDecode>>(encode: E): TTransform> { const schema = TypeClone.Type(this.schema) - // prettier-ignore return ( TypeGuard.TTransform(schema) ? (() => { const Encode = (value: unknown) => schema[Transform].Encode(encode(value as any)) @@ -2979,7 +2944,7 @@ let TypeOrdinal = 0 // -------------------------------------------------------------------------- // TypeBuilder // -------------------------------------------------------------------------- -export class TypeBuilderError extends TypeBoxError { } +export class TypeBuilderError extends TypeBoxError {} export class TypeBuilder { /** `[Internal]` Creates a schema without `static` and `params` types */ protected Create(schema: Omit): T { @@ -3004,6 +2969,7 @@ export class TypeBuilder { // -------------------------------------------------------------------------- // JsonTypeBuilder // -------------------------------------------------------------------------- +// prettier-ignore export class JsonTypeBuilder extends TypeBuilder { // ------------------------------------------------------------------------ // Modifiers @@ -3049,7 +3015,6 @@ export class JsonTypeBuilder extends TypeBuilder { /** `[Json]` Creates a Enum type */ public Enum>(item: T, options: SchemaOptions = {}): TEnum { if (ValueGuard.IsUndefined(item)) return this.Throw('Enum undefined or empty') - // prettier-ignore const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key as any)).map((key) => item[key]) as T[keyof T][] const values2 = [...new Set(values1)] const anyOf = values2.map((value) => Type.Literal(value)) @@ -3068,30 +3033,28 @@ export class JsonTypeBuilder extends TypeBuilder { } /** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */ public Exclude(unionType: L, excludedMembers: R, options: SchemaOptions = {}): TExclude { - // prettier-ignore return ( TypeGuard.TTemplateLiteral(unionType) ? this.Exclude(TemplateLiteralResolver.Resolve(unionType), excludedMembers, options) : - TypeGuard.TTemplateLiteral(excludedMembers) ? this.Exclude(unionType, TemplateLiteralResolver.Resolve(excludedMembers), options) : - TypeGuard.TUnion(unionType) ? (() => { - const narrowed = unionType.anyOf.filter((inner) => TypeExtends.Extends(inner, excludedMembers) === TypeExtendsResult.False) - return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options)) as TExclude - })() : - TypeExtends.Extends(unionType, excludedMembers) !== TypeExtendsResult.False ? this.Never(options) : - TypeClone.Type(unionType, options) + TypeGuard.TTemplateLiteral(excludedMembers) ? this.Exclude(unionType, TemplateLiteralResolver.Resolve(excludedMembers), options) : + TypeGuard.TUnion(unionType) ? (() => { + const narrowed = unionType.anyOf.filter((inner) => TypeExtends.Extends(inner, excludedMembers) === TypeExtendsResult.False) + return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options)) as TExclude + })() : + TypeExtends.Extends(unionType, excludedMembers) !== TypeExtendsResult.False ? this.Never(options) : + TypeClone.Type(unionType, options) ) as TExclude } /** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */ public Extract(type: L, union: R, options: SchemaOptions = {}): TExtract { - // prettier-ignore return ( TypeGuard.TTemplateLiteral(type) ? this.Extract(TemplateLiteralResolver.Resolve(type), union, options) : - TypeGuard.TTemplateLiteral(union) ? this.Extract(type, TemplateLiteralResolver.Resolve(union) as any, options) : - TypeGuard.TUnion(type) ? (() => { - const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False) - return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options)) - })() : - TypeExtends.Extends(type, union) !== TypeExtendsResult.False ? TypeClone.Type(type, options) : - this.Never(options) + TypeGuard.TTemplateLiteral(union) ? this.Extract(type, TemplateLiteralResolver.Resolve(union) as any, options) : + TypeGuard.TUnion(type) ? (() => { + const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False) + return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options)) + })() : + TypeExtends.Extends(type, union) !== TypeExtendsResult.False ? TypeClone.Type(type, options) : + this.Never(options) ) as TExtract } // /** `[Json]` Returns an Indexed property type for the given keys */ @@ -3117,20 +3080,19 @@ export class JsonTypeBuilder extends TypeBuilder { public Index(schema: T, keys: [...K], options?: SchemaOptions): TIndex /** `[Json]` Returns an Indexed property type for the given keys */ public Index(schema: TSchema, unresolved: any, options: SchemaOptions = {}): any { - // prettier-ignore return ( TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved) ? (() => { return TypeClone.Type(schema.items, options) })() : - TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved) ? (() => { - const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items - const cloned = items.map((schema) => TypeClone.Type(schema)) - return this.Union(cloned, options) - })() : (() => { - const keys = KeyArrayResolver.Resolve(unresolved) - const clone = TypeClone.Type(schema) - return Accessor.Resolve(clone, keys, options) - })() + TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved) ? (() => { + const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items + const cloned = items.map((schema) => TypeClone.Type(schema)) + return this.Union(cloned, options) + })() : (() => { + const keys = KeyArrayResolver.Resolve(unresolved) + const clone = TypeClone.Type(schema) + return Accessor.Resolve(clone, keys, options) + })() ) } /** `[Json]` Creates an Integer type */ @@ -3150,7 +3112,6 @@ export class JsonTypeBuilder extends TypeBuilder { if (allOf.some((schema) => TypeGuard.TTransform(schema))) this.Throw('Cannot intersect transform types') const objects = allOf.every((schema) => TypeGuard.TObject(schema)) const cloned = TypeClone.Rest(allOf) - // prettier-ignore const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties) ? { unevaluatedProperties: TypeClone.Type(options.unevaluatedProperties) } : {} @@ -3160,7 +3121,6 @@ export class JsonTypeBuilder extends TypeBuilder { } /** `[Json]` Creates a KeyOf type */ public KeyOf(schema: T, options: SchemaOptions = {}): TKeyOf { - // prettier-ignore return ( TypeGuard.TRecord(schema) ? (() => { const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0] @@ -3233,7 +3193,6 @@ export class JsonTypeBuilder extends TypeBuilder { /** `[Json]` Constructs a type whose keys are omitted from the given type */ public Omit(schema: TSchema, unresolved: any, options: SchemaOptions = {}): any { const keys = KeyArrayResolver.Resolve(unresolved) - // prettier-ignore return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', Transform]), (object) => { if (ValueGuard.IsArray(object.required)) { object.required = object.required.filter((key: string) => !keys.includes(key as any)) @@ -3247,7 +3206,6 @@ export class JsonTypeBuilder extends TypeBuilder { } /** `[Json]` Constructs a type where all properties are optional */ public Partial(schema: T, options: ObjectOptions = {}): TPartial { - // prettier-ignore return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', Transform]), (object) => { const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => { return { ...acc, [key]: this.Optional(object.properties[key]) } @@ -3268,7 +3226,6 @@ export class JsonTypeBuilder extends TypeBuilder { /** `[Json]` Constructs a type whose keys are picked from the given type */ public Pick(schema: TSchema, unresolved: any, options: SchemaOptions = {}): any { const keys = KeyArrayResolver.Resolve(unresolved) - // prettier-ignore return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', Transform]), (object) => { if (ValueGuard.IsArray(object.required)) { object.required = object.required.filter((key: any) => keys.includes(key)) @@ -3282,36 +3239,33 @@ export class JsonTypeBuilder extends TypeBuilder { } /** `[Json]` Creates a Record type */ public Record(key: K, schema: T, options: ObjectOptions = {}): TRecordResolve { - // prettier-ignore return ( TypeGuard.TTemplateLiteral(key) ? (() => { const expression = TemplateLiteralParser.ParseExact(key.pattern) - // prettier-ignore return TemplateLiteralFinite.Check(expression) - ? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(schema) }), {} as TProperties), options)) - : this.Create({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Type(schema) } }) + ? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(schema) }), {} as TProperties), options)) + : this.Create({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Type(schema) } }) })() : - TypeGuard.TUnion(key) ? (() => { - const union = UnionResolver.Resolve(key) - if (TypeGuard.TUnionLiteral(union)) { - const properties = union.anyOf.reduce((acc: any, literal: any) => ({ ...acc, [literal.const]: TypeClone.Type(schema) }), {} as TProperties) - return this.Object(properties, { ...options, [Hint]: 'Record' }) - } else this.Throw('Record key of type union contains non-literal types') - })() : - TypeGuard.TLiteral(key) ? (() => { - // prettier-ignore - return (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const)) - ? this.Object({ [key.const]: TypeClone.Type(schema) }, options) - : this.Throw('Record key of type literal is not of type string or number') - })() : - TypeGuard.TInteger(key) || TypeGuard.TNumber(key) ? (() => { - return this.Create({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [PatternNumberExact]: TypeClone.Type(schema) } }) - })() : - TypeGuard.TString(key) ? (() => { - const pattern = ValueGuard.IsUndefined(key.pattern) ? PatternStringExact : key.pattern - return this.Create({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Type(schema) } }) - })() : - this.Never() + TypeGuard.TUnion(key) ? (() => { + const union = UnionResolver.Resolve(key) + if (TypeGuard.TUnionLiteral(union)) { + const properties = union.anyOf.reduce((acc: any, literal: any) => ({ ...acc, [literal.const]: TypeClone.Type(schema) }), {} as TProperties) + return this.Object(properties, { ...options, [Hint]: 'Record' }) + } else this.Throw('Record key of type union contains non-literal types') + })() : + TypeGuard.TLiteral(key) ? (() => { + return (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const)) + ? this.Object({ [key.const]: TypeClone.Type(schema) }, options) + : this.Throw('Record key of type literal is not of type string or number') + })() : + TypeGuard.TInteger(key) || TypeGuard.TNumber(key) ? (() => { + return this.Create({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [PatternNumberExact]: TypeClone.Type(schema) } }) + })() : + TypeGuard.TString(key) ? (() => { + const pattern = ValueGuard.IsUndefined(key.pattern) ? PatternStringExact : key.pattern + return this.Create({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Type(schema) } }) + })() : + this.Never() ) } /** `[Json]` Creates a Recursive type */ @@ -3333,7 +3287,6 @@ export class JsonTypeBuilder extends TypeBuilder { } /** `[Json]` Constructs a type where all properties are required */ public Required(schema: T, options: SchemaOptions = {}): TRequired { - // prettier-ignore return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', Transform]), (object) => { const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => { return { ...acc, [key]: this.Discard(object.properties[key], [Optional]) as TSchema } @@ -3357,7 +3310,6 @@ export class JsonTypeBuilder extends TypeBuilder { public TemplateLiteral(kinds: [...T], options?: SchemaOptions): TTemplateLiteral /** `[Json]` Creates a TemplateLiteral type */ public TemplateLiteral(unresolved: unknown, options: SchemaOptions = {}) { - // prettier-ignore const pattern = ValueGuard.IsString(unresolved) ? TemplateLiteralPattern.Create(TemplateLiteralDslParser.Parse(unresolved)) : TemplateLiteralPattern.Create(unresolved as TTemplateLiteralKind[]) @@ -3371,7 +3323,6 @@ export class JsonTypeBuilder extends TypeBuilder { public Tuple(items: [...T], options: SchemaOptions = {}): TTuple { const [additionalItems, minItems, maxItems] = [false, items.length, items.length] const clonedItems = TypeClone.Rest(items) - // prettier-ignore const schema = (items.length > 0 ? { ...options, [Kind]: 'Tuple', type: 'array', items: clonedItems, additionalItems, minItems, maxItems } : { ...options, [Kind]: 'Tuple', type: 'array', minItems, maxItems }) as any @@ -3391,7 +3342,6 @@ export class JsonTypeBuilder extends TypeBuilder { public Union(template: T): TUnionTemplateLiteral /** `[Json]` Creates a Union type */ public Union(union: TSchema[] | TTemplateLiteral, options: SchemaOptions = {}) { - // prettier-ignore return TypeGuard.TTemplateLiteral(union) ? TemplateLiteralResolver.Resolve(union) : (() => { @@ -3418,6 +3368,7 @@ export class JsonTypeBuilder extends TypeBuilder { // -------------------------------------------------------------------------- // JavaScriptTypeBuilder // -------------------------------------------------------------------------- +// prettier-ignore export class JavaScriptTypeBuilder extends JsonTypeBuilder { /** `[JavaScript]` Creates a AsyncIterator type */ public AsyncIterator(items: T, options: SchemaOptions = {}): TAsyncIterator { @@ -3425,17 +3376,15 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder { } /** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */ public Awaited(schema: T, options: SchemaOptions = {}): TAwaited { - // prettier-ignore const Unwrap = (rest: TSchema[]): TSchema[] => rest.length > 0 ? (() => { const [L, ...R] = rest return [this.Awaited(L), ...Unwrap(R)] })() : rest - // prettier-ignore return ( TypeGuard.TIntersect(schema) ? Type.Intersect(Unwrap(schema.allOf)) : - TypeGuard.TUnion(schema) ? Type.Union(Unwrap(schema.anyOf)) : - TypeGuard.TPromise(schema) ? this.Awaited(schema.item) : - TypeClone.Type(schema, options) + TypeGuard.TUnion(schema) ? Type.Union(Unwrap(schema.anyOf)) : + TypeGuard.TPromise(schema) ? this.Awaited(schema.item) : + TypeClone.Type(schema, options) ) as TAwaited } /** `[JavaScript]` Creates a BigInt type */ @@ -3485,12 +3434,6 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder { const pattern = ValueGuard.IsString(unresolved) ? unresolved : unresolved.source return this.Create({ ...options, [Kind]: 'String', type: 'string', pattern }) } - /** - * @deprecated Use `Type.RegExp` - */ - public RegEx(regex: RegExp, options: SchemaOptions = {}): TString { - return this.RegExp(regex, options) - } /** `[JavaScript]` Extracts the ReturnType from the given Function type */ public ReturnType>(schema: T, options: SchemaOptions = {}): TReturnType { return TypeClone.Type(schema.returns, options)