From 74e16aae1c8fffcca9b38d8bf90c685267d702a5 Mon Sep 17 00:00:00 2001 From: "Ciuca, Alexandru" Date: Thu, 17 Mar 2016 12:12:16 +0200 Subject: [PATCH] Type safety for dispatch signature --- index.d.ts | 10 ++++++++-- test/typescript/dispatch.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index ad6973eb71..b91207af14 100644 --- a/index.d.ts +++ b/index.d.ts @@ -75,6 +75,10 @@ export function combineReducers(reducers: ReducersMapObject): Reducer; /* store */ +export interface MiddlewareDispatch { + (action: TMiddlewareAction): TMiddlewareActionResult; +} + /** * A *dispatching function* (or simply *dispatch function*) is a function that * accepts an action or an async action; it then may or may not dispatch one @@ -93,7 +97,9 @@ export function combineReducers(reducers: ReducersMapObject): Reducer; * transform, delay, ignore, or otherwise interpret actions or async actions * before passing them to the next middleware. */ -export type Dispatch = (action: any) => any; +export interface Dispatch extends MiddlewareDispatch { + (action: Action): Action; +} /** * Function to remove listener added by `Store.subscribe()`. @@ -265,7 +271,7 @@ export interface MiddlewareAPI { * asynchronous API call into a series of synchronous actions. */ export interface Middleware { - (api: MiddlewareAPI): (next: Dispatch) => (action: any) => any; + (api: MiddlewareAPI): (next: MiddlewareDispatch) => MiddlewareDispatch; } /** diff --git a/test/typescript/dispatch.ts b/test/typescript/dispatch.ts index b589592f1b..7cdfb2e254 100644 --- a/test/typescript/dispatch.ts +++ b/test/typescript/dispatch.ts @@ -9,4 +9,4 @@ const dispatchResult: Action = dispatch({type: 'TYPE'}); type Thunk = () => O; -const dispatchThunkResult: number = dispatch(() => 42); +const dispatchThunkResult: number = dispatch, number>(() => 42);