Skip to content

Commit 207e6d2

Browse files
committed
hotfix, bogus test case for discussion
1 parent 70ada97 commit 207e6d2

File tree

4 files changed

+116
-33
lines changed

4 files changed

+116
-33
lines changed

package-lock.json

Lines changed: 46 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@types/json-stringify-safe": "^5.0.0",
2929
"@types/nanoid": "^2.1.0",
3030
"@types/node": "^10.14.4",
31+
"axios": "^0.19.2",
3132
"console-testing-library": "^0.3.1",
3233
"eslint-config-react-app": "^5.0.1",
3334
"invariant": "^2.2.4",

src/createAsyncThunk.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ export function createAsyncThunk<
120120
arg: ThunkArg,
121121
thunkAPI: GetThunkAPI<ThunkApiConfig>
122122
) =>
123-
| Promise<Returned>
123+
| Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>>
124124
| Returned
125-
| Promise<RejectWithValue<GetRejectValue<ThunkApiConfig>>>
126125
| RejectWithValue<GetRejectValue<ThunkApiConfig>>
127126
) {
128127
type RejectedValue = GetRejectValue<ThunkApiConfig>

type-tests/files/createAsyncThunk.typetest.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { createAsyncThunk, Dispatch, createReducer, AnyAction } from 'src'
22
import { ThunkDispatch } from 'redux-thunk'
3-
import { unwrapResult } from 'src/createAsyncThunk'
3+
import { unwrapResult, SerializedError } from 'src/createAsyncThunk'
4+
5+
import apiRequest, { AxiosError } from 'axios'
6+
import { IsAny } from 'src/tsHelpers'
47

58
function expectType<T>(t: T) {
69
return t
@@ -122,3 +125,67 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
122125
expectType<ReturnValue>(returned.payload)
123126
}
124127
})()
128+
129+
// bogus testcase for discussion
130+
{
131+
interface Call {
132+
qwe: 'asd'
133+
}
134+
interface RootState {}
135+
136+
interface RejectedErrorPayload<T> {
137+
data: T
138+
error: string
139+
}
140+
141+
interface ValidationErrorsResponse extends CallsResponse {}
142+
143+
interface CallsResponse {
144+
data: Call[]
145+
}
146+
147+
const fetchLiveCallsError = createAsyncThunk<
148+
Call[],
149+
string,
150+
{
151+
state: RootState
152+
rejectValue: RejectedErrorPayload<ValidationErrorsResponse>
153+
}
154+
>('calls/fetchLiveCalls', async (organizationId, { rejectWithValue }) => {
155+
try {
156+
const {
157+
data: { data }
158+
} = await apiRequest.get<CallsResponse>(
159+
`organizations/${organizationId}/calls/live/iwill404`
160+
)
161+
return data
162+
} catch (err) {
163+
let error: AxiosError = err // cast for access to AxiosError properties
164+
return rejectWithValue({
165+
error: 'just a test message',
166+
data: error.response?.data
167+
})
168+
}
169+
})
170+
171+
defaultDispatch(fetchLiveCallsError('asd')).then(result => {
172+
if (fetchLiveCallsError.fulfilled.match(result)) {
173+
//success
174+
expectType<ReturnType<typeof fetchLiveCallsError['fulfilled']>>(result)
175+
expectType<Call[]>(result.payload)
176+
} else {
177+
expectType<ReturnType<typeof fetchLiveCallsError['rejected']>>(result)
178+
if (result.payload) {
179+
// rejected with value
180+
expectType<RejectedErrorPayload<ValidationErrorsResponse>>(
181+
result.payload
182+
)
183+
} else {
184+
// rejected by throw
185+
expectType<undefined>(result.payload)
186+
// result.error is `any`, because `miniSerializeError` currently returns any (if you pass in a non-object, it returns it and not a SerializedError)
187+
expectType<IsAny<typeof result['error'], true, false>>(true)
188+
}
189+
}
190+
})
191+
}

0 commit comments

Comments
 (0)