Skip to content

Commit d13ffde

Browse files
committed
Hopefully fix type definitions for empty thunk action params
- Made `ActionParams = void`, which allows not declaring any args in the payload creation function without TS complaining - Found out I can switch the args order back so it's `(args, other)`
1 parent 6054a27 commit d13ffde

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/createAsyncThunk.test.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createAsyncThunk } from './createAsyncThunk'
2+
import { configureStore } from './configureStore'
23

34
describe('createAsyncThunk', () => {
45
it('creates the action types', () => {
@@ -10,6 +11,27 @@ describe('createAsyncThunk', () => {
1011
expect(thunkActionCreator.rejected.type).toBe('testType/rejected')
1112
})
1213

14+
it('works without passing arguments to the payload creator', async () => {
15+
const thunkActionCreator = createAsyncThunk('testType', async () => 42)
16+
17+
let timesReducerCalled = 0
18+
19+
const reducer = () => {
20+
timesReducerCalled++
21+
}
22+
23+
const store = configureStore({
24+
reducer
25+
})
26+
27+
// reset from however many times the store called it
28+
timesReducerCalled = 0
29+
30+
await store.dispatch(thunkActionCreator())
31+
32+
expect(timesReducerCalled).toBe(3)
33+
})
34+
1335
it('accepts arguments and dispatches the actions on resolve', async () => {
1436
const dispatch = jest.fn()
1537

@@ -36,17 +58,17 @@ describe('createAsyncThunk', () => {
3658

3759
expect(dispatch).toHaveBeenNthCalledWith(
3860
1,
39-
thunkActionCreator.pending(args, generatedRequestId)
61+
thunkActionCreator.pending(generatedRequestId, args)
4062
)
4163

4264
expect(dispatch).toHaveBeenNthCalledWith(
4365
2,
44-
thunkActionCreator.fulfilled(result, args, generatedRequestId)
66+
thunkActionCreator.fulfilled(result, generatedRequestId, args)
4567
)
4668

4769
expect(dispatch).toHaveBeenNthCalledWith(
4870
3,
49-
thunkActionCreator.finished(args, generatedRequestId)
71+
thunkActionCreator.finished(generatedRequestId, args)
5072
)
5173
})
5274

@@ -72,17 +94,17 @@ describe('createAsyncThunk', () => {
7294

7395
expect(dispatch).toHaveBeenNthCalledWith(
7496
1,
75-
thunkActionCreator.pending(args, generatedRequestId)
97+
thunkActionCreator.pending(generatedRequestId, args)
7698
)
7799

78100
expect(dispatch).toHaveBeenNthCalledWith(
79101
2,
80-
thunkActionCreator.rejected(error, args, generatedRequestId)
102+
thunkActionCreator.rejected(error, generatedRequestId, args)
81103
)
82104

83105
expect(dispatch).toHaveBeenNthCalledWith(
84106
3,
85-
thunkActionCreator.finished(args, generatedRequestId)
107+
thunkActionCreator.finished(generatedRequestId, args)
86108
)
87109
})
88110
})

src/createAsyncThunk.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type AsyncThunksArgs<S, E, D extends Dispatch = Dispatch> = {
1919
export function createAsyncThunk<
2020
ActionType extends string,
2121
Returned,
22-
ActionParams = never,
22+
ActionParams = void,
2323
TA extends AsyncThunksArgs<any, any, any> = AsyncThunksArgs<
2424
unknown,
2525
unknown,
@@ -34,7 +34,7 @@ export function createAsyncThunk<
3434
) {
3535
const fulfilled = createAction(
3636
type + '/fulfilled',
37-
(result: Returned, args: ActionParams, requestId: string) => {
37+
(result: Returned, requestId: string, args: ActionParams) => {
3838
return {
3939
payload: result,
4040
meta: { args, requestId }
@@ -44,7 +44,7 @@ export function createAsyncThunk<
4444

4545
const pending = createAction(
4646
type + '/pending',
47-
(args: ActionParams, requestId: string) => {
47+
(requestId: string, args: ActionParams) => {
4848
return {
4949
payload: undefined,
5050
meta: { args, requestId }
@@ -54,7 +54,7 @@ export function createAsyncThunk<
5454

5555
const finished = createAction(
5656
type + '/finished',
57-
(args: ActionParams, requestId: string) => {
57+
(requestId: string, args: ActionParams) => {
5858
return {
5959
payload: undefined,
6060
meta: { args, requestId }
@@ -64,7 +64,7 @@ export function createAsyncThunk<
6464

6565
const rejected = createAction(
6666
type + '/rejected',
67-
(error: Error, args: ActionParams, requestId: string) => {
67+
(error: Error, requestId: string, args: ActionParams) => {
6868
return {
6969
payload: undefined,
7070
error,
@@ -82,7 +82,7 @@ export function createAsyncThunk<
8282
const requestId = nanoid()
8383

8484
try {
85-
dispatch(pending(args, requestId))
85+
dispatch(pending(requestId, args))
8686
// TODO Also ugly types
8787
const result = (await payloadCreator(args, {
8888
dispatch,
@@ -92,13 +92,13 @@ export function createAsyncThunk<
9292
} as TA)) as Returned
9393

9494
// TODO How do we avoid errors in here from hitting the catch clause?
95-
return dispatch(fulfilled(result, args, requestId))
95+
return dispatch(fulfilled(result, requestId, args))
9696
} catch (err) {
9797
// TODO Errors aren't serializable
98-
dispatch(rejected(err, args, requestId))
98+
dispatch(rejected(err, requestId, args))
9999
} finally {
100100
// TODO IS there really a benefit from a "finished" action?
101-
dispatch(finished(args, requestId))
101+
dispatch(finished(requestId, args))
102102
}
103103
}
104104
}

0 commit comments

Comments
 (0)