Skip to content

Commit

Permalink
Type safety for dispatch signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciuca, Alexandru committed Mar 17, 2016
1 parent 175f32a commit 74e16aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export function combineReducers<S>(reducers: ReducersMapObject): Reducer<S>;

/* store */

export interface MiddlewareDispatch {
<TMiddlewareAction, TMiddlewareActionResult>(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
Expand All @@ -93,7 +97,9 @@ export function combineReducers<S>(reducers: ReducersMapObject): Reducer<S>;
* 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()`.
Expand Down Expand Up @@ -265,7 +271,7 @@ export interface MiddlewareAPI<S> {
* asynchronous API call into a series of synchronous actions.
*/
export interface Middleware {
<S>(api: MiddlewareAPI<S>): (next: Dispatch) => (action: any) => any;
<S>(api: MiddlewareAPI<S>): (next: MiddlewareDispatch) => MiddlewareDispatch;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const dispatchResult: Action = dispatch({type: 'TYPE'});

type Thunk<O> = () => O;

const dispatchThunkResult: number = dispatch(() => 42);
const dispatchThunkResult: number = dispatch<Thunk<number>, number>(() => 42);

0 comments on commit 74e16aa

Please sign in to comment.