@sinclair/typebox 0.34.26 → 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/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/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
|
@@ -20,11 +20,12 @@ import { TPartial } from '../partial/index';
|
|
|
20
20
|
import { type TReadonly } from '../readonly/index';
|
|
21
21
|
import { type TRecordOrObject, type TRecord } from '../record/index';
|
|
22
22
|
import { type TRef } from '../ref/index';
|
|
23
|
-
import { TRequired } from '../required/index';
|
|
23
|
+
import { type TRequired } from '../required/index';
|
|
24
|
+
import { type TTransform } from '../transform/index';
|
|
24
25
|
import { type TTuple } from '../tuple/index';
|
|
25
26
|
import { type TUnion, type TUnionEvaluated } from '../union/index';
|
|
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>;
|
|
@@ -25,24 +25,27 @@ const index_18 = require("../record/index");
|
|
|
25
25
|
const index_19 = require("../required/index");
|
|
26
26
|
const index_20 = require("../tuple/index");
|
|
27
27
|
const index_21 = require("../union/index");
|
|
28
|
+
// ------------------------------------------------------------------
|
|
29
|
+
// Symbols
|
|
30
|
+
// ------------------------------------------------------------------
|
|
28
31
|
const index_22 = require("../symbols/index");
|
|
29
32
|
// ------------------------------------------------------------------
|
|
30
33
|
// KindGuard
|
|
31
34
|
// ------------------------------------------------------------------
|
|
32
35
|
const KindGuard = require("../guard/kind");
|
|
33
36
|
// prettier-ignore
|
|
34
|
-
function
|
|
37
|
+
function DereferenceParameters(moduleProperties, types) {
|
|
35
38
|
return types.map((type) => {
|
|
36
39
|
return KindGuard.IsRef(type)
|
|
37
|
-
?
|
|
40
|
+
? Dereference(moduleProperties, type.$ref)
|
|
38
41
|
: FromType(moduleProperties, type);
|
|
39
42
|
});
|
|
40
43
|
}
|
|
41
44
|
// prettier-ignore
|
|
42
|
-
function
|
|
45
|
+
function Dereference(moduleProperties, ref) {
|
|
43
46
|
return (ref in moduleProperties
|
|
44
47
|
? KindGuard.IsRef(moduleProperties[ref])
|
|
45
|
-
?
|
|
48
|
+
? Dereference(moduleProperties, moduleProperties[ref].$ref)
|
|
46
49
|
: FromType(moduleProperties, moduleProperties[ref])
|
|
47
50
|
: (0, index_16.Never)());
|
|
48
51
|
}
|
|
@@ -76,7 +79,7 @@ function FromRequired(parameters) {
|
|
|
76
79
|
}
|
|
77
80
|
// prettier-ignore
|
|
78
81
|
function FromComputed(moduleProperties, target, parameters) {
|
|
79
|
-
const dereferenced =
|
|
82
|
+
const dereferenced = DereferenceParameters(moduleProperties, parameters);
|
|
80
83
|
return (target === 'Awaited' ? FromAwaited(dereferenced) :
|
|
81
84
|
target === 'Index' ? FromIndex(dereferenced) :
|
|
82
85
|
target === 'KeyOf' ? FromKeyOf(dereferenced) :
|
|
@@ -86,6 +89,26 @@ function FromComputed(moduleProperties, target, parameters) {
|
|
|
86
89
|
target === 'Required' ? FromRequired(dereferenced) :
|
|
87
90
|
(0, index_16.Never)());
|
|
88
91
|
}
|
|
92
|
+
function FromArray(moduleProperties, type) {
|
|
93
|
+
return (0, index_4.Array)(FromType(moduleProperties, type));
|
|
94
|
+
}
|
|
95
|
+
function FromAsyncIterator(moduleProperties, type) {
|
|
96
|
+
return (0, index_6.AsyncIterator)(FromType(moduleProperties, type));
|
|
97
|
+
}
|
|
98
|
+
// prettier-ignore
|
|
99
|
+
function FromConstructor(moduleProperties, parameters, instanceType) {
|
|
100
|
+
return (0, index_7.Constructor)(FromTypes(moduleProperties, parameters), FromType(moduleProperties, instanceType));
|
|
101
|
+
}
|
|
102
|
+
// prettier-ignore
|
|
103
|
+
function FromFunction(moduleProperties, parameters, returnType) {
|
|
104
|
+
return (0, index_9.Function)(FromTypes(moduleProperties, parameters), FromType(moduleProperties, returnType));
|
|
105
|
+
}
|
|
106
|
+
function FromIntersect(moduleProperties, types) {
|
|
107
|
+
return (0, index_10.Intersect)(FromTypes(moduleProperties, types));
|
|
108
|
+
}
|
|
109
|
+
function FromIterator(moduleProperties, type) {
|
|
110
|
+
return (0, index_11.Iterator)(FromType(moduleProperties, type));
|
|
111
|
+
}
|
|
89
112
|
function FromObject(moduleProperties, properties) {
|
|
90
113
|
return (0, index_13.Object)(globalThis.Object.keys(properties).reduce((result, key) => {
|
|
91
114
|
return { ...result, [key]: FromType(moduleProperties, properties[key]) };
|
|
@@ -99,53 +122,41 @@ function FromRecord(moduleProperties, type) {
|
|
|
99
122
|
return result;
|
|
100
123
|
}
|
|
101
124
|
// prettier-ignore
|
|
102
|
-
function
|
|
103
|
-
return (
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function FromFunction(moduleProperties, parameters, returnType) {
|
|
107
|
-
return (0, index_9.Function)(FromRest(moduleProperties, parameters), FromType(moduleProperties, returnType));
|
|
125
|
+
function FromTransform(moduleProperties, transform) {
|
|
126
|
+
return (KindGuard.IsRef(transform))
|
|
127
|
+
? { ...Dereference(moduleProperties, transform.$ref), [index_22.TransformKind]: transform[index_22.TransformKind] }
|
|
128
|
+
: transform;
|
|
108
129
|
}
|
|
109
130
|
function FromTuple(moduleProperties, types) {
|
|
110
|
-
return (0, index_20.Tuple)(
|
|
111
|
-
}
|
|
112
|
-
function FromIntersect(moduleProperties, types) {
|
|
113
|
-
return (0, index_10.Intersect)(FromRest(moduleProperties, types));
|
|
131
|
+
return (0, index_20.Tuple)(FromTypes(moduleProperties, types));
|
|
114
132
|
}
|
|
115
133
|
function FromUnion(moduleProperties, types) {
|
|
116
|
-
return (0, index_21.Union)(
|
|
117
|
-
}
|
|
118
|
-
function FromArray(moduleProperties, type) {
|
|
119
|
-
return (0, index_4.Array)(FromType(moduleProperties, type));
|
|
120
|
-
}
|
|
121
|
-
function FromAsyncIterator(moduleProperties, type) {
|
|
122
|
-
return (0, index_6.AsyncIterator)(FromType(moduleProperties, type));
|
|
123
|
-
}
|
|
124
|
-
function FromIterator(moduleProperties, type) {
|
|
125
|
-
return (0, index_11.Iterator)(FromType(moduleProperties, type));
|
|
134
|
+
return (0, index_21.Union)(FromTypes(moduleProperties, types));
|
|
126
135
|
}
|
|
127
|
-
function
|
|
136
|
+
function FromTypes(moduleProperties, types) {
|
|
128
137
|
return types.map((type) => FromType(moduleProperties, type));
|
|
129
138
|
}
|
|
130
139
|
// prettier-ignore
|
|
131
140
|
function FromType(moduleProperties, type) {
|
|
132
141
|
return (
|
|
133
|
-
//
|
|
142
|
+
// Modifiers
|
|
134
143
|
KindGuard.IsOptional(type) ? (0, index_1.CreateType)(FromType(moduleProperties, (0, index_3.Discard)(type, [index_22.OptionalKind])), type) :
|
|
135
144
|
KindGuard.IsReadonly(type) ? (0, index_1.CreateType)(FromType(moduleProperties, (0, index_3.Discard)(type, [index_22.ReadonlyKind])), type) :
|
|
136
|
-
//
|
|
137
|
-
KindGuard.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
// Transform
|
|
146
|
+
KindGuard.IsTransform(type) ? (0, index_1.CreateType)(FromTransform(moduleProperties, type), type) :
|
|
147
|
+
// Types
|
|
148
|
+
KindGuard.IsArray(type) ? (0, index_1.CreateType)(FromArray(moduleProperties, type.items), type) :
|
|
149
|
+
KindGuard.IsAsyncIterator(type) ? (0, index_1.CreateType)(FromAsyncIterator(moduleProperties, type.items), type) :
|
|
150
|
+
KindGuard.IsComputed(type) ? (0, index_1.CreateType)(FromComputed(moduleProperties, type.target, type.parameters)) :
|
|
151
|
+
KindGuard.IsConstructor(type) ? (0, index_1.CreateType)(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
|
|
152
|
+
KindGuard.IsFunction(type) ? (0, index_1.CreateType)(FromFunction(moduleProperties, type.parameters, type.returns), type) :
|
|
153
|
+
KindGuard.IsIntersect(type) ? (0, index_1.CreateType)(FromIntersect(moduleProperties, type.allOf), type) :
|
|
154
|
+
KindGuard.IsIterator(type) ? (0, index_1.CreateType)(FromIterator(moduleProperties, type.items), type) :
|
|
155
|
+
KindGuard.IsObject(type) ? (0, index_1.CreateType)(FromObject(moduleProperties, type.properties), type) :
|
|
156
|
+
KindGuard.IsRecord(type) ? (0, index_1.CreateType)(FromRecord(moduleProperties, type)) :
|
|
157
|
+
KindGuard.IsTuple(type) ? (0, index_1.CreateType)(FromTuple(moduleProperties, type.items || []), type) :
|
|
158
|
+
KindGuard.IsUnion(type) ? (0, index_1.CreateType)(FromUnion(moduleProperties, type.anyOf), type) :
|
|
159
|
+
type);
|
|
149
160
|
}
|
|
150
161
|
// prettier-ignore
|
|
151
162
|
function ComputeType(moduleProperties, key) {
|
|
@@ -7,6 +7,7 @@ import type { TConstructor } from '../constructor/index';
|
|
|
7
7
|
import type { TEnum } from '../enum/index';
|
|
8
8
|
import type { TFunction } from '../function/index';
|
|
9
9
|
import type { TIntersect } from '../intersect/index';
|
|
10
|
+
import type { TImport } from '../module/index';
|
|
10
11
|
import type { TIterator } from '../iterator/index';
|
|
11
12
|
import type { TNot } from '../not/index';
|
|
12
13
|
import type { TObject, TProperties } from '../object/index';
|
|
@@ -19,17 +20,20 @@ import type { TUnion } from '../union/index';
|
|
|
19
20
|
import type { TUnsafe } from '../unsafe/index';
|
|
20
21
|
import type { TSchema } from '../schema/index';
|
|
21
22
|
import type { TTransform } from '../transform/index';
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
import type { TNever } from '../never/index';
|
|
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 {};
|
|
@@ -85,12 +85,10 @@ function FromIntersect(schema, references, path, value) {
|
|
|
85
85
|
}
|
|
86
86
|
// prettier-ignore
|
|
87
87
|
function FromImport(schema, references, path, value) {
|
|
88
|
-
const
|
|
88
|
+
const additional = globalThis.Object.values(schema.$defs);
|
|
89
89
|
const target = schema.$defs[schema.$ref];
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
const transformTarget = { [index_1.TransformKind]: transform, ...target };
|
|
93
|
-
return Visit(transformTarget, [...references, ...definitions], path, value);
|
|
90
|
+
const result = Visit(target, [...references, ...additional], path, value);
|
|
91
|
+
return Default(schema, path, result);
|
|
94
92
|
}
|
|
95
93
|
function FromNot(schema, references, path, value) {
|
|
96
94
|
return Default(schema, path, Visit(schema.not, references, path, value));
|
|
@@ -62,12 +62,10 @@ function FromArray(schema, references, path, value) {
|
|
|
62
62
|
}
|
|
63
63
|
// prettier-ignore
|
|
64
64
|
function FromImport(schema, references, path, value) {
|
|
65
|
-
const
|
|
65
|
+
const additional = globalThis.Object.values(schema.$defs);
|
|
66
66
|
const target = schema.$defs[schema.$ref];
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
const transformTarget = { [index_1.TransformKind]: transform, ...target };
|
|
70
|
-
return Visit(transformTarget, [...references, ...definitions], path, value);
|
|
67
|
+
const result = Default(schema, path, value);
|
|
68
|
+
return Visit(target, [...references, ...additional], path, result);
|
|
71
69
|
}
|
|
72
70
|
// prettier-ignore
|
|
73
71
|
function FromIntersect(schema, references, path, value) {
|
|
@@ -33,6 +33,12 @@ function FromIntersect(schema, references) {
|
|
|
33
33
|
return (0, kind_1.IsTransform)(schema) || (0, kind_1.IsTransform)(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references));
|
|
34
34
|
}
|
|
35
35
|
// prettier-ignore
|
|
36
|
+
function FromImport(schema, references) {
|
|
37
|
+
const additional = globalThis.Object.getOwnPropertyNames(schema.$defs).reduce((result, key) => [...result, schema.$defs[key]], []);
|
|
38
|
+
const target = schema.$defs[schema.$ref];
|
|
39
|
+
return (0, kind_1.IsTransform)(schema) || Visit(target, [...additional, ...references]);
|
|
40
|
+
}
|
|
41
|
+
// prettier-ignore
|
|
36
42
|
function FromIterator(schema, references) {
|
|
37
43
|
return (0, kind_1.IsTransform)(schema) || Visit(schema.items, references);
|
|
38
44
|
}
|
|
@@ -93,6 +99,8 @@ function Visit(schema, references) {
|
|
|
93
99
|
return FromConstructor(schema_, references_);
|
|
94
100
|
case 'Function':
|
|
95
101
|
return FromFunction(schema_, references_);
|
|
102
|
+
case 'Import':
|
|
103
|
+
return FromImport(schema_, references_);
|
|
96
104
|
case 'Intersect':
|
|
97
105
|
return FromIntersect(schema_, references_);
|
|
98
106
|
case 'Iterator':
|
|
@@ -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':
|