DefinitelyTyped/types/redux-promise-middleware/redux-promise-middleware-tests.ts
Paul Morelle 0e7cc3ff0c redux-promise-middleware: fix Dispatch return type
When the payload is a Promise, the Promise Middleware will change what
`dispatch()` returns. Reflect this with typing.
According to documentation [1], it is a Promise of an object with the
promised `value` and the FULFILLED action `type`.

[1] https://github.com/pburtchaell/redux-promise-middleware/blob/master/docs/guides/chaining-actions.md
2018-09-13 12:00:44 -04:00

45 lines
1.2 KiB
TypeScript

import { createStore, applyMiddleware, Store, Dispatch } from "redux";
import promiseMiddleware from "redux-promise-middleware";
declare var rootReducer: (state: any, action: any) => any;
declare var doSomethingAsyncAndReturnPromise: any;
declare var someActionCreator: any;
const createStoreWithMiddleware = applyMiddleware(
promiseMiddleware()
)(createStore);
const store: Store<any> = createStoreWithMiddleware(rootReducer);
export function myAsyncActionCreator(data: any) {
return {
type: "ACTION",
payload: {
promise: doSomethingAsyncAndReturnPromise(data),
data: data
}
};
}
const actionCreator1 = () => ({
type: "FIRST_ACTION_TYPE",
payload: {
promise: Promise.resolve({
type: "SECOND_ACTION_TYPE",
payload: "...",
})
}
});
const actionCreator2 = () => ({
type: "FIRST_ACTION_TYPE",
payload: {
promise: Promise.resolve((action: string, dispatch: Dispatch<any>, getState: Function) => {
dispatch({ type: "SECEOND_ACTION_TYPE", payload: "..." });
dispatch(someActionCreator());
dispatch({ type: "CHAINED_ACTION", payload: Promise.resolve("...") })
.then(({ type, value }) => {})
})
}
});