@@ -147,6 +147,34 @@ const anyAction = { type: 'foo' } as AnyAction
147
147
} )
148
148
} ) ( )
149
149
150
+ /**
151
+ * Should handle reject withvalue within a try catch block
152
+ *
153
+ * Note:
154
+ * this is a sample code taken from #1605
155
+ *
156
+ */
157
+ ; ( async ( ) => {
158
+ type ResultType = {
159
+ text : string
160
+ }
161
+ const demoPromise = async ( ) : Promise < ResultType > =>
162
+ new Promise ( ( resolve , _ ) => resolve ( { text : '' } ) )
163
+ const thunk = createAsyncThunk ( 'thunk' , async ( args , thunkAPI ) => {
164
+ try {
165
+ const result = await demoPromise ( )
166
+ return result
167
+ } catch ( error ) {
168
+ return thunkAPI . rejectWithValue ( error )
169
+ }
170
+ } )
171
+ createReducer ( { } , ( builder ) =>
172
+ builder . addCase ( thunk . fulfilled , ( s , action ) => {
173
+ expectType < ResultType > ( action . payload )
174
+ } )
175
+ )
176
+ } ) ( )
177
+
150
178
{
151
179
interface Item {
152
180
name : string
@@ -453,27 +481,49 @@ const anyAction = { type: 'foo' } as AnyAction
453
481
{
454
482
// return values
455
483
createAsyncThunk < 'ret' , void , { } > ( 'test' , ( _ , api ) => 'ret' as const )
484
+ createAsyncThunk < 'ret' , void , { } > ( 'test' , async ( _ , api ) => 'ret' as const )
456
485
createAsyncThunk < 'ret' , void , { fulfilledMeta : string } > ( 'test' , ( _ , api ) =>
457
486
api . fulfillWithValue ( 'ret' as const , '' )
458
487
)
488
+ createAsyncThunk < 'ret' , void , { fulfilledMeta : string } > (
489
+ 'test' ,
490
+ async ( _ , api ) => api . fulfillWithValue ( 'ret' as const , '' )
491
+ )
459
492
createAsyncThunk < 'ret' , void , { fulfilledMeta : string } > (
460
493
'test' ,
461
494
// @ts -expect-error has to be a fulfilledWithValue call
462
495
( _ , api ) => 'ret' as const
463
496
)
497
+ createAsyncThunk < 'ret' , void , { fulfilledMeta : string } > (
498
+ 'test' ,
499
+ // @ts -expect-error has to be a fulfilledWithValue call
500
+ async ( _ , api ) => 'ret' as const
501
+ )
464
502
createAsyncThunk < 'ret' , void , { fulfilledMeta : string } > (
465
503
'test' , // @ts -expect-error should only allow returning with 'test'
466
504
( _ , api ) => api . fulfillWithValue ( 5 , '' )
467
505
)
506
+ createAsyncThunk < 'ret' , void , { fulfilledMeta : string } > (
507
+ 'test' , // @ts -expect-error should only allow returning with 'test'
508
+ async ( _ , api ) => api . fulfillWithValue ( 5 , '' )
509
+ )
468
510
469
511
// reject values
470
512
createAsyncThunk < 'ret' , void , { rejectValue : string } > ( 'test' , ( _ , api ) =>
471
513
api . rejectWithValue ( 'ret' )
472
514
)
515
+ createAsyncThunk < 'ret' , void , { rejectValue : string } > (
516
+ 'test' ,
517
+ async ( _ , api ) => api . rejectWithValue ( 'ret' )
518
+ )
473
519
createAsyncThunk < 'ret' , void , { rejectValue : string ; rejectedMeta : number } > (
474
520
'test' ,
475
521
( _ , api ) => api . rejectWithValue ( 'ret' , 5 )
476
522
)
523
+ createAsyncThunk < 'ret' , void , { rejectValue : string ; rejectedMeta : number } > (
524
+ 'test' ,
525
+ async ( _ , api ) => api . rejectWithValue ( 'ret' , 5 )
526
+ )
477
527
createAsyncThunk < 'ret' , void , { rejectValue : string ; rejectedMeta : number } > (
478
528
'test' ,
479
529
( _ , api ) => api . rejectWithValue ( 'ret' , 5 )
@@ -483,9 +533,19 @@ const anyAction = { type: 'foo' } as AnyAction
483
533
// @ts -expect-error wrong rejectedMeta type
484
534
( _ , api ) => api . rejectWithValue ( 'ret' , '' )
485
535
)
536
+ createAsyncThunk < 'ret' , void , { rejectValue : string ; rejectedMeta : number } > (
537
+ 'test' ,
538
+ // @ts -expect-error wrong rejectedMeta type
539
+ async ( _ , api ) => api . rejectWithValue ( 'ret' , '' )
540
+ )
486
541
createAsyncThunk < 'ret' , void , { rejectValue : string ; rejectedMeta : number } > (
487
542
'test' ,
488
543
// @ts -expect-error wrong rejectValue type
489
544
( _ , api ) => api . rejectWithValue ( 5 , '' )
490
545
)
546
+ createAsyncThunk < 'ret' , void , { rejectValue : string ; rejectedMeta : number } > (
547
+ 'test' ,
548
+ // @ts -expect-error wrong rejectValue type
549
+ async ( _ , api ) => api . rejectWithValue ( 5 , '' )
550
+ )
491
551
}
0 commit comments