@@ -35,6 +35,7 @@ import type { TConstructor } from '../constructor/index'
35
35
import type { TEnum } from '../enum/index'
36
36
import type { TFunction } from '../function/index'
37
37
import type { TIntersect } from '../intersect/index'
38
+ import type { TImport } from '../module/index'
38
39
import type { TIterator } from '../iterator/index'
39
40
import type { TNot } from '../not/index'
40
41
import type { TObject , TProperties } from '../object/index'
@@ -47,48 +48,73 @@ import type { TUnion } from '../union/index'
47
48
import type { TUnsafe } from '../unsafe/index'
48
49
import type { TSchema } from '../schema/index'
49
50
import type { TTransform } from '../transform/index'
51
+ import type { TNever } from '../never/index'
50
52
51
53
// ------------------------------------------------------------------
52
- // DecodeType
54
+ // Import
53
55
// ------------------------------------------------------------------
54
56
// prettier-ignore
55
- export type TDecodeProperties < T extends TProperties > = {
56
- [ K in keyof T ] : TDecodeType < T [ K ] >
57
+ type TDecodeImport < ModuleProperties extends TProperties , Key extends PropertyKey > = (
58
+ Key extends keyof ModuleProperties
59
+ ? TDecodeType < ModuleProperties [ Key ] > extends infer Type extends TSchema
60
+ ? Type extends TRef < infer Ref extends string >
61
+ ? TDecodeImport < ModuleProperties , Ref >
62
+ : Type
63
+ : TNever
64
+ : TNever
65
+ )
66
+ // ------------------------------------------------------------------
67
+ // Properties
68
+ // ------------------------------------------------------------------
69
+ // prettier-ignore
70
+ type TDecodeProperties < Properties extends TProperties > = {
71
+ [ Key in keyof Properties ] : TDecodeType < Properties [ Key ] >
57
72
}
73
+ // ------------------------------------------------------------------
74
+ // Types
75
+ // ------------------------------------------------------------------
58
76
// prettier-ignore
59
- export type TDecodeRest < T extends TSchema [ ] , Acc extends TSchema [ ] = [ ] > =
60
- T extends [ infer L extends TSchema , ...infer R extends TSchema [ ] ]
61
- ? TDecodeRest < R , [ ...Acc , TDecodeType < L > ] >
62
- : Acc
77
+ type TDecodeTypes < Types extends TSchema [ ] , Result extends TSchema [ ] = [ ] > = (
78
+ Types extends [ infer Left extends TSchema , ...infer Right extends TSchema [ ] ]
79
+ ? TDecodeTypes < Right , [ ...Result , TDecodeType < Left > ] >
80
+ : Result
81
+ )
82
+ // ------------------------------------------------------------------
83
+ // Types
84
+ // ------------------------------------------------------------------
63
85
// prettier-ignore
64
- export type TDecodeType < T extends TSchema > = (
65
- T extends TOptional < infer S extends TSchema > ? TOptional < TDecodeType < S > > :
66
- T extends TReadonly < infer S extends TSchema > ? TReadonly < TDecodeType < S > > :
67
- T extends TTransform < infer _ , infer R > ? TUnsafe < R > :
68
- T extends TArray < infer S extends TSchema > ? TArray < TDecodeType < S > > :
69
- T extends TAsyncIterator < infer S extends TSchema > ? TAsyncIterator < TDecodeType < S > > :
70
- T extends TConstructor < infer P extends TSchema [ ] , infer R extends TSchema > ? TConstructor < TDecodeRest < P > , TDecodeType < R > > :
71
- T extends TEnum < infer S > ? TEnum < S > : // intercept for union. interior non decodable
72
- T extends TFunction < infer P extends TSchema [ ] , infer R extends TSchema > ? TFunction < TDecodeRest < P > , TDecodeType < R > > :
73
- T extends TIntersect < infer S extends TSchema [ ] > ? TIntersect < TDecodeRest < S > > :
74
- T extends TIterator < infer S extends TSchema > ? TIterator < TDecodeType < S > > :
75
- T extends TNot < infer S extends TSchema > ? TNot < TDecodeType < S > > :
76
- T extends TObject < infer S > ? TObject < Evaluate < TDecodeProperties < S > > > :
77
- T extends TPromise < infer S extends TSchema > ? TPromise < TDecodeType < S > > :
78
- T extends TRecord < infer K , infer S > ? TRecord < K , TDecodeType < S > > :
79
- T extends TRecursive < infer S extends TSchema > ? TRecursive < TDecodeType < S > > :
80
- T extends TRef < infer S extends string > ? TRef < S > :
81
- T extends TTuple < infer S extends TSchema [ ] > ? TTuple < TDecodeRest < S > > :
82
- T extends TUnion < infer S extends TSchema [ ] > ? TUnion < TDecodeRest < S > > :
83
- T
86
+ export type TDecodeType < Type extends TSchema > = (
87
+ Type extends TOptional < infer Type extends TSchema > ? TOptional < TDecodeType < Type > > :
88
+ Type extends TReadonly < infer Type extends TSchema > ? TReadonly < TDecodeType < Type > > :
89
+ Type extends TTransform < infer _Input extends TSchema , infer Output > ? TUnsafe < Output > :
90
+ Type extends TArray < infer Type extends TSchema > ? TArray < TDecodeType < Type > > :
91
+ Type extends TAsyncIterator < infer Type extends TSchema > ? TAsyncIterator < TDecodeType < Type > > :
92
+ Type extends TConstructor < infer Parameters extends TSchema [ ] , infer InstanceType extends TSchema > ? TConstructor < TDecodeTypes < Parameters > , TDecodeType < InstanceType > > :
93
+ Type extends TEnum < infer Values > ? TEnum < Values > : // intercept for union. interior non decodable
94
+ Type extends TFunction < infer Parameters extends TSchema [ ] , infer ReturnType extends TSchema > ? TFunction < TDecodeTypes < Parameters > , TDecodeType < ReturnType > > :
95
+ Type extends TIntersect < infer Types extends TSchema [ ] > ? TIntersect < TDecodeTypes < Types > > :
96
+ Type extends TImport < infer ModuleProperties extends TProperties , infer Key > ? TDecodeImport < ModuleProperties , Key > :
97
+ Type extends TIterator < infer Type extends TSchema > ? TIterator < TDecodeType < Type > > :
98
+ Type extends TNot < infer Type extends TSchema > ? TNot < TDecodeType < Type > > :
99
+ Type extends TObject < infer Properties extends TProperties > ? TObject < Evaluate < TDecodeProperties < Properties > > > :
100
+ Type extends TPromise < infer Type extends TSchema > ? TPromise < TDecodeType < Type > > :
101
+ Type extends TRecord < infer Key extends TSchema , infer Value extends TSchema > ? TRecord < Key , TDecodeType < Value > > :
102
+ Type extends TRecursive < infer Type extends TSchema > ? TRecursive < TDecodeType < Type > > :
103
+ Type extends TRef < infer Ref extends string > ? TRef < Ref > :
104
+ Type extends TTuple < infer Types extends TSchema [ ] > ? TTuple < TDecodeTypes < Types > > :
105
+ Type extends TUnion < infer Types extends TSchema [ ] > ? TUnion < TDecodeTypes < Types > > :
106
+ Type
84
107
)
85
108
// ------------------------------------------------------------------
86
109
// Static
87
110
// ------------------------------------------------------------------
88
- export type StaticDecodeIsAny < T > = boolean extends ( T extends TSchema ? true : false ) ? true : false
111
+ export type StaticDecodeIsAny < Type > = boolean extends ( Type extends TSchema ? true : false ) ? true : false
89
112
/** Creates an decoded static type from a TypeBox type */
90
- export type StaticDecode < T extends TSchema , P extends unknown [ ] = [ ] > = StaticDecodeIsAny < T > extends true ? unknown : Static < TDecodeType < T > , P >
113
+ // prettier-ignore
114
+ export type StaticDecode < Type extends TSchema , Params extends unknown [ ] = [ ] ,
115
+ Result = StaticDecodeIsAny < Type > extends true ? unknown : Static < TDecodeType < Type > , Params >
116
+ > = Result
91
117
/** Creates an encoded static type from a TypeBox type */
92
- export type StaticEncode < T extends TSchema , P extends unknown [ ] = [ ] > = Static < T , P >
118
+ export type StaticEncode < Type extends TSchema , Params extends unknown [ ] = [ ] , Result = Static < Type , Params > > = Result
93
119
/** Creates a static type from a TypeBox type */
94
- export type Static < T extends TSchema , P extends unknown [ ] = [ ] > = ( T & { params : P } ) [ 'static' ]
120
+ export type Static < Type extends TSchema , Params extends unknown [ ] = [ ] , Result = ( Type & { params : Params } ) [ 'static' ] > = Result
0 commit comments