Skip to content

Commit e5ef8e2

Browse files
committed
Add typetests for thunk return type
1 parent 98cbd26 commit e5ef8e2

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

packages/toolkit/src/configureStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export interface EnhancedStore<
110110
*
111111
* @inheritdoc
112112
*/
113-
dispatch: DispatchForMiddlewares<M> & Dispatch<A>
113+
dispatch: Dispatch<A> & DispatchForMiddlewares<M>
114114
}
115115

116116
/**

packages/toolkit/src/tests/configureStore.typetest.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import type { Dispatch, AnyAction, Middleware, Reducer, Store } from 'redux'
33
import { applyMiddleware } from 'redux'
44
import type { PayloadAction } from '@reduxjs/toolkit'
5-
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'
5+
import {
6+
configureStore,
7+
getDefaultMiddleware,
8+
createSlice,
9+
} from '@reduxjs/toolkit'
610
import type { ThunkMiddleware, ThunkAction } from 'redux-thunk'
711
import thunk, { ThunkDispatch } from 'redux-thunk'
812
import { expectNotAny, expectType } from './helpers'
@@ -187,6 +191,60 @@ const _anyMiddleware: any = () => () => () => {}
187191
store.dispatch(thunkA())
188192
// @ts-expect-error
189193
store.dispatch(thunkB())
194+
195+
const res = store.dispatch((dispatch, getState) => {
196+
return 42
197+
})
198+
199+
const action = store.dispatch({ type: 'foo' })
200+
}
201+
/**
202+
* Test: return type of thunks and actions is inferred correctly
203+
*/
204+
{
205+
const slice = createSlice({
206+
name: 'counter',
207+
initialState: {
208+
value: 0,
209+
},
210+
reducers: {
211+
incrementByAmount: (state, action: PayloadAction<number>) => {
212+
state.value += action.payload
213+
},
214+
},
215+
})
216+
217+
const store = configureStore({
218+
reducer: {
219+
counter: slice.reducer,
220+
},
221+
})
222+
223+
const action = slice.actions.incrementByAmount(2)
224+
225+
const dispatchResult = store.dispatch(action)
226+
expectType<{ type: string; payload: number }>(dispatchResult)
227+
228+
const promiseResult = store.dispatch(async (dispatch) => {
229+
return 42
230+
})
231+
232+
expectType<Promise<number>>(promiseResult)
233+
234+
const store2 = configureStore({
235+
reducer: {
236+
counter: slice.reducer,
237+
},
238+
middleware: (gDM) =>
239+
gDM({
240+
thunk: {
241+
extraArgument: 42,
242+
},
243+
}),
244+
})
245+
246+
const dispatchResult2 = store2.dispatch(action)
247+
expectType<{ type: string; payload: number }>(dispatchResult2)
190248
}
191249
/**
192250
* Test: removing the Thunk Middleware

0 commit comments

Comments
 (0)