From 69ad11b449d40e424fe683be32e2850ed882edba Mon Sep 17 00:00:00 2001 From: Hendrik Liebau Date: Thu, 7 Jul 2016 17:54:47 +0200 Subject: [PATCH] declare own Dispatch and Store interfaces The current implementation of the typings makes it impossible to create a middleware that only handles standard actions. This means a middleware must handle thunked actions even though it never actually handles them during runtime. (see issue and examples in #82) By declaring the Dispatch and Store interfaces again instead of overloading the original implementation of redux a consumer of redux-thunk can choose the right implementation for his use case (e.g. ActionCreator vs. Middleware). fixes #82 --- index.d.ts | 18 +++++++++--------- test/typescript.ts | 9 ++++++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index 24cf875..d2e6458 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,18 +1,18 @@ -import {Middleware, Dispatch} from "redux"; +import { Action, Middleware, Dispatch as ReduxDispatch, Store as ReduxStore } from "redux"; - -export type ThunkAction = (dispatch: Dispatch, getState: () => S, - extraArgument: E) => R; - -declare module "redux" { - export interface Dispatch { +export interface Dispatch extends ReduxDispatch { (asyncAction: ThunkAction): R; - } } +export interface Store extends ReduxStore { + dispatch: Dispatch; +} + +export type ThunkAction = (dispatch: Dispatch, getState: () => S, + extraArgument: E) => R; declare const thunk: Middleware & { - withExtraArgument(extraArgument: any): Middleware; + withExtraArgument(extraArgument: any): Middleware; }; export default thunk; diff --git a/test/typescript.ts b/test/typescript.ts index 816bf47..a43e5e2 100644 --- a/test/typescript.ts +++ b/test/typescript.ts @@ -1,6 +1,5 @@ -import {Store, Middleware} from 'redux'; -import thunk, {ThunkAction} from '../index.d.ts'; - +import { Action, Dispatch as ReduxDispatch, Middleware } from 'redux'; +import thunk, { Dispatch, ThunkAction, Store } from '../index.d.ts'; declare const store: Store<{foo: string}>; @@ -31,3 +30,7 @@ const thunkAction: ThunkAction = const thunkActionDispatchOnly: ThunkAction = dispatch => { dispatch({type: 'FOO'}); }; + +export const otherMiddleware: Middleware = (store: Store) => (next: ReduxDispatch) => (action: Action) => { + next(action); +};