From e53c3246bc5050daccff26cbc3c81a84dea5e443 Mon Sep 17 00:00:00 2001 From: Andrew Luca Date: Tue, 10 Sep 2019 06:16:16 +0300 Subject: [PATCH] feat(redux-api-middleware): overload redux/Dispatch signature (#38175) --- types/redux-api-middleware/index.d.ts | 17 +++++++++++++++-- .../redux-api-middleware-tests.ts | 12 ++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/types/redux-api-middleware/index.d.ts b/types/redux-api-middleware/index.d.ts index 76973c993c..5f7012def8 100644 --- a/types/redux-api-middleware/index.d.ts +++ b/types/redux-api-middleware/index.d.ts @@ -74,13 +74,13 @@ export interface RSAARequestTypeDescriptor { type: string | symbol; - payload?: ((action: RSAAAction, state: State, res: any) => Payload) | Payload; + payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload; meta?: ((action: RSAAAction, state: State, res: any) => Meta) | Meta; } export interface RSAAFailureTypeDescriptor { type: string | symbol; - payload?: ((action: RSAAAction, state: State) => Payload) | Payload; + payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload; meta?: ((action: RSAAAction, state: State, res: any) => Meta) | Meta; } @@ -105,4 +105,17 @@ export interface RSAAAction { [RSAA]: RSAACall; } +/** + * Redux behaviour changed by middleware, so overloads here + */ +declare module 'redux' { + /* + * Overload to add api middleware support to Redux's dispatch() function. + * Useful for react-redux or any other library which could use this type. + */ + interface Dispatch { + (action: RSAAAction): Promise; + } +} + export {}; diff --git a/types/redux-api-middleware/redux-api-middleware-tests.ts b/types/redux-api-middleware/redux-api-middleware-tests.ts index c063791956..abb20cfd38 100644 --- a/types/redux-api-middleware/redux-api-middleware-tests.ts +++ b/types/redux-api-middleware/redux-api-middleware-tests.ts @@ -1,3 +1,4 @@ +import { createStore, Store } from 'redux'; import { RSAA, isRSAA, @@ -134,3 +135,14 @@ import { types: ['REQ_TYPE', 'SUCCESS_TYPE', 'FAILURE_TYPE']; } } + +{ + const store: Store = createStore(() => undefined); + store.dispatch({ + [RSAA]: { + endpoint: '/test/endpoint', + method: 'GET', + types: ['REQ_TYPE', 'SUCCESS_TYPE', 'FAILURE_TYPE'], + } + }); +}