typebox 1.1.12 → 1.1.13

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.
@@ -3,7 +3,7 @@ import { type TUnion } from '../../types/union.mjs';
3
3
  import { type TDeferred } from '../../types/deferred.mjs';
4
4
  import { type TRef } from '../../types/ref.mjs';
5
5
  import { type TParameter } from '../../types/parameter.mjs';
6
- type TCollectDistributionNames<Expression extends TSchema, Result extends string[] = []> = (Expression extends TDeferred<'Conditional', [infer Left extends TSchema, infer _Right extends TSchema, infer True extends TSchema, infer False extends TSchema]> ? Left extends TRef<infer Name extends string> ? TCollectDistributionNames<True, TCollectDistributionNames<False, [...Result, Name]>> : TCollectDistributionNames<True, TCollectDistributionNames<False, Result>> : Expression extends TDeferred<'Mapped', [infer _Identifier extends TSchema, infer Type extends TSchema, infer _As extends TSchema, infer _Property extends TSchema]> ? (Type extends TDeferred<'KeyOf', [infer Ref extends TRef]> ? [...Result, Ref['$ref']] : Result) : Result);
6
+ type TCollectDistributionNames<Expression extends TSchema, Result extends string[] = []> = (Expression extends TDeferred<'Conditional', [infer Left extends TSchema, infer _Right extends TSchema, infer True extends TSchema, infer False extends TSchema]> ? Left extends TRef ? TCollectDistributionNames<True, TCollectDistributionNames<False, [...Result, Left['$ref']]>> : TCollectDistributionNames<True, TCollectDistributionNames<False, Result>> : Expression extends TDeferred<'Mapped', [infer _Identifier extends TSchema, infer Type extends TSchema, infer _As extends TSchema, infer _Property extends TSchema]> ? (Type extends TDeferred<'KeyOf', [infer Ref extends TRef]> ? [...Result, Ref['$ref']] : Result) : Result);
7
7
  type TBuildDistributionArray<Parameters extends TParameter[], Names extends string[], Result extends boolean[] = []> = (Parameters extends [infer Left extends TParameter, ...infer Right extends TParameter[]] ? Left['name'] extends Names[number] ? TBuildDistributionArray<Right, Names, [...Result, true]> : TBuildDistributionArray<Right, Names, [...Result, false]> : Result);
8
8
  type TZipDistributionArray<Arguments extends TSchema[], DistributionArray extends boolean[], Result extends [boolean, TSchema][] = []> = (Arguments extends [infer ArgumentLeft extends TSchema, ...infer ArgumentRight extends TSchema[]] ? DistributionArray extends [infer BooleanLeft extends boolean, ...infer BooleanRight extends boolean[]] ? TZipDistributionArray<ArgumentRight, BooleanRight, [...Result, [BooleanLeft, ArgumentLeft]]> : Result : Result);
9
9
  type TExpand<Type extends TSchema> = (Type extends TUnion<infer Types extends TSchema[]> ? [...Types] : [Type]);
