@@ -12,40 +12,47 @@ import type * as util from "./util.js";
12
12
////////// //////////
13
13
//////////////////////////////////////////
14
14
//////////////////////////////////////////
15
- export interface $ZodFunctionDef /* extends schemas.$ZodTypeDef */ {
15
+ export interface $ZodFunctionDef <
16
+ In extends $ZodFunctionIn = $ZodFunctionIn ,
17
+ Out extends $ZodFunctionOut = $ZodFunctionOut ,
18
+ > {
16
19
type : "function" ;
17
- input : $ZodFunctionArgs | null ;
18
- output : schemas . $ZodType | null ;
20
+ input : In ;
21
+ output : Out ;
19
22
}
20
23
21
24
export type $ZodFunctionArgs = schemas . $ZodType < unknown [ ] , unknown [ ] > ;
25
+ export type $ZodFunctionIn = $ZodFunctionArgs | null ;
26
+ export type $ZodFunctionOut = schemas . $ZodType | null ;
22
27
23
- export type $InferInnerFunctionType < Args extends $ZodFunctionArgs , Returns extends schemas . $ZodType > = (
24
- ...args : Args [ "_zod" ] [ "output" ]
25
- ) => Returns [ "_zod" ] [ "input" ] ;
28
+ export type $InferInnerFunctionType < Args extends $ZodFunctionIn , Returns extends $ZodFunctionOut > = (
29
+ ...args : null extends Args ? never [ ] : NonNullable < Args > [ "_zod" ] [ "output" ]
30
+ ) => null extends Returns ? unknown : NonNullable < Returns > [ "_zod" ] [ "input" ] ;
26
31
27
- export type $InferInnerFunctionTypeAsync < Args extends $ZodFunctionArgs , Returns extends schemas . $ZodType > = (
28
- ...args : Args [ "_zod" ] [ "output" ]
29
- ) => util . MaybeAsync < Returns [ "_zod" ] [ "input" ] > ;
32
+ export type $InferInnerFunctionTypeAsync < Args extends $ZodFunctionIn , Returns extends $ZodFunctionOut > = (
33
+ ...args : null extends Args ? never [ ] : NonNullable < Args > [ "_zod" ] [ "output" ]
34
+ ) => null extends Returns ? unknown : util . MaybeAsync < NonNullable < Returns > [ "_zod" ] [ "input" ] > ;
30
35
31
- export type $InferOuterFunctionType < Args extends $ZodFunctionArgs , Returns extends schemas . $ZodType > = (
32
- ...args : Args [ "_zod" ] [ "input" ]
33
- ) => Returns [ "_zod" ] [ "output" ] ;
36
+ export type $InferOuterFunctionType < Args extends $ZodFunctionIn , Returns extends $ZodFunctionOut > = (
37
+ ...args : null extends Args ? never [ ] : NonNullable < Args > [ "_zod" ] [ "input" ]
38
+ ) => null extends Returns ? unknown : NonNullable < Returns > [ "_zod" ] [ "output" ] ;
34
39
35
- export type $InferOuterFunctionTypeAsync < Args extends $ZodFunctionArgs , Returns extends schemas . $ZodType > = (
36
- ...args : Args [ "_zod" ] [ "input" ]
37
- ) => util . MaybeAsync < Returns [ "_zod" ] [ "output" ] > ;
40
+ export type $InferOuterFunctionTypeAsync < Args extends $ZodFunctionIn , Returns extends $ZodFunctionOut > = (
41
+ ...args : null extends Args ? never [ ] : NonNullable < Args > [ "_zod" ] [ "input" ]
42
+ ) => null extends Returns ? unknown : util . MaybeAsync < NonNullable < Returns > [ "_zod" ] [ "output" ] > ;
38
43
39
44
export class $ZodFunction <
40
- Args extends $ZodFunctionArgs = $ZodFunctionArgs ,
41
- Returns extends schemas . $ZodType = schemas . $ZodType ,
45
+ Args extends $ZodFunctionIn = $ZodFunctionIn ,
46
+ Returns extends $ZodFunctionOut = $ZodFunctionOut ,
42
47
> {
43
- _def : $ZodFunctionDef ;
48
+ _def : $ZodFunctionDef < Args , Returns > ;
49
+ def : $ZodFunctionDef < Args , Returns > ;
44
50
_input ! : $InferInnerFunctionType < Args , Returns > ;
45
51
_output ! : $InferOuterFunctionType < Args , Returns > ;
46
52
47
- constructor ( def : $ZodFunctionDef ) {
53
+ constructor ( def : $ZodFunctionDef < Args , Returns > ) {
48
54
this . _def = def ;
55
+ this . def = def ;
49
56
}
50
57
51
58
implement < F extends $InferInnerFunctionType < Args , Returns > > (
@@ -59,7 +66,7 @@ export class $ZodFunction<
59
66
if ( ! Array . isArray ( parsedArgs ) ) {
60
67
throw new Error ( "Invalid arguments schema: not an array or tuple schema." ) ;
61
68
}
62
- const output = func ( ...parsedArgs ) ;
69
+ const output = func ( ...( parsedArgs as any ) ) ;
63
70
return this . _def . output ? parse ( this . _def . output , output , undefined , { callee : impl } ) : output ;
64
71
} ) as any ;
65
72
return impl ;
@@ -77,17 +84,17 @@ export class $ZodFunction<
77
84
if ( ! Array . isArray ( parsedArgs ) ) {
78
85
throw new Error ( "Invalid arguments schema: not an array or tuple schema." ) ;
79
86
}
80
- const output = await func ( ...parsedArgs ) ;
87
+ const output = await func ( ...( parsedArgs as any ) ) ;
81
88
return this . _def . output ? parseAsync ( this . _def . output , output , undefined , { callee : impl } ) : output ;
82
89
} ) as any ;
83
90
return impl ;
84
91
}
85
92
86
- input < const Items extends util . TupleItems , const Rest extends schemas . $ZodType | null = null > (
93
+ input < const Items extends util . TupleItems , const Rest extends $ZodFunctionOut = null > (
87
94
args : Items ,
88
95
rest ?: Rest
89
96
) : $ZodFunction < schemas . $ZodTuple < Items , Rest > , Returns > ;
90
- input < NewArgs extends $ZodFunctionArgs > ( args : NewArgs ) : $ZodFunction < NewArgs , Returns > ;
97
+ input < NewArgs extends $ZodFunctionIn > ( args : NewArgs ) : $ZodFunction < NewArgs , Returns > ;
91
98
input ( ...args : any [ ] ) : $ZodFunction < any , Returns > {
92
99
if ( Array . isArray ( args [ 0 ] ) ) {
93
100
return new $ZodFunction ( {
@@ -116,19 +123,28 @@ export class $ZodFunction<
116
123
}
117
124
}
118
125
119
- export interface $ZodFunctionParams < I extends $ZodFunctionArgs , O extends schemas . $ZodType > {
126
+ export interface $ZodFunctionParams < I extends $ZodFunctionIn , O extends schemas . $ZodType > {
120
127
input ?: I ;
121
128
output ?: O ;
122
129
}
123
130
124
131
function _function ( ) : $ZodFunction ;
132
+ function _function < const In extends Array < schemas . $ZodType > = Array < schemas . $ZodType > > ( params : {
133
+ input : In ;
134
+ } ) : $ZodFunction < $ZodTuple < In , null > , null > ;
135
+ function _function < const In extends $ZodFunctionIn = $ZodFunctionIn > ( params : {
136
+ input : In ;
137
+ } ) : $ZodFunction < In , null > ;
138
+ function _function < const Out extends $ZodFunctionOut = $ZodFunctionOut > ( params : {
139
+ output : Out ;
140
+ } ) : $ZodFunction < null , Out > ;
125
141
function _function <
126
- const In extends Array < schemas . $ZodType > = Array < schemas . $ZodType > ,
142
+ In extends $ZodFunctionIn = $ZodFunctionIn ,
127
143
Out extends schemas . $ZodType = schemas . $ZodType ,
128
- > ( params ?: { input ?: In ; output ?: Out } ) : $ZodFunction < $ZodTuple < In , null > , Out > ;
129
- function _function < In extends $ZodFunctionArgs = $ZodFunctionArgs , Out extends schemas . $ZodType = schemas . $ZodType > (
130
- params ?: $ZodFunctionParams < In , Out >
131
- ) : $ZodFunction < In , Out > ;
144
+ > ( params ?: {
145
+ input : In ;
146
+ output : Out ;
147
+ } ) : $ZodFunction < In , Out > ;
132
148
function _function ( params ?: {
133
149
output ?: schemas . $ZodType ;
134
150
input ?: $ZodFunctionArgs | Array < schemas . $ZodType > ;
0 commit comments