@@ -11,12 +11,18 @@ import { FallbackIfUnknown } from './tsHelpers'
11
11
// @ts -ignore we need the import of these types due to a bundling issue.
12
12
type _Keep = PayloadAction | ActionCreatorWithPreparedPayload < any , unknown >
13
13
14
- export type BaseThunkAPI < S , E , D extends Dispatch = Dispatch > = {
14
+ export type BaseThunkAPI <
15
+ S ,
16
+ E ,
17
+ D extends Dispatch = Dispatch ,
18
+ RejectedValue = undefined
19
+ > = {
15
20
dispatch : D
16
21
getState : ( ) => S
17
22
extra : E
18
23
requestId : string
19
24
signal : AbortSignal
25
+ rejectWithValue ( value : RejectedValue ) : RejectWithValue < RejectedValue >
20
26
}
21
27
22
28
/**
@@ -36,21 +42,8 @@ const commonProperties: Array<keyof SerializedError> = [
36
42
'code'
37
43
]
38
44
39
- const rejectionSymbol : unique symbol = Symbol ( 'rejectWithValue' )
40
- type RejectWithValue < RejectValue > = {
41
- [ rejectionSymbol ] : 'reject'
42
- value : RejectValue
43
- }
44
- function rejectWithValue < RejectValue > (
45
- value : RejectValue
46
- ) : RejectWithValue < RejectValue > {
47
- return {
48
- [ rejectionSymbol ] : 'reject' ,
49
- value
50
- }
51
- }
52
- function isRejectWithValue ( value : any ) : value is RejectWithValue < unknown > {
53
- return value && value [ rejectionSymbol ] === 'reject'
45
+ class RejectWithValue < RejectValue > {
46
+ constructor ( public readonly value : RejectValue ) { }
54
47
}
55
48
56
49
// Reworked from https://github.com/sindresorhus/serialize-error
@@ -100,13 +93,15 @@ type GetDispatch<ThunkApiConfig> = ThunkApiConfig extends {
100
93
type GetThunkAPI < ThunkApiConfig > = BaseThunkAPI <
101
94
GetState < ThunkApiConfig > ,
102
95
GetExtra < ThunkApiConfig > ,
103
- GetDispatch < ThunkApiConfig >
96
+ GetDispatch < ThunkApiConfig > ,
97
+ GetRejectValue < ThunkApiConfig >
104
98
>
99
+
105
100
type GetRejectValue < ThunkApiConfig > = ThunkApiConfig extends {
106
101
rejectValue : infer RejectValue
107
102
}
108
103
? RejectValue
109
- : undefined
104
+ : unknown
110
105
111
106
/**
112
107
*
@@ -163,7 +158,7 @@ export function createAsyncThunk<
163
158
const aborted = error && error . name === 'AbortError'
164
159
return {
165
160
payload,
166
- error : miniSerializeError ( error ) ,
161
+ error : miniSerializeError ( error ) || { message : 'Rejected' } ,
167
162
meta : {
168
163
arg,
169
164
requestId,
@@ -207,10 +202,13 @@ export function createAsyncThunk<
207
202
getState,
208
203
extra,
209
204
requestId,
210
- signal : abortController . signal
205
+ signal : abortController . signal ,
206
+ rejectWithValue ( value : RejectedValue ) {
207
+ return new RejectWithValue ( value )
208
+ }
211
209
} )
212
210
) . then ( result => {
213
- if ( isRejectWithValue ( result ) ) {
211
+ if ( result instanceof RejectWithValue ) {
214
212
return rejected ( null , requestId , arg , result . value )
215
213
}
216
214
return fulfilled ( result , requestId , arg )
@@ -237,16 +235,24 @@ export function createAsyncThunk<
237
235
fulfilled
238
236
} )
239
237
}
240
- createAsyncThunk . rejectWithValue = rejectWithValue
238
+
239
+ type ActionTypesWithOptionalErrorAction =
240
+ | { error : any }
241
+ | { error ?: never ; payload : any }
242
+ type PayloadForActionTypesExcludingErrorActions < T > = T extends { error : any }
243
+ ? never
244
+ : T extends { payload : infer P }
245
+ ? P
246
+ : never
241
247
242
248
/**
243
249
* @alpha
244
250
*/
245
- export function unwrapResult < T > (
246
- returned : { error : any } | { payload : NonNullable < T > }
247
- ) : NonNullable < T > {
251
+ export function unwrapResult < R extends ActionTypesWithOptionalErrorAction > (
252
+ returned : R
253
+ ) : PayloadForActionTypesExcludingErrorActions < R > {
248
254
if ( 'error' in returned ) {
249
255
throw returned . error
250
256
}
251
- return returned . payload
257
+ return ( returned as any ) . payload
252
258
}
0 commit comments