@@ -8,7 +8,7 @@ function CollectDistributionNames(expression, result = []) {
8
8
  // Conditional
9
9
  IsDeferred(expression) && Guard.IsEqual(expression.action, 'Conditional')
10
10
  ? IsRef(expression.parameters[0])
11
- ? CollectDistributionNames(expression.parameters[2], CollectDistributionNames(expression.parameters[3], [...result, expression.parameters[0].$ref]))
11
+ ? CollectDistributionNames(expression.parameters[2], CollectDistributionNames(expression.parameters[3], [...result, expression.parameters[0]['$ref']]))
12
12
  : CollectDistributionNames(expression.parameters[2], CollectDistributionNames(expression.parameters[3], result))
13
13
  // Mapped
14
14
  : IsDeferred(expression) && Guard.IsEqual(expression.action, 'Mapped')
@@ -1,8 +1,10 @@
1
1
  import { type TSchema } from '../types/schema.mjs';
2
2
  import { type TProperties } from '../types/properties.mjs';
3
+ import { type TAny } from '../types/any.mjs';
3
4
  import { type TConstructor } from '../types/constructor.mjs';
5
+ import { type TUnknown } from '../types/unknown.mjs';
4
6
  import * as Result from './result.mjs';
5
7
  import { type TExtendsParameters } from './parameters.mjs';
6
8
  import { type TExtendsReturnType } from './return-type.mjs';
7
- export type TExtendsConstructor<Inferred extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema, Right extends TSchema> = (Right extends TConstructor<infer RightParamters extends TSchema[], infer RightInstanceType extends TSchema> ? TExtendsParameters<Inferred, Parameters, RightParamters> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, InstanceType, RightInstanceType> : Result.TExtendsFalse : Result.TExtendsFalse);
9
+ export type TExtendsConstructor<Inferred extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema, Right extends TSchema> = (Right extends TAny ? Result.TExtendsTrue : Right extends TUnknown ? Result.TExtendsTrue : Right extends TConstructor ? TExtendsParameters<Inferred, Parameters, Right['parameters']> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, InstanceType, Right['instanceType']> : Result.TExtendsFalse : Result.TExtendsFalse);
8
10
  export declare function ExtendsConstructor<Inferred extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema, Right extends TSchema>(inferred: Inferred, parameters: [...Parameters], returnType: InstanceType, right: Right): TExtendsConstructor<Inferred, Parameters, InstanceType, Right>;
@@ -1,5 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
+ import { IsAny } from '../types/any.mjs';
2
3
  import { IsConstructor } from '../types/constructor.mjs';
4
+ import { IsUnknown } from '../types/unknown.mjs';
3
5
  import * as Result from './result.mjs';
4
6
  // ------------------------------------------------------------------
5
7
  // Parameters | ReturnType
@@ -7,10 +9,9 @@ import * as Result from './result.mjs';
7
9
  import { ExtendsParameters } from './parameters.mjs';
8
10
  import { ExtendsReturnType } from './return-type.mjs';
9
11
  export function ExtendsConstructor(inferred, parameters, returnType, right) {
10
- return (IsConstructor(right) ? (() => {
11
- const check = ExtendsParameters(inferred, parameters, right.parameters);
12
- return Result.IsExtendsTrueLike(check)
13
- ? ExtendsReturnType(check.inferred, returnType, right.instanceType)
14
- : Result.ExtendsFalse();
15
- })() : Result.ExtendsFalse());
12
+ return (IsAny(right) ? Result.ExtendsTrue(inferred) :
13
+ IsUnknown(right) ? Result.ExtendsTrue(inferred) :
14
+ IsConstructor(right) ? Result.Match(ExtendsParameters(inferred, parameters, right['parameters']), inferred => ExtendsReturnType(inferred, returnType, right['instanceType']), () => Result.ExtendsFalse()) // 'not-a-parameter-match'
15
+ : Result.ExtendsFalse() // 'not-a-constructor'
16
+ );
16
17
  }
@@ -1,6 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Memory } from '../../system/memory/index.mjs';
3
- import { IsSchema } from '../types/schema.mjs';
4
3
  import { IsAny } from '../types/any.mjs';
5
4
  import { IsEnum } from '../types/enum.mjs';
6
5
  import { IsInfer } from '../types/infer.mjs';
@@ -26,26 +25,14 @@ function ExtendsRightEnum(inferred, left, right) {
26
25
  return ExtendsLeft(inferred, left, union);
27
26
  }
28
27
  function ExtendsRightIntersect(inferred, left, right) {
29
- const [head, ...tail] = right;
30
- return (IsSchema(head) ? (() => {
31
- const check = ExtendsLeft(inferred, left, head);
32
- return Result.IsExtendsTrueLike(check)
33
- ? ExtendsRightIntersect(check.inferred, left, tail)
34
- : Result.ExtendsFalse();
35
- })() : Result.ExtendsTrue(inferred));
28
+ return Result.TakeLeft(right, (head, tail) => Result.Match(ExtendsLeft(inferred, left, head), inferred => ExtendsRightIntersect(inferred, left, tail), () => Result.ExtendsFalse()), () => Result.ExtendsTrue(inferred));
36
29
  }
37
30
  function ExtendsRightTemplateLiteral(inferred, left, right) {
38
31
  const decoded = TemplateLiteralDecode(right);
39
32
  return ExtendsLeft(inferred, left, decoded);
40
33
  }
41
34
  function ExtendsRightUnion(inferred, left, right) {
42
- const [head, ...tail] = right;
43
- return (IsSchema(head) ? (() => {
44
- const check = ExtendsLeft(inferred, left, head);
45
- return Result.IsExtendsTrueLike(check)
46
- ? Result.ExtendsTrue(check.inferred)
47
- : ExtendsRightUnion(inferred, left, tail);
48
- })() : Result.ExtendsFalse());
35
+ return Result.TakeLeft(right, (head, tail) => Result.Match(ExtendsLeft(inferred, left, head), inferred => Result.ExtendsTrue(inferred), () => ExtendsRightUnion(inferred, left, tail)), () => Result.ExtendsFalse());
49
36
  }
