@sinclair/typebox 0.34.25 → 0.34.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/parser/runtime/guard.d.ts +12 -6
- package/build/cjs/parser/runtime/guard.js +31 -20
- package/build/cjs/parser/runtime/module.d.ts +2 -2
- package/build/cjs/parser/runtime/module.js +4 -4
- package/build/cjs/parser/runtime/parse.d.ts +8 -8
- package/build/cjs/parser/runtime/parse.js +71 -43
- package/build/cjs/parser/runtime/types.d.ts +56 -26
- package/build/cjs/parser/runtime/types.js +34 -8
- package/build/cjs/parser/static/parse.d.ts +6 -3
- package/build/cjs/parser/static/types.d.ts +41 -18
- package/build/cjs/syntax/runtime.d.ts +2 -2
- package/build/cjs/syntax/runtime.js +13 -9
- package/build/cjs/syntax/static.d.ts +8 -5
- package/build/cjs/syntax/syntax.d.ts +2 -4
- package/build/cjs/syntax/syntax.js +1 -8
- package/build/cjs/type/module/compute.d.ts +16 -14
- package/build/cjs/type/module/compute.js +51 -40
- package/build/cjs/type/static/static.d.ts +14 -10
- package/build/cjs/value/transform/decode.js +3 -5
- package/build/cjs/value/transform/encode.js +3 -5
- package/build/cjs/value/transform/has.js +8 -0
- package/build/esm/parser/runtime/guard.d.mts +12 -6
- package/build/esm/parser/runtime/guard.mjs +26 -18
- package/build/esm/parser/runtime/module.d.mts +2 -2
- package/build/esm/parser/runtime/module.mjs +4 -4
- package/build/esm/parser/runtime/parse.d.mts +8 -8
- package/build/esm/parser/runtime/parse.mjs +71 -43
- package/build/esm/parser/runtime/types.d.mts +56 -26
- package/build/esm/parser/runtime/types.mjs +29 -6
- package/build/esm/parser/static/parse.d.mts +6 -3
- package/build/esm/parser/static/types.d.mts +41 -18
- package/build/esm/syntax/runtime.d.mts +2 -2
- package/build/esm/syntax/runtime.mjs +13 -9
- package/build/esm/syntax/static.d.mts +8 -5
- package/build/esm/syntax/syntax.d.mts +2 -4
- package/build/esm/syntax/syntax.mjs +1 -8
- package/build/esm/type/module/compute.d.mts +16 -14
- package/build/esm/type/module/compute.mjs +52 -41
- package/build/esm/type/static/static.d.mts +14 -10
- package/build/esm/value/transform/decode.mjs +3 -5
- package/build/esm/value/transform/encode.mjs +3 -5
- package/build/esm/value/transform/has.mjs +8 -0
- package/package.json +1 -1
|
@@ -56,15 +56,15 @@ const GenericArgumentList = Runtime.Union([
|
|
|
56
56
|
// GenericArguments
|
|
57
57
|
// ------------------------------------------------------------------
|
|
58
58
|
// prettier-ignore
|
|
59
|
-
const GenericArgumentsContext = (args) => {
|
|
59
|
+
const GenericArgumentsContext = (args, context) => {
|
|
60
60
|
return args.reduce((result, arg, index) => {
|
|
61
61
|
return { ...result, [arg]: t.Argument(index) };
|
|
62
|
-
},
|
|
62
|
+
}, context);
|
|
63
63
|
};
|
|
64
64
|
// prettier-ignore
|
|
65
|
-
const GenericArgumentsMapping = (results) => {
|
|
65
|
+
const GenericArgumentsMapping = (results, context) => {
|
|
66
66
|
return results.length === 3
|
|
67
|
-
? GenericArgumentsContext(results[1])
|
|
67
|
+
? GenericArgumentsContext(results[1], context)
|
|
68
68
|
: {};
|
|
69
69
|
};
|
|
70
70
|
// prettier-ignore
|
|
@@ -72,7 +72,7 @@ const GenericArguments = Runtime.Tuple([
|
|
|
72
72
|
Runtime.Const(LAngle),
|
|
73
73
|
Runtime.Ref('GenericArgumentList'),
|
|
74
74
|
Runtime.Const(RAngle),
|
|
75
|
-
], results => GenericArgumentsMapping(results));
|
|
75
|
+
], (results, context) => GenericArgumentsMapping(results, context));
|
|
76
76
|
// ------------------------------------------------------------------
|
|
77
77
|
// GenericReference
|
|
78
78
|
// ------------------------------------------------------------------
|
|
@@ -282,7 +282,11 @@ const Expr = Runtime.Tuple([
|
|
|
282
282
|
// ------------------------------------------------------------------
|
|
283
283
|
// Type
|
|
284
284
|
// ------------------------------------------------------------------
|
|
285
|
-
|
|
285
|
+
// prettier-ignore
|
|
286
|
+
const Type = Runtime.Union([
|
|
287
|
+
Runtime.Context(Runtime.Ref('GenericArguments'), Runtime.Ref('Expr')),
|
|
288
|
+
Runtime.Ref('Expr')
|
|
289
|
+
]);
|
|
286
290
|
// ------------------------------------------------------------------
|
|
287
291
|
// Properties
|
|
288
292
|
// ------------------------------------------------------------------
|
|
@@ -649,12 +653,12 @@ const Uint8Array = Runtime.Const('Uint8Array', Runtime.As(t.Uint8Array()));
|
|
|
649
653
|
// prettier-ignore
|
|
650
654
|
export const Module = new Runtime.Module({
|
|
651
655
|
// ----------------------------------------------------------------
|
|
652
|
-
//
|
|
656
|
+
// Generics
|
|
653
657
|
// ----------------------------------------------------------------
|
|
654
658
|
GenericArgumentList,
|
|
655
659
|
GenericArguments,
|
|
656
660
|
// ----------------------------------------------------------------
|
|
657
|
-
// Type
|
|
661
|
+
// Type
|
|
658
662
|
// ----------------------------------------------------------------
|
|
659
663
|
Literal,
|
|
660
664
|
Keyword,
|
|
@@ -667,7 +671,7 @@ export const Module = new Runtime.Module({
|
|
|
667
671
|
ExprTerm,
|
|
668
672
|
ExprTail,
|
|
669
673
|
Expr,
|
|
670
|
-
Type,
|
|
674
|
+
Type,
|
|
671
675
|
PropertyKey,
|
|
672
676
|
Readonly,
|
|
673
677
|
Optional,
|
|
@@ -52,13 +52,13 @@ type GenericArgumentList = Static.Union<[
|
|
|
52
52
|
Static.Tuple<[Static.Ident]>,
|
|
53
53
|
Static.Tuple<[]>
|
|
54
54
|
], GenericArgumentListMapping>;
|
|
55
|
-
type GenericArgumentsContext<Args extends string[], Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Result & {
|
|
55
|
+
type GenericArgumentsContext<Args extends string[], Context extends t.TProperties, Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Context, Result & {
|
|
56
56
|
[_ in Right]: t.TArgument<Left['length']>;
|
|
57
|
-
}> : t.Evaluate<Result>);
|
|
57
|
+
}> : t.Evaluate<Result & Context>);
|
|
58
58
|
interface GenericArgumentsMapping extends Static.IMapping {
|
|
59
|
-
output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? GenericArgumentsContext<Args> : never;
|
|
59
|
+
output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? this['context'] extends infer Context extends t.TProperties ? GenericArgumentsContext<Args, Context> : never : never;
|
|
60
60
|
}
|
|
61
|
-
|
|
61
|
+
type GenericArguments = Static.Tuple<[
|
|
62
62
|
Static.Const<LAngle>,
|
|
63
63
|
GenericArgumentList,
|
|
64
64
|
Static.Const<RAngle>
|
|
@@ -204,7 +204,10 @@ type Expr = Static.Tuple<[
|
|
|
204
204
|
ExprTerm,
|
|
205
205
|
ExprTail
|
|
206
206
|
], ExprBinaryMapping>;
|
|
207
|
-
export type Type =
|
|
207
|
+
export type Type = Static.Union<[
|
|
208
|
+
Static.Context<GenericArguments, Expr>,
|
|
209
|
+
Expr
|
|
210
|
+
]>;
|
|
208
211
|
interface PropertyKeyStringMapping extends Static.IMapping {
|
|
209
212
|
output: this['input'];
|
|
210
213
|
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import * as t from '../type/index.mjs';
|
|
2
2
|
import { Static } from '../parser/index.mjs';
|
|
3
|
-
import { Type
|
|
4
|
-
type TParseSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<GenericArguments, Code, {}> extends [infer Args extends t.TProperties, infer Rest extends string] ? Static.Parse<Type, Rest, Context & Args> : Static.Parse<Type, Code, Context>);
|
|
3
|
+
import { Type } from './static.mjs';
|
|
5
4
|
/** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
6
5
|
export declare function NoInfer<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema;
|
|
7
6
|
/** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
8
7
|
export declare function NoInfer<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema;
|
|
9
8
|
/** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
|
|
10
|
-
export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (
|
|
9
|
+
export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<Type, Code, Context> extends [infer Type extends t.TSchema, string] ? Type : t.TNever);
|
|
11
10
|
/** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
|
|
12
11
|
export declare function Syntax<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>;
|
|
13
12
|
/** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
|
|
@@ -28,4 +27,3 @@ export declare function ParseOnly<Context extends Record<PropertyKey, t.TSchema>
|
|
|
28
27
|
* @deprecated Use NoInfer() function
|
|
29
28
|
*/
|
|
30
29
|
export declare function ParseOnly<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema | undefined;
|
|
31
|
-
export {};
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import * as t from '../type/index.mjs';
|
|
2
2
|
import { Module } from './runtime.mjs';
|
|
3
|
-
// prettier-ignore
|
|
4
|
-
function ParseSyntax(context, code) {
|
|
5
|
-
const results = Module.Parse('GenericArguments', code, {}); // [ArgumentContext, Rest]
|
|
6
|
-
return (results.length === 2
|
|
7
|
-
? Module.Parse('Type', results[1], { ...context, ...results[0] })
|
|
8
|
-
: Module.Parse('Type', code, context));
|
|
9
|
-
}
|
|
10
3
|
/** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
11
4
|
// prettier-ignore
|
|
12
5
|
export function NoInfer(...args) {
|
|
13
6
|
const withContext = typeof args[0] === 'string' ? false : true;
|
|
14
7
|
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
|
|
15
|
-
const result =
|
|
8
|
+
const result = Module.Parse('Type', code, context)[0];
|
|
16
9
|
return t.KindGuard.IsSchema(result)
|
|
17
10
|
? t.CloneType(result, options)
|
|
18
11
|
: t.Never(options);
|
|
@@ -20,11 +20,12 @@ import { TPartial } from '../partial/index.mjs';
|
|
|
20
20
|
import { type TReadonly } from '../readonly/index.mjs';
|
|
21
21
|
import { type TRecordOrObject, type TRecord } from '../record/index.mjs';
|
|
22
22
|
import { type TRef } from '../ref/index.mjs';
|
|
23
|
-
import { TRequired } from '../required/index.mjs';
|
|
23
|
+
import { type TRequired } from '../required/index.mjs';
|
|
24
|
+
import { type TTransform } from '../transform/index.mjs';
|
|
24
25
|
import { type TTuple } from '../tuple/index.mjs';
|
|
25
26
|
import { type TUnion, type TUnionEvaluated } from '../union/index.mjs';
|
|
26
|
-
type
|
|
27
|
-
type
|
|
27
|
+
type TDereferenceParameters<ModuleProperties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TRef<infer Key extends string> ? TDereferenceParameters<ModuleProperties, Right, [...Result, TDereference<ModuleProperties, Key>]> : TDereferenceParameters<ModuleProperties, Right, [...Result, TFromType<ModuleProperties, Left>]> : Result);
|
|
28
|
+
type TDereference<ModuleProperties extends TProperties, Ref extends string, Result extends TSchema = (Ref extends keyof ModuleProperties ? ModuleProperties[Ref] extends TRef<infer Ref2 extends string> ? TDereference<ModuleProperties, Ref2> : TFromType<ModuleProperties, ModuleProperties[Ref]> : TNever)> = Result;
|
|
28
29
|
type TFromAwaited<Parameters extends TSchema[]> = (Parameters extends [infer T0 extends TSchema] ? TAwaited<T0> : never);
|
|
29
30
|
type TFromIndex<Parameters extends TSchema[]> = (Parameters extends [infer T0 extends TSchema, infer T1 extends TSchema] ? TIndex<T0, TIndexPropertyKeys<T1>> extends infer Result extends TSchema ? Result : never : never);
|
|
30
31
|
type TFromKeyOf<Parameters extends TSchema[]> = (Parameters extends [infer T0 extends TSchema] ? TKeyOf<T0> : never);
|
|
@@ -32,21 +33,22 @@ type TFromPartial<Parameters extends TSchema[]> = (Parameters extends [infer T0
|
|
|
32
33
|
type TFromOmit<Parameters extends TSchema[]> = (Parameters extends [infer T0 extends TSchema, infer T1 extends TSchema] ? TOmit<T0, T1> : never);
|
|
33
34
|
type TFromPick<Parameters extends TSchema[]> = (Parameters extends [infer T0 extends TSchema, infer T1 extends TSchema] ? TPick<T0, T1> : never);
|
|
34
35
|
type TFromRequired<Parameters extends TSchema[]> = (Parameters extends [infer T0 extends TSchema] ? TRequired<T0> : never);
|
|
35
|
-
type TFromComputed<ModuleProperties extends TProperties, Target extends string, Parameters extends TSchema[], Dereferenced extends TSchema[] =
|
|
36
|
+
type TFromComputed<ModuleProperties extends TProperties, Target extends string, Parameters extends TSchema[], Dereferenced extends TSchema[] = TDereferenceParameters<ModuleProperties, Parameters>> = (Target extends 'Awaited' ? TFromAwaited<Dereferenced> : Target extends 'Index' ? TFromIndex<Dereferenced> : Target extends 'KeyOf' ? TFromKeyOf<Dereferenced> : Target extends 'Partial' ? TFromPartial<Dereferenced> : Target extends 'Omit' ? TFromOmit<Dereferenced> : Target extends 'Pick' ? TFromPick<Dereferenced> : Target extends 'Required' ? TFromRequired<Dereferenced> : TNever);
|
|
37
|
+
type TFromArray<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<TArray<TFromType<ModuleProperties, Type>>>);
|
|
38
|
+
type TFromAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TAsyncIterator<TFromType<ModuleProperties, Type>>);
|
|
39
|
+
type TFromConstructor<ModuleProperties extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema> = (TConstructor<TFromTypes<ModuleProperties, Parameters>, TFromType<ModuleProperties, InstanceType>>);
|
|
40
|
+
type TFromFunction<ModuleProperties extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema> = Ensure<Ensure<TFunction<TFromTypes<ModuleProperties, Parameters>, TFromType<ModuleProperties, ReturnType>>>>;
|
|
41
|
+
type TFromIntersect<ModuleProperties extends TProperties, Types extends TSchema[]> = (Ensure<TIntersectEvaluated<TFromTypes<ModuleProperties, Types>>>);
|
|
42
|
+
type TFromIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TIterator<TFromType<ModuleProperties, Type>>);
|
|
36
43
|
type TFromObject<ModuleProperties extends TProperties, Properties extends TProperties> = Ensure<TObject<Evaluate<{
|
|
37
44
|
[Key in keyof Properties]: TFromType<ModuleProperties, Properties[Key]>;
|
|
38
45
|
}>>>;
|
|
39
46
|
type TFromRecord<ModuleProperties extends TProperties, Key extends TSchema, Value extends TSchema, Result extends TSchema = TRecordOrObject<Key, TFromType<ModuleProperties, Value>>> = Result;
|
|
40
|
-
type
|
|
41
|
-
type
|
|
42
|
-
type
|
|
43
|
-
type
|
|
44
|
-
type
|
|
45
|
-
type TFromArray<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<TArray<TFromType<ModuleProperties, Type>>>);
|
|
46
|
-
type TFromAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TAsyncIterator<TFromType<ModuleProperties, Type>>);
|
|
47
|
-
type TFromIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TIterator<TFromType<ModuleProperties, Type>>);
|
|
48
|
-
type TFromRest<ModuleProperties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromRest<ModuleProperties, Right, [...Result, TFromType<ModuleProperties, Left>]> : Result);
|
|
49
|
-
export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TOptional<infer Type extends TSchema> ? TOptional<TFromType<ModuleProperties, Type>> : Type extends TReadonly<infer Type extends TSchema> ? TReadonly<TFromType<ModuleProperties, Type>> : Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TFromRecord<ModuleProperties, Key, Value> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
|
|
47
|
+
type TFromTransform<ModuleProperties extends TProperties, Input extends TSchema, Output extends unknown, Result extends TSchema = Input extends TRef<infer Key extends string> ? TTransform<TDereference<ModuleProperties, Key>, Output> : TTransform<Input, Output>> = Result;
|
|
48
|
+
type TFromTuple<ModuleProperties extends TProperties, Types extends TSchema[]> = (Ensure<TTuple<TFromTypes<ModuleProperties, Types>>>);
|
|
49
|
+
type TFromUnion<ModuleProperties extends TProperties, Types extends TSchema[]> = (Ensure<TUnionEvaluated<TFromTypes<ModuleProperties, Types>>>);
|
|
50
|
+
type TFromTypes<ModuleProperties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<ModuleProperties, Right, [...Result, TFromType<ModuleProperties, Left>]> : Result);
|
|
51
|
+
export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TOptional<infer Type extends TSchema> ? TOptional<TFromType<ModuleProperties, Type>> : Type extends TReadonly<infer Type extends TSchema> ? TReadonly<TFromType<ModuleProperties, Type>> : Type extends TTransform<infer Input extends TSchema, infer Output extends unknown> ? TFromTransform<ModuleProperties, Input, Output> : Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TFromRecord<ModuleProperties, Key, Value> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
|
|
50
52
|
export declare function FromType<ModuleProperties extends TProperties, Type extends TSchema>(moduleProperties: ModuleProperties, type: Type): TFromType<ModuleProperties, Type>;
|
|
51
53
|
export type TComputeType<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TFromType<ModuleProperties, ModuleProperties[Key]> : TNever);
|
|
52
54
|
export declare function ComputeType<ModuleProperties extends TProperties, Key extends PropertyKey>(moduleProperties: ModuleProperties, key: Key): TComputeType<ModuleProperties, Key>;
|
|
@@ -19,24 +19,27 @@ import { RecordValue, RecordPattern } from '../record/index.mjs';
|
|
|
19
19
|
import { Required } from '../required/index.mjs';
|
|
20
20
|
import { Tuple } from '../tuple/index.mjs';
|
|
21
21
|
import { Union } from '../union/index.mjs';
|
|
22
|
-
|
|
22
|
+
// ------------------------------------------------------------------
|
|
23
|
+
// Symbols
|
|
24
|
+
// ------------------------------------------------------------------
|
|
25
|
+
import { TransformKind, OptionalKind, ReadonlyKind } from '../symbols/index.mjs';
|
|
23
26
|
// ------------------------------------------------------------------
|
|
24
27
|
// KindGuard
|
|
25
28
|
// ------------------------------------------------------------------
|
|
26
29
|
import * as KindGuard from '../guard/kind.mjs';
|
|
27
30
|
// prettier-ignore
|
|
28
|
-
function
|
|
31
|
+
function DereferenceParameters(moduleProperties, types) {
|
|
29
32
|
return types.map((type) => {
|
|
30
33
|
return KindGuard.IsRef(type)
|
|
31
|
-
?
|
|
34
|
+
? Dereference(moduleProperties, type.$ref)
|
|
32
35
|
: FromType(moduleProperties, type);
|
|
33
36
|
});
|
|
34
37
|
}
|
|
35
38
|
// prettier-ignore
|
|
36
|
-
function
|
|
39
|
+
function Dereference(moduleProperties, ref) {
|
|
37
40
|
return (ref in moduleProperties
|
|
38
41
|
? KindGuard.IsRef(moduleProperties[ref])
|
|
39
|
-
?
|
|
42
|
+
? Dereference(moduleProperties, moduleProperties[ref].$ref)
|
|
40
43
|
: FromType(moduleProperties, moduleProperties[ref])
|
|
41
44
|
: Never());
|
|
42
45
|
}
|
|
@@ -70,7 +73,7 @@ function FromRequired(parameters) {
|
|
|
70
73
|
}
|
|
71
74
|
// prettier-ignore
|
|
72
75
|
function FromComputed(moduleProperties, target, parameters) {
|
|
73
|
-
const dereferenced =
|
|
76
|
+
const dereferenced = DereferenceParameters(moduleProperties, parameters);
|
|
74
77
|
return (target === 'Awaited' ? FromAwaited(dereferenced) :
|
|
75
78
|
target === 'Index' ? FromIndex(dereferenced) :
|
|
76
79
|
target === 'KeyOf' ? FromKeyOf(dereferenced) :
|
|
@@ -80,6 +83,26 @@ function FromComputed(moduleProperties, target, parameters) {
|
|
|
80
83
|
target === 'Required' ? FromRequired(dereferenced) :
|
|
81
84
|
Never());
|
|
82
85
|
}
|
|
86
|
+
function FromArray(moduleProperties, type) {
|
|
87
|
+
return Array(FromType(moduleProperties, type));
|
|
88
|
+
}
|
|
89
|
+
function FromAsyncIterator(moduleProperties, type) {
|
|
90
|
+
return AsyncIterator(FromType(moduleProperties, type));
|
|
91
|
+
}
|
|
92
|
+
// prettier-ignore
|
|
93
|
+
function FromConstructor(moduleProperties, parameters, instanceType) {
|
|
94
|
+
return Constructor(FromTypes(moduleProperties, parameters), FromType(moduleProperties, instanceType));
|
|
95
|
+
}
|
|
96
|
+
// prettier-ignore
|
|
97
|
+
function FromFunction(moduleProperties, parameters, returnType) {
|
|
98
|
+
return FunctionType(FromTypes(moduleProperties, parameters), FromType(moduleProperties, returnType));
|
|
99
|
+
}
|
|
100
|
+
function FromIntersect(moduleProperties, types) {
|
|
101
|
+
return Intersect(FromTypes(moduleProperties, types));
|
|
102
|
+
}
|
|
103
|
+
function FromIterator(moduleProperties, type) {
|
|
104
|
+
return Iterator(FromType(moduleProperties, type));
|
|
105
|
+
}
|
|
83
106
|
function FromObject(moduleProperties, properties) {
|
|
84
107
|
return Object(globalThis.Object.keys(properties).reduce((result, key) => {
|
|
85
108
|
return { ...result, [key]: FromType(moduleProperties, properties[key]) };
|
|
@@ -93,53 +116,41 @@ function FromRecord(moduleProperties, type) {
|
|
|
93
116
|
return result;
|
|
94
117
|
}
|
|
95
118
|
// prettier-ignore
|
|
96
|
-
function
|
|
97
|
-
return
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function FromFunction(moduleProperties, parameters, returnType) {
|
|
101
|
-
return FunctionType(FromRest(moduleProperties, parameters), FromType(moduleProperties, returnType));
|
|
119
|
+
function FromTransform(moduleProperties, transform) {
|
|
120
|
+
return (KindGuard.IsRef(transform))
|
|
121
|
+
? { ...Dereference(moduleProperties, transform.$ref), [TransformKind]: transform[TransformKind] }
|
|
122
|
+
: transform;
|
|
102
123
|
}
|
|
103
124
|
function FromTuple(moduleProperties, types) {
|
|
104
|
-
return Tuple(
|
|
105
|
-
}
|
|
106
|
-
function FromIntersect(moduleProperties, types) {
|
|
107
|
-
return Intersect(FromRest(moduleProperties, types));
|
|
125
|
+
return Tuple(FromTypes(moduleProperties, types));
|
|
108
126
|
}
|
|
109
127
|
function FromUnion(moduleProperties, types) {
|
|
110
|
-
return Union(
|
|
111
|
-
}
|
|
112
|
-
function FromArray(moduleProperties, type) {
|
|
113
|
-
return Array(FromType(moduleProperties, type));
|
|
114
|
-
}
|
|
115
|
-
function FromAsyncIterator(moduleProperties, type) {
|
|
116
|
-
return AsyncIterator(FromType(moduleProperties, type));
|
|
117
|
-
}
|
|
118
|
-
function FromIterator(moduleProperties, type) {
|
|
119
|
-
return Iterator(FromType(moduleProperties, type));
|
|
128
|
+
return Union(FromTypes(moduleProperties, types));
|
|
120
129
|
}
|
|
121
|
-
function
|
|
130
|
+
function FromTypes(moduleProperties, types) {
|
|
122
131
|
return types.map((type) => FromType(moduleProperties, type));
|
|
123
132
|
}
|
|
124
133
|
// prettier-ignore
|
|
125
134
|
export function FromType(moduleProperties, type) {
|
|
126
135
|
return (
|
|
127
|
-
//
|
|
136
|
+
// Modifiers
|
|
128
137
|
KindGuard.IsOptional(type) ? CreateType(FromType(moduleProperties, Discard(type, [OptionalKind])), type) :
|
|
129
138
|
KindGuard.IsReadonly(type) ? CreateType(FromType(moduleProperties, Discard(type, [ReadonlyKind])), type) :
|
|
130
|
-
//
|
|
131
|
-
KindGuard.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
139
|
+
// Transform
|
|
140
|
+
KindGuard.IsTransform(type) ? CreateType(FromTransform(moduleProperties, type), type) :
|
|
141
|
+
// Types
|
|
142
|
+
KindGuard.IsArray(type) ? CreateType(FromArray(moduleProperties, type.items), type) :
|
|
143
|
+
KindGuard.IsAsyncIterator(type) ? CreateType(FromAsyncIterator(moduleProperties, type.items), type) :
|
|
144
|
+
KindGuard.IsComputed(type) ? CreateType(FromComputed(moduleProperties, type.target, type.parameters)) :
|
|
145
|
+
KindGuard.IsConstructor(type) ? CreateType(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
|
|
146
|
+
KindGuard.IsFunction(type) ? CreateType(FromFunction(moduleProperties, type.parameters, type.returns), type) :
|
|
147
|
+
KindGuard.IsIntersect(type) ? CreateType(FromIntersect(moduleProperties, type.allOf), type) :
|
|
148
|
+
KindGuard.IsIterator(type) ? CreateType(FromIterator(moduleProperties, type.items), type) :
|
|
149
|
+
KindGuard.IsObject(type) ? CreateType(FromObject(moduleProperties, type.properties), type) :
|
|
150
|
+
KindGuard.IsRecord(type) ? CreateType(FromRecord(moduleProperties, type)) :
|
|
151
|
+
KindGuard.IsTuple(type) ? CreateType(FromTuple(moduleProperties, type.items || []), type) :
|
|
152
|
+
KindGuard.IsUnion(type) ? CreateType(FromUnion(moduleProperties, type.anyOf), type) :
|
|
153
|
+
type);
|
|
143
154
|
}
|
|
144
155
|
// prettier-ignore
|
|
145
156
|
export function ComputeType(moduleProperties, key) {
|
|
@@ -7,6 +7,7 @@ import type { TConstructor } from '../constructor/index.mjs';
|
|
|
7
7
|
import type { TEnum } from '../enum/index.mjs';
|
|
8
8
|
import type { TFunction } from '../function/index.mjs';
|
|
9
9
|
import type { TIntersect } from '../intersect/index.mjs';
|
|
10
|
+
import type { TImport } from '../module/index.mjs';
|
|
10
11
|
import type { TIterator } from '../iterator/index.mjs';
|
|
11
12
|
import type { TNot } from '../not/index.mjs';
|
|
12
13
|
import type { TObject, TProperties } from '../object/index.mjs';
|
|
@@ -19,17 +20,20 @@ import type { TUnion } from '../union/index.mjs';
|
|
|
19
20
|
import type { TUnsafe } from '../unsafe/index.mjs';
|
|
20
21
|
import type { TSchema } from '../schema/index.mjs';
|
|
21
22
|
import type { TTransform } from '../transform/index.mjs';
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
import type { TNever } from '../never/index.mjs';
|
|
24
|
+
type TDecodeImport<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TDecodeType<ModuleProperties[Key]> extends infer Type extends TSchema ? Type extends TRef<infer Ref extends string> ? TDecodeImport<ModuleProperties, Ref> : Type : TNever : TNever);
|
|
25
|
+
type TDecodeProperties<Properties extends TProperties> = {
|
|
26
|
+
[Key in keyof Properties]: TDecodeType<Properties[Key]>;
|
|
24
27
|
};
|
|
25
|
-
|
|
26
|
-
export type TDecodeType<
|
|
27
|
-
export type StaticDecodeIsAny<
|
|
28
|
+
type TDecodeTypes<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TDecodeTypes<Right, [...Result, TDecodeType<Left>]> : Result);
|
|
29
|
+
export type TDecodeType<Type extends TSchema> = (Type extends TOptional<infer Type extends TSchema> ? TOptional<TDecodeType<Type>> : Type extends TReadonly<infer Type extends TSchema> ? TReadonly<TDecodeType<Type>> : Type extends TTransform<infer _Input extends TSchema, infer Output> ? TUnsafe<Output> : Type extends TArray<infer Type extends TSchema> ? TArray<TDecodeType<Type>> : Type extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TDecodeType<Type>> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TConstructor<TDecodeTypes<Parameters>, TDecodeType<InstanceType>> : Type extends TEnum<infer Values> ? TEnum<Values> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TDecodeTypes<Parameters>, TDecodeType<ReturnType>> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TDecodeTypes<Types>> : Type extends TImport<infer ModuleProperties extends TProperties, infer Key> ? TDecodeImport<ModuleProperties, Key> : Type extends TIterator<infer Type extends TSchema> ? TIterator<TDecodeType<Type>> : Type extends TNot<infer Type extends TSchema> ? TNot<TDecodeType<Type>> : Type extends TObject<infer Properties extends TProperties> ? TObject<Evaluate<TDecodeProperties<Properties>>> : Type extends TPromise<infer Type extends TSchema> ? TPromise<TDecodeType<Type>> : Type extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TRecord<Key, TDecodeType<Value>> : Type extends TRecursive<infer Type extends TSchema> ? TRecursive<TDecodeType<Type>> : Type extends TRef<infer Ref extends string> ? TRef<Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TTuple<TDecodeTypes<Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TDecodeTypes<Types>> : Type);
|
|
30
|
+
export type StaticDecodeIsAny<Type> = boolean extends (Type extends TSchema ? true : false) ? true : false;
|
|
28
31
|
/** Creates an decoded static type from a TypeBox type */
|
|
29
|
-
export type StaticDecode<
|
|
32
|
+
export type StaticDecode<Type extends TSchema, Params extends unknown[] = [], Result = StaticDecodeIsAny<Type> extends true ? unknown : Static<TDecodeType<Type>, Params>> = Result;
|
|
30
33
|
/** Creates an encoded static type from a TypeBox type */
|
|
31
|
-
export type StaticEncode<
|
|
34
|
+
export type StaticEncode<Type extends TSchema, Params extends unknown[] = [], Result = Static<Type, Params>> = Result;
|
|
32
35
|
/** Creates a static type from a TypeBox type */
|
|
33
|
-
export type Static<
|
|
34
|
-
params:
|
|
35
|
-
})['static'];
|
|
36
|
+
export type Static<Type extends TSchema, Params extends unknown[] = [], Result = (Type & {
|
|
37
|
+
params: Params;
|
|
38
|
+
})['static']> = Result;
|
|
39
|
+
export {};
|
|
@@ -78,12 +78,10 @@ function FromIntersect(schema, references, path, value) {
|
|
|
78
78
|
}
|
|
79
79
|
// prettier-ignore
|
|
80
80
|
function FromImport(schema, references, path, value) {
|
|
81
|
-
const
|
|
81
|
+
const additional = globalThis.Object.values(schema.$defs);
|
|
82
82
|
const target = schema.$defs[schema.$ref];
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
const transformTarget = { [TransformKind]: transform, ...target };
|
|
86
|
-
return Visit(transformTarget, [...references, ...definitions], path, value);
|
|
83
|
+
const result = Visit(target, [...references, ...additional], path, value);
|
|
84
|
+
return Default(schema, path, result);
|
|
87
85
|
}
|
|
88
86
|
function FromNot(schema, references, path, value) {
|
|
89
87
|
return Default(schema, path, Visit(schema.not, references, path, value));
|
|
@@ -55,12 +55,10 @@ function FromArray(schema, references, path, value) {
|
|
|
55
55
|
}
|
|
56
56
|
// prettier-ignore
|
|
57
57
|
function FromImport(schema, references, path, value) {
|
|
58
|
-
const
|
|
58
|
+
const additional = globalThis.Object.values(schema.$defs);
|
|
59
59
|
const target = schema.$defs[schema.$ref];
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
const transformTarget = { [TransformKind]: transform, ...target };
|
|
63
|
-
return Visit(transformTarget, [...references, ...definitions], path, value);
|
|
60
|
+
const result = Default(schema, path, value);
|
|
61
|
+
return Visit(target, [...references, ...additional], path, result);
|
|
64
62
|
}
|
|
65
63
|
// prettier-ignore
|
|
66
64
|
function FromIntersect(schema, references, path, value) {
|
|
@@ -29,6 +29,12 @@ function FromIntersect(schema, references) {
|
|
|
29
29
|
return IsTransform(schema) || IsTransform(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references));
|
|
30
30
|
}
|
|
31
31
|
// prettier-ignore
|
|
32
|
+
function FromImport(schema, references) {
|
|
33
|
+
const additional = globalThis.Object.getOwnPropertyNames(schema.$defs).reduce((result, key) => [...result, schema.$defs[key]], []);
|
|
34
|
+
const target = schema.$defs[schema.$ref];
|
|
35
|
+
return IsTransform(schema) || Visit(target, [...additional, ...references]);
|
|
36
|
+
}
|
|
37
|
+
// prettier-ignore
|
|
32
38
|
function FromIterator(schema, references) {
|
|
33
39
|
return IsTransform(schema) || Visit(schema.items, references);
|
|
34
40
|
}
|
|
@@ -89,6 +95,8 @@ function Visit(schema, references) {
|
|
|
89
95
|
return FromConstructor(schema_, references_);
|
|
90
96
|
case 'Function':
|
|
91
97
|
return FromFunction(schema_, references_);
|
|
98
|
+
case 'Import':
|
|
99
|
+
return FromImport(schema_, references_);
|
|
92
100
|
case 'Intersect':
|
|
93
101
|
return FromIntersect(schema_, references_);
|
|
94
102
|
case 'Iterator':
|