|
2 | 2 | import type { Dispatch, AnyAction, Middleware, Reducer, Store } from 'redux'
|
3 | 3 | import { applyMiddleware } from 'redux'
|
4 | 4 | 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' |
6 | 10 | import type { ThunkMiddleware, ThunkAction } from 'redux-thunk'
|
7 | 11 | import thunk, { ThunkDispatch } from 'redux-thunk'
|
8 | 12 | import { expectNotAny, expectType } from './helpers'
|
@@ -187,6 +191,60 @@ const _anyMiddleware: any = () => () => () => {}
|
187 | 191 | store.dispatch(thunkA())
|
188 | 192 | // @ts-expect-error
|
189 | 193 | 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) |
190 | 248 | }
|
191 | 249 | /**
|
192 | 250 | * Test: removing the Thunk Middleware
|
|
0 commit comments