@@ -19,7 +19,7 @@ const DEFAULT_RESULT = {
19
19
20
20
const getActionStatus = <
21
21
const ServerError ,
22
- const S extends Schema ,
22
+ const S extends Schema | undefined ,
23
23
const BAS extends readonly Schema [ ] ,
24
24
const FVE ,
25
25
const FBAVE ,
@@ -49,7 +49,7 @@ const getActionStatus = <
49
49
50
50
const useActionCallbacks = <
51
51
const ServerError ,
52
- const S extends Schema ,
52
+ const S extends Schema | undefined ,
53
53
const BAS extends readonly Schema [ ] ,
54
54
const FVE ,
55
55
const FBAVE ,
@@ -62,7 +62,7 @@ const useActionCallbacks = <
62
62
cb,
63
63
} : {
64
64
result : HookResult < ServerError , S , BAS , FVE , FBAVE , Data > ;
65
- input : InferIn < S > ;
65
+ input : S extends Schema ? InferIn < S > : undefined ;
66
66
status : HookActionStatus ;
67
67
reset : ( ) => void ;
68
68
cb ?: HookCallbacks < ServerError , S , BAS , FVE , FBAVE , Data > ;
@@ -110,7 +110,7 @@ const useActionCallbacks = <
110
110
*/
111
111
export const useAction = <
112
112
const ServerError ,
113
- const S extends Schema ,
113
+ const S extends Schema | undefined ,
114
114
const BAS extends readonly Schema [ ] ,
115
115
const FVE ,
116
116
const FBAVE ,
@@ -122,18 +122,18 @@ export const useAction = <
122
122
const [ , startTransition ] = React . useTransition ( ) ;
123
123
const [ result , setResult ] =
124
124
React . useState < HookResult < ServerError , S , BAS , FVE , FBAVE , Data > > ( DEFAULT_RESULT ) ;
125
- const [ input , setInput ] = React . useState < InferIn < S > > ( ) ;
125
+ const [ input , setInput ] = React . useState < S extends Schema ? InferIn < S > : void > ( ) ;
126
126
const [ isExecuting , setIsExecuting ] = React . useState ( false ) ;
127
127
128
128
const status = getActionStatus < ServerError , S , BAS , FVE , FBAVE , Data > ( { isExecuting, result } ) ;
129
129
130
130
const execute = React . useCallback (
131
- ( input : InferIn < S > ) => {
131
+ ( input : S extends Schema ? InferIn < S > : void ) => {
132
132
setInput ( input ) ;
133
133
setIsExecuting ( true ) ;
134
134
135
135
return startTransition ( ( ) => {
136
- return safeActionFn ( input )
136
+ return safeActionFn ( input as S extends Schema ? InferIn < S > : undefined )
137
137
. then ( ( res ) => setResult ( res ?? DEFAULT_RESULT ) )
138
138
. catch ( ( e ) => {
139
139
if ( isRedirectError ( e ) || isNotFoundError ( e ) ) {
@@ -154,7 +154,13 @@ export const useAction = <
154
154
setResult ( DEFAULT_RESULT ) ;
155
155
} , [ ] ) ;
156
156
157
- useActionCallbacks ( { result, input, status, reset, cb : callbacks } ) ;
157
+ useActionCallbacks ( {
158
+ result,
159
+ input : input as S extends Schema ? InferIn < S > : undefined ,
160
+ status,
161
+ reset,
162
+ cb : callbacks ,
163
+ } ) ;
158
164
159
165
return {
160
166
execute,
@@ -177,38 +183,38 @@ export const useAction = <
177
183
*/
178
184
export const useOptimisticAction = <
179
185
const ServerError ,
180
- const S extends Schema ,
186
+ const S extends Schema | undefined ,
181
187
const BAS extends readonly Schema [ ] ,
182
188
const FVE ,
183
189
const FBAVE ,
184
190
const Data ,
185
191
> (
186
192
safeActionFn : HookSafeActionFn < ServerError , S , BAS , FVE , FBAVE , Data > ,
187
193
initialOptimisticData : Data ,
188
- reducer : ( state : Data , input : InferIn < S > ) => Data ,
194
+ reducer : ( state : Data , input : S extends Schema ? InferIn < S > : undefined ) => Data ,
189
195
callbacks ?: HookCallbacks < ServerError , S , BAS , FVE , FBAVE , Data >
190
196
) => {
191
197
const [ , startTransition ] = React . useTransition ( ) ;
192
198
const [ result , setResult ] =
193
199
React . useState < HookResult < ServerError , S , BAS , FVE , FBAVE , Data > > ( DEFAULT_RESULT ) ;
194
- const [ input , setInput ] = React . useState < InferIn < S > > ( ) ;
200
+ const [ input , setInput ] = React . useState < S extends Schema ? InferIn < S > : void > ( ) ;
195
201
const [ isExecuting , setIsExecuting ] = React . useState ( false ) ;
196
202
197
- const [ optimisticData , setOptimisticState ] = React . useOptimistic < Data , InferIn < S > > (
198
- initialOptimisticData ,
199
- reducer
200
- ) ;
203
+ const [ optimisticData , setOptimisticState ] = React . useOptimistic <
204
+ Data ,
205
+ S extends Schema ? InferIn < S > : undefined
206
+ > ( initialOptimisticData , reducer ) ;
201
207
202
208
const status = getActionStatus < ServerError , S , BAS , FVE , FBAVE , Data > ( { isExecuting, result } ) ;
203
209
204
210
const execute = React . useCallback (
205
- ( input : InferIn < S > ) => {
211
+ ( input : S extends Schema ? InferIn < S > : void ) => {
206
212
setInput ( input ) ;
207
213
setIsExecuting ( true ) ;
208
214
209
215
return startTransition ( ( ) => {
210
- setOptimisticState ( input ) ;
211
- return safeActionFn ( input )
216
+ setOptimisticState ( input as S extends Schema ? InferIn < S > : undefined ) ;
217
+ return safeActionFn ( input as S extends Schema ? InferIn < S > : undefined )
212
218
. then ( ( res ) => setResult ( res ?? DEFAULT_RESULT ) )
213
219
. catch ( ( e ) => {
214
220
if ( isRedirectError ( e ) || isNotFoundError ( e ) ) {
@@ -229,7 +235,13 @@ export const useOptimisticAction = <
229
235
setResult ( DEFAULT_RESULT ) ;
230
236
} , [ ] ) ;
231
237
232
- useActionCallbacks ( { result, input, status, reset, cb : callbacks } ) ;
238
+ useActionCallbacks ( {
239
+ result,
240
+ input : input as S extends Schema ? InferIn < S > : undefined ,
241
+ status,
242
+ reset,
243
+ cb : callbacks ,
244
+ } ) ;
233
245
234
246
return {
235
247
execute,
0 commit comments