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
This commit is contained in:
Paul Morelle
2018-09-13 12:00:44 -04:00
parent 5f7be1fd62
commit 0e7cc3ff0c
2 changed files with 18 additions and 1 deletions
+16
View File
@@ -2,7 +2,23 @@
// Project: https://github.com/pburtchaell/redux-promise-middleware
// Definitions by: ianks <https://github.com/ianks>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
// TypeScript Version: 2.8
import { Middleware } from 'redux';
export default function promiseMiddleware(config?: { promiseTypeSuffixes?: string[], promiseTypeDelimiter?: string }): Middleware;
declare module "redux" {
export interface PromiseAction<R> extends Action {
payload: Promise<R>;
}
type PayloadType<PA> = PA extends PromiseAction<infer R> ? R : never;
export interface Dispatch<S> {
<R, PA extends PromiseAction<R>>(action: PA): Promise<{
value: PayloadType<PA>;
type: string;
}>;
}
}
@@ -2,7 +2,6 @@ import { createStore, applyMiddleware, Store, Dispatch } from "redux";
import promiseMiddleware from "redux-promise-middleware";
declare var rootReducer: (state: any, action: any) => any;
declare var Promise: any;
declare var doSomethingAsyncAndReturnPromise: any;
declare var someActionCreator: any;
@@ -38,6 +37,8 @@ const actionCreator2 = () => ({
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 }) => {})
})
}
});