* Store enhancer that extends the type of dispatch.
()
| 9 | * Store enhancer that extends the type of dispatch. |
| 10 | */ |
| 11 | function dispatchExtension() { |
| 12 | type PromiseDispatch = <T extends Action>(promise: Promise<T>) => Promise<T> |
| 13 | |
| 14 | const enhancer: StoreEnhancer<{ |
| 15 | dispatch: PromiseDispatch |
| 16 | }> = |
| 17 | createStore => |
| 18 | <S, A extends Action, PreloadedState>( |
| 19 | reducer: Reducer<S, A, PreloadedState>, |
| 20 | preloadedState?: PreloadedState | undefined |
| 21 | ) => { |
| 22 | const store = createStore(reducer, preloadedState) |
| 23 | return { |
| 24 | ...store, |
| 25 | dispatch: (action: any) => { |
| 26 | if (action.type) { |
| 27 | store.dispatch(action) |
| 28 | } else if (action.then) { |
| 29 | action.then(store.dispatch) |
| 30 | } |
| 31 | return action |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | const store = createStore(reducer, enhancer) |
| 37 | |
| 38 | store.dispatch({ type: 'INCREMENT' }) |
| 39 | store.dispatch(Promise.resolve({ type: 'INCREMENT' })) |
| 40 | // @ts-expect-error |
| 41 | store.dispatch('not-an-action') |
| 42 | // @ts-expect-error |
| 43 | store.dispatch(Promise.resolve('not-an-action')) |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Store enhancer that extends the type of the state. |
nothing calls this directly
no test coverage detected