50
37
  export function ExtendsRight(inferred, left, right) {
51
38
  return (IsAny(right) ? ExtendsRightAny(inferred, left) :
@@ -1,8 +1,10 @@
1
1
  import { type TSchema } from '../types/schema.mjs';
2
2
  import { type TProperties } from '../types/properties.mjs';
3
+ import { type TAny } from '../types/any.mjs';
3
4
  import { type TFunction } from '../types/function.mjs';
5
+ import { type TUnknown } from '../types/unknown.mjs';
4
6
  import * as Result from './result.mjs';
5
7
  import { type TExtendsParameters } from './parameters.mjs';
6
8
  import { type TExtendsReturnType } from './return-type.mjs';
7
- export type TExtendsFunction<Inferred extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema, Right extends TSchema> = (Right extends TFunction<infer RightParamters extends TSchema[], infer RightReturnType extends TSchema> ? TExtendsParameters<Inferred, Parameters, RightParamters> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, ReturnType, RightReturnType> : Result.TExtendsFalse : Result.TExtendsFalse);
9
+ export type TExtendsFunction<Inferred extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema, Right extends TSchema> = (Right extends TAny ? Result.TExtendsTrue : Right extends TUnknown ? Result.TExtendsTrue : Right extends TFunction ? TExtendsParameters<Inferred, Parameters, Right['parameters']> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, ReturnType, Right['returnType']> : Result.TExtendsFalse : Result.TExtendsFalse);
8
10
  export declare function ExtendsFunction<Inferred extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema, Right extends TSchema>(inferred: Inferred, parameters: [...Parameters], returnType: ReturnType, right: Right): TExtendsFunction<Inferred, Parameters, ReturnType, Right>;
@@ -1,5 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
+ import { IsAny } from '../types/any.mjs';
2
3
  import { IsFunction } from '../types/function.mjs';
4
+ import { IsUnknown } from '../types/unknown.mjs';
3
5
  import * as Result from './result.mjs';
4
6
  // ------------------------------------------------------------------
5
7
  // Parameters | ReturnType
@@ -7,10 +9,9 @@ import * as Result from './result.mjs';
7
9
  import { ExtendsParameters } from './parameters.mjs';
8
10
  import { ExtendsReturnType } from './return-type.mjs';
9
11
  export function ExtendsFunction(inferred, parameters, returnType, right) {
10
- return (IsFunction(right) ? (() => {
11
- const check = ExtendsParameters(inferred, parameters, right.parameters);
12
- return Result.IsExtendsTrueLike(check)
13
- ? ExtendsReturnType(check.inferred, returnType, right.returnType)
14
- : Result.ExtendsFalse();
15
- })() : Result.ExtendsFalse());
12
+ return (IsAny(right) ? Result.ExtendsTrue(inferred) :
13
+ IsUnknown(right) ? Result.ExtendsTrue(inferred) :
14
+ IsFunction(right) ? Result.Match(ExtendsParameters(inferred, parameters, right['parameters']), inferred => ExtendsReturnType(inferred, returnType, right['returnType']), () => Result.ExtendsFalse()) // 'not-a-parameter-match'
15
+ : Result.ExtendsFalse() // 'not-a-function'
16
+ );
16
17
  }
@@ -21,7 +21,7 @@ export type TTryRestInferable<Type extends TSchema, Result extends TInferable |
21
21
  export declare function TryRestInferable<Type extends TSchema>(type: Type): TTryRestInferable<Type>;
22
22
  export type TTryInferable<Type extends TSchema, Result extends TInferable | undefined = (Type extends TInfer<infer Name extends string, infer Type extends TSchema> ? TInferable<Name, Type> : undefined)> = Result;
23
23
  export declare function TryInferable<Type extends TSchema>(type: Type): TTryInferable<Type>;
24
- type TryInferResults<Rest extends TSchema[], Right extends TSchema, Result extends TSchema[] = []> = (Rest extends [infer Head extends TSchema, ...infer Tail extends TSchema[]] ? TExtendsLeft<{}, Head, Right> extends Result.TExtendsTrueLike<TProperties> ? TryInferResults<Tail, Right, [...Result, Head]> : undefined : Result);
24
+ type TryInferResults<Rest extends TSchema[], Right extends TSchema, Result extends TSchema[] = []> = (Rest extends [infer Head extends TSchema, ...infer Tail extends TSchema[]] ? TExtendsLeft<{}, Head, Right> extends Result.TExtendsTrueLike ? TryInferResults<Tail, Right, [...Result, Head]> : undefined : Result);
25
25
  declare function TryInferResults<Rest extends TSchema[], Right extends TSchema>(rest: [...Rest], right: Right, result?: TSchema[]): TryInferResults<Rest, Right>;
26
26
  export type TInferTupleResult<Inferred extends TProperties, Name extends string, Left extends TSchema[], Right extends TSchema, Results extends TSchema[] | undefined = TryInferResults<Left, Right>> = (Results extends [...infer Results extends TSchema[]] ? Result.TExtendsTrue<Memory.TAssign<Inferred, {
27
27
  [_ in Name]: TTuple<Results>;
@@ -2,10 +2,6 @@
2
2
  import { Unreachable } from '../../system/unreachable/index.mjs';
3
3
  import { Memory } from '../../system/memory/index.mjs';
4
4
  import { Guard } from '../../guard/index.mjs';
5
- // ----------------------------------------------------------------------------
6
- // Schematics
7
- // ----------------------------------------------------------------------------
8
- import { IsSchema } from '../types/schema.mjs';
9
5
  import { IsArray } from '../types/array.mjs';
10
6
  import { IsUnknown } from '../types/unknown.mjs';
11
7
  import { Tuple } from '../types/tuple.mjs';
@@ -49,14 +45,7 @@ export function TryInferable(type) {
49
45
  undefined);
50
46
  }
51
47
  function TryInferResults(rest, right, result = []) {
52
- const [head, ...tail] = rest;
53
- return (IsSchema(head)
54
- ? (() => {
55
- const check = ExtendsLeft({}, head, right);
56
- return Result.IsExtendsTrueLike(check)
57
- ? TryInferResults(tail, right, [...result, head])
58
- : undefined;
59
- })() : result);
48
+ return Result.TakeLeft(rest, (head, tail) => Result.Match(ExtendsLeft({}, head, right), () => TryInferResults(tail, right, [...result, head]), () => undefined), () => result);
60
49
  }
61
50
  export function InferTupleResult(inferred, name, left, right) {
62
51
  const results = TryInferResults(left, right);
@@ -19,12 +19,9 @@ function ExtendsPropertyOptional(inferred, left, right) {
19
19
  function ExtendsProperty(inferred, left, right) {
20
20
  return (
21
21
  // Right TInfer<TNever> is TExtendsFalse
22
- IsInfer(right) && IsNever(right.extends) ? Result.ExtendsFalse() : (() => {
23
- const check = ExtendsLeft(inferred, left, right);
24
- return (Result.IsExtendsTrueLike(check)
25
- ? ExtendsPropertyOptional(check.inferred, left, right)
26
- : Result.ExtendsFalse());
27
- })());
22
+ (IsInfer(right) && IsNever(right.extends))
23
+ ? Result.ExtendsFalse()
24
+ : Result.Match(ExtendsLeft(inferred, left, right), inferred => ExtendsPropertyOptional(inferred, left, right), () => Result.ExtendsFalse()));
28
25
  }
29
26
  function ExtractInferredProperties(keys, properties) {
30
27
  return keys.reduce((result, key) => {
@@ -1,6 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { IsInfer } from '../types/infer.mjs';
3
- import { IsSchema } from '../types/schema.mjs';
4
3
  import { IsOptional } from '../types/_optional.mjs';
5
4
  import { ExtendsLeft } from './extends-left.mjs';
6
5
  import * as Result from './result.mjs';
@@ -12,25 +11,19 @@ function ParameterCompare(inferred, left, leftRest, right, rightRest) {
12
11
  const isRightOptional = IsOptional(right);
13
12
  const check = ExtendsLeft(inferred, checkLeft, checkRight);
14
13
  return (!isLeftOptional && isRightOptional
15
- ? Result.ExtendsFalse()
14
+ ? Result.ExtendsFalse() // 'fail: left-required-but-right-is-optional'
16
15
  : Result.IsExtendsTrueLike(check)
17
16
  ? ExtendsParameters(check.inferred, leftRest, rightRest)
18
- : Result.ExtendsFalse());
17
+ : Result.ExtendsFalse() // 'fail: left-and-right-did-not-match'
18
+ );
19
19
  }
20
20
  function ParameterRight(inferred, left, leftRest, rightRest) {
21
- const [head, ...tail] = rightRest;
22
- return (IsSchema(head)
23
- ? ParameterCompare(inferred, left, leftRest, head, tail)
24
- : IsOptional(left) // 'right-did-not-have-enough-elements'
25
- ? Result.ExtendsTrue(inferred) // 'ok: left was optional'
26
- : Result.ExtendsFalse() // 'fail: left was required'
27
- );
21
+ return Result.TakeLeft(rightRest, (head, tail) => ParameterCompare(inferred, left, leftRest, head, tail), () => IsOptional(left) // 'right-did-not-have-enough-elements'
22
+ ? Result.ExtendsTrue(inferred) // 'ok: left was optional'
23
+ : Result.ExtendsFalse()); // 'fail: left was required'
28
24
  }
29
25
  function ParametersLeft(inferred, left, rightRest) {
30
- const [head, ...tail] = left;
31
- return (IsSchema(head)
32
- ? ParameterRight(inferred, head, tail, rightRest)
33
- : Result.ExtendsTrue(inferred));
26
+ return Result.TakeLeft(left, (head, tail) => ParameterRight(inferred, head, tail, rightRest), () => Result.ExtendsTrue(inferred)); // 'ok: no-more-elements-in-left'
34
27
  }
35
28
  export function ExtendsParameters(inferred, left, right) {
36
29
  return ParametersLeft(inferred, left, right);
@@ -19,3 +19,9 @@ export declare function ExtendsFalse(): TExtendsFalse;
19
19
  export declare function IsExtendsFalse(value: unknown): value is TExtendsFalse;
20
20
  export type TExtendsTrueLike<Inferred extends TProperties = TProperties> = TExtendsUnion<Inferred> | TExtendsTrue<Inferred>;
21
21
  export declare function IsExtendsTrueLike(value: unknown): value is TExtendsTrueLike;
22
+ export type MatchTrueLike = (inferred: TProperties) => unknown;
23
+ export type MatchFalse = () => unknown;
24
+ export declare function Match(result: TResult, true_: MatchTrueLike, false_: MatchFalse): unknown;
25
+ export type TakeLeftTrue<T> = (left: T, right: T[]) => unknown;
26
+ export type TakeLeftFalse = () => unknown;
27
+ export declare function TakeLeft<T>(array: T[], true_: TakeLeftTrue<T>, false_: TakeLeftFalse): unknown;
@@ -31,3 +31,9 @@ export function IsExtendsFalse(value) {
31
31
  export function IsExtendsTrueLike(value) {
32
32
  return IsExtendsUnion(value) || IsExtendsTrue(value);
33
33
  }
34
+ export function Match(result, true_, false_) {
35
+ return IsExtendsTrueLike(result) ? true_(result.inferred) : false_();
36
+ }
37
+ export function TakeLeft(array, true_, false_) {
38
+ return Guard.IsEqual(array.length, 0) ? false_() : true_(array[0], array.slice(1));
39
+ }
@@ -11,13 +11,13 @@ type TReverse<Types extends TSchema[], Result extends TSchema[] = []> = (Types e
11
11
  type TApplyReverse<Types extends TSchema[], Reversed extends boolean> = Reversed extends true ? TReverse<Types> : Types;
12
12
  type TReversed<Types extends TSchema[], First extends TSchema | undefined = Types extends [infer Left extends TSchema, ...infer _ extends TSchema[]] ? Left : undefined, Inferable extends TSchema | undefined = First extends TSchema ? TTryRestInferable<First> : undefined, Result extends boolean = Inferable extends TSchema ? true : false> = Result;
13
13
  type TElementsCompare<Inferred extends TProperties, Reversed extends boolean, Left extends TSchema, LeftRest extends TSchema[], Right extends TSchema, RightRest extends TSchema[]> = (TExtendsLeft<Inferred, Left, Right> extends Result.TExtendsTrueLike<infer CheckInferred extends TProperties> ? TElements<CheckInferred, Reversed, LeftRest, RightRest> : Result.TExtendsFalse);
14
- type TElementsLeft<Inferred extends TProperties, Reversed extends boolean, LeftRest extends TSchema[], Right extends TSchema, RightRest extends TSchema[], Inferable extends TInferable | undefined = TTryRestInferable<Right>> = (Inferable extends TInferable<infer Name extends string, infer Type extends TSchema> ? TInferTupleResult<Inferred, Name, TApplyReverse<LeftRest, Reversed>, Type> : LeftRest extends [infer Head extends TSchema, ...infer Tail extends TSchema[]] ? TElementsCompare<Inferred, Reversed, Head, Tail, Right, RightRest> : Result.TExtendsFalse);
14
+ type TElementsLeft<Inferred extends TProperties, Reversed extends boolean, LeftRest extends TSchema[], Right extends TSchema, RightRest extends TSchema[], Inferable extends TInferable | undefined = TTryRestInferable<Right>> = (Inferable extends TInferable ? TInferTupleResult<Inferred, Inferable['name'], TApplyReverse<LeftRest, Reversed>, Inferable['type']> : LeftRest extends [infer Head extends TSchema, ...infer Tail extends TSchema[]] ? TElementsCompare<Inferred, Reversed, Head, Tail, Right, RightRest> : Result.TExtendsFalse);
15
15
  type TElementsRight<Inferred extends TProperties, Reversed extends boolean, LeftRest extends TSchema[], RightRest extends TSchema[]> = (RightRest extends [infer Head extends TSchema, ...infer Tail extends TSchema[]] ? TElementsLeft<Inferred, Reversed, LeftRest, Head, Tail> : LeftRest['length'] extends 0 ? Result.TExtendsTrue<Inferred> : Result.TExtendsFalse);
16
16
  type TElements<Inferred extends TProperties, Reversed extends boolean, LeftRest extends TSchema[], RightRest extends TSchema[]> = TElementsRight<Inferred, Reversed, LeftRest, RightRest>;
17
17
  type TExtendsTupleToTuple<Inferred extends TProperties, Left extends TSchema[], Right extends TSchema[], InstantiatedRight extends TSchema[] = TInstantiateElements<Inferred, {
18
18
  callstack: [];
19
19
  }, Right>, Reversed extends boolean = TReversed<InstantiatedRight>> = TElements<Inferred, Reversed, TApplyReverse<Left, Reversed>, TApplyReverse<InstantiatedRight, Reversed>>;
20
- type TExtendsTupleToArray<Inferred extends TProperties, Left extends TSchema[], Right extends TSchema, Inferrable extends TInferable | undefined = TTryInferable<Right>> = (Inferrable extends TInferable<infer Name extends string, infer Type extends TSchema> ? TInferUnionResult<Inferred, Name, Left, Type> : Left extends [infer Head extends TSchema, ...infer Tail extends TSchema[]] ? TExtendsLeft<Inferred, Head, Right> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsTupleToArray<Inferred, Tail, Right> : Result.TExtendsFalse : Result.TExtendsTrue<Inferred>);
20
+ type TExtendsTupleToArray<Inferred extends TProperties, Left extends TSchema[], Right extends TSchema, Inferrable extends TInferable | undefined = TTryInferable<Right>> = (Inferrable extends TInferable ? TInferUnionResult<Inferred, Inferrable['name'], Left, Inferrable['type']> : Left extends [infer Head extends TSchema, ...infer Tail extends TSchema[]] ? TExtendsLeft<Inferred, Head, Right> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsTupleToArray<Inferred, Tail, Right> : Result.TExtendsFalse : Result.TExtendsTrue<Inferred>);
21
21
  export type TExtendsTuple<Inferred extends TProperties, Left extends TSchema[], Right extends TSchema, InstantiatedLeft extends TSchema[] = TInstantiateElements<Inferred, {
22
22
  callstack: [];
23
23
  }, Left>> = (Right extends TTuple<infer Types extends TSchema[]> ? TExtendsTupleToTuple<Inferred, InstantiatedLeft, Types> : Right extends TArray<infer Type extends TSchema> ? TExtendsTupleToArray<Inferred, InstantiatedLeft, Type> : TExtendsRight<Inferred, TTuple<InstantiatedLeft>, Right>);
@@ -31,23 +31,16 @@ function ElementsCompare(inferred, reversed, left, leftRest, right, rightRest) {
31
31
  }
32
32
  function ElementsLeft(inferred, reversed, leftRest, right, rightRest) {
33
33
  const inferable = TryRestInferable(right);
34
- return (IsInferable(inferable)
35
- ? InferTupleResult(inferred, inferable.name, ApplyReverse(leftRest, reversed), inferable.type)
36
- : (() => {
37
- const [head, ...tail] = leftRest;
38
- return IsSchema(head)
39
- ? ElementsCompare(inferred, reversed, head, tail, right, rightRest)
40
- : Result.ExtendsFalse();
41
- })());
34
+ return (
35
+ // Rest Inferrable Right Means we delegate to TInferTupleResult to Generate a Result
36
+ IsInferable(inferable)
37
+ ? InferTupleResult(inferred, inferable['name'], ApplyReverse(leftRest, reversed), inferable['type'])
38
+ : Result.TakeLeft(leftRest, (head, tail) => ElementsCompare(inferred, reversed, head, tail, right, rightRest), () => Result.ExtendsFalse()));
42
39
  }
43
40
  function ElementsRight(inferred, reversed, leftRest, rightRest) {
44
- const [head, ...tail] = rightRest;
45
- return (IsSchema(head)
46
- ? ElementsLeft(inferred, reversed, leftRest, head, tail)
47
- : Guard.IsEqual(leftRest.length, 0)
48
- ? Result.ExtendsTrue(inferred) // 'Ok: right-empty-and-left-empty'
49
- : Result.ExtendsFalse() // 'Fail: right-empty-and-left-not-empty'
50
- );
41
+ return Result.TakeLeft(rightRest, (head, tail) => ElementsLeft(inferred, reversed, leftRest, head, tail), () => Guard.IsEqual(leftRest.length, 0)
42
+ ? Result.ExtendsTrue(inferred) // 'Ok: right-empty-and-left-empty'
43
+ : Result.ExtendsFalse()); // 'Fail: right-empty-and-left-not-empty'
51
44
  }
52
45
  function Elements(inferred, reversed, leftRest, rightRest) {
53
46
  return ElementsRight(inferred, reversed, leftRest, rightRest);
@@ -60,19 +53,8 @@ function ExtendsTupleToTuple(inferred, left, right) {
60
53
  function ExtendsTupleToArray(inferred, left, right) {
61
54
  const inferrable = TryInferable(right);
62
55
  return (IsInferable(inferrable)
63
- // @ts-ignore 4.9.5 fails to see `type` property on inferrable
64
- ? InferUnionResult(inferred, inferrable.name, left, inferrable.type)
65
- : (() => {
66
- const [head, ...tail] = left;
67
- return IsSchema(head)
68
- ? (() => {
69
- const check = ExtendsLeft(inferred, head, right);
70
- return Result.IsExtendsTrueLike(check)
71
- ? ExtendsTupleToArray(check.inferred, tail, right)
72
- : Result.ExtendsFalse();
73
- })()
74
- : Result.ExtendsTrue(inferred);
75
- })());
56
+ ? InferUnionResult(inferred, inferrable['name'], left, inferrable['type'])
57
+ : Result.TakeLeft(left, (head, tail) => Result.Match(ExtendsLeft(inferred, head, right), inferred => ExtendsTupleToArray(inferred, tail, right), () => Result.ExtendsFalse()), () => Result.ExtendsTrue(inferred)));
76
58
  }
77
59
  export function ExtendsTuple(inferred, left, right) {
78
60
  const instantiatedLeft = InstantiateElements(inferred, { callstack: [] }, left);
@@ -1,5 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- import { IsSchema } from '../types/schema.mjs';
3
2
  import { IsUnion } from '../types/union.mjs';
4
3
  import { ExtendsLeft } from './extends-left.mjs';
5
4
  import * as Result from './result.mjs';
@@ -8,22 +7,10 @@ import * as Result from './result.mjs';
8
7
  // ----------------------------------------------------------------------------
9
8
  import { IsInferable, TryInferable, InferUnionResult } from './inference.mjs';
10
9
  function ExtendsUnionSome(inferred, type, unionTypes) {
11
- const [head, ...tail] = unionTypes;
12
- return (IsSchema(head) ? (() => {
13
- const check = ExtendsLeft(inferred, type, head);
14
- return Result.IsExtendsTrueLike(check)
15
- ? Result.ExtendsTrue(check.inferred)
16
- : ExtendsUnionSome(inferred, type, tail);
17
- })() : Result.ExtendsFalse());
10
+ return Result.TakeLeft(unionTypes, (head, tail) => Result.Match(ExtendsLeft(inferred, type, head), inferred => Result.ExtendsTrue(inferred), () => ExtendsUnionSome(inferred, type, tail)), () => Result.ExtendsFalse());
18
11
  }
19
12
  function ExtendsUnionLeft(inferred, left, right) {
20
- const [head, ...tail] = left;
21
- return (IsSchema(head) ? (() => {
22
- const check = ExtendsUnionSome(inferred, head, right);
23
- return Result.IsExtendsTrueLike(check)
24
- ? ExtendsUnionLeft(check.inferred, tail, right)
25
- : Result.ExtendsFalse();
26
- })() : Result.ExtendsTrue(inferred));
13
+ return Result.TakeLeft(left, (head, tail) => Result.Match(ExtendsUnionSome(inferred, head, right), inferred => ExtendsUnionLeft(inferred, tail, right), () => Result.ExtendsFalse()), () => Result.ExtendsTrue(inferred));
27
14
  }
28
15
  export function ExtendsUnion(inferred, left, right) {
29
16
  const inferrable = TryInferable(right);
@@ -164,7 +164,7 @@ export type TExtendsMapping<Input extends [unknown, unknown, unknown, unknown, u
164
164
  export declare function ExtendsMapping(input: [unknown, unknown, unknown, unknown, unknown, unknown] | []): unknown;
165
165
  export type TBaseMapping<Input extends [unknown, unknown, unknown] | unknown> = (Input extends ['(', infer Type extends T.TSchema, ')'] ? Type : Input extends infer Type extends T.TSchema ? Type : never);
166
166
  export declare function BaseMapping(input: [unknown, unknown, unknown] | unknown): unknown;
167
- type TFactorIndexArray<Type extends T.TSchema, IndexArray extends unknown[]> = (IndexArray extends [...infer Left extends unknown[], infer Right extends T.TSchema[]] ? (Right extends [infer Indexer extends T.TSchema] ? C.TIndexDeferred<TFactorIndexArray<Type, Left>, Indexer> : Right extends [] ? T.TArray<TFactorIndexArray<Type, Left>> : T.TNever) : Type);
167
+ type TFactorIndexArray<Type extends T.TSchema, IndexArray extends unknown[]> = (IndexArray extends [infer Left extends T.TSchema[], ...infer Right extends unknown[]] ? (Left extends [infer Indexer extends T.TSchema] ? TFactorIndexArray<C.TIndexDeferred<Type, Indexer>, Right> : Left extends [] ? TFactorIndexArray<T.TArray<Type>, Right> : T.TNever) : Type);
168
168
  type TFactorExtends<Type extends T.TSchema, Extends extends unknown[]> = (Extends extends [infer Right extends T.TSchema, infer True extends T.TSchema, infer False extends T.TSchema] ? C.TConditionalDeferred<Type, Right, True, False> : Type);
169
169
  export type TFactorMapping<Input extends [unknown, unknown, unknown, unknown]> = (Input extends [infer KeyOf extends boolean, infer Type extends T.TSchema, infer IndexArray extends unknown[], infer Extend extends unknown[]] ? KeyOf extends true ? TFactorExtends<C.TKeyOfDeferred<TFactorIndexArray<Type, IndexArray>>, Extend> : TFactorExtends<TFactorIndexArray<Type, IndexArray>, Extend> : never);
170
170
  export declare function FactorMapping(input: [unknown, unknown, unknown, unknown]): unknown;
@@ -188,10 +188,10 @@ export function BaseMapping(input) {
188
188
  // deno-coverage-ignore-start
189
189
  // ...
190
190
  const FactorIndexArray = (Type, indexArray) => {
191
- return indexArray.reduceRight((result, right) => {
192
- const _right = right;
193
- return (Guard.IsEqual(_right.length, 1) ? C.IndexDeferred(result, _right[0]) :
194
- Guard.IsEqual(_right.length, 0) ? T.Array(result) :
191
+ return indexArray.reduce((result, left) => {
192
+ const _left = left;
193
+ return (Guard.IsEqual(_left.length, 1) ? C.IndexDeferred(result, _left[0]) :
194
+ Guard.IsEqual(_left.length, 0) ? T.Array(result) :
195
195
  Unreachable());
196
196
  }, Type);
197
197
  };
@@ -2,9 +2,30 @@
2
2
  import { Guard } from '../../guard/index.mjs';
3
3
  import { FromType } from './from-type.mjs';
4
4
  import { Callback } from './callback.mjs';
5
- export function FromRef(direction, context, type, value) {
6
- value = Guard.HasPropertyKey(context, type.$ref)
5
+ // ------------------------------------------------------------------
6
+ // ResolveRef
7
+ // ------------------------------------------------------------------
8
+ function ResolveRef(direction, context, type, value) {
9
+ return Guard.HasPropertyKey(context, type.$ref)
7
10
  ? FromType(direction, context, context[type.$ref], value)
8
11
  : value;
9
- return Callback(direction, context, type, value);
12
+ }
13
+ // ------------------------------------------------------------------
14
+ // FromRef
15
+ //
16
+ // Decode and Encode apply the Callback and the referenced type's
17
+ // codec pipeline in opposite orders, since the two operations are
18
+ // inverses of each other.
19
+ //
20
+ // Decode: referenced type resolves first, Callback runs after.
21
+ // wire value -> resolve $ref -> Callback -> decoded value
22
+ //
23
+ // Encode: Callback runs first, referenced type resolves after.
24
+ // encoded value -> Callback -> resolve $ref -> wire value
25
+ //
26
+ // ------------------------------------------------------------------
27
+ export function FromRef(direction, context, type, value) {
28
+ return Guard.IsEqual(direction, 'Decode')
29
+ ? Callback(direction, context, type, ResolveRef(direction, context, type, value))
30
+ : ResolveRef(direction, context, type, Callback(direction, context, type, value));
10
31
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
4
- "version": "1.1.12",
4
+ "version": "1.1.13",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"