Skip to content

Commit 3178815

Browse files
author
Hendrik Liebau
committed
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
1 parent 00cc012 commit 3178815

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

index.d.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import {Middleware, Dispatch} from "redux";
1+
import { Action, Middleware, Dispatch as ReduxDispatch, Store as ReduxStore } from "redux";
22

3-
4-
export type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState: () => S,
5-
extraArgument: E) => R;
6-
7-
declare module "redux" {
8-
export interface Dispatch<S> {
3+
export interface Dispatch<S> extends ReduxDispatch<S> {
94
<R, E>(asyncAction: ThunkAction<R, S, E>): R;
10-
}
115
}
126

7+
export interface Store<S> extends ReduxStore<S> {
8+
dispatch: Dispatch<S>;
9+
}
10+
11+
export type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState: () => S,
12+
extraArgument: E) => R;
1313

1414
declare const thunk: Middleware & {
15-
withExtraArgument(extraArgument: any): Middleware;
15+
withExtraArgument(extraArgument: any): Middleware;
1616
};
1717

1818
export default thunk;

test/typescript.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {Store, Middleware} from 'redux';
2-
import thunk, {ThunkAction} from '../index.d.ts';
3-
1+
import { Action, Dispatch as ReduxDispatch, Middleware } from 'redux';
2+
import thunk, { Dispatch, ThunkAction, Store } from '../index.d.ts';
43

54
declare const store: Store<{foo: string}>;
65

@@ -31,3 +30,7 @@ const thunkAction: ThunkAction<void, {foo: string}, {bar: number}> =
3130
const thunkActionDispatchOnly: ThunkAction<void, {}, {}> = dispatch => {
3231
dispatch({type: 'FOO'});
3332
};
33+
34+
export const otherMiddleware: Middleware = (store: Store<any>) => (next: ReduxDispatch<any>) => (action: Action) => {
35+
next(action);
36+
};

0 commit comments

Comments
 (0)