Skip to content

Commit e546d62

Browse files
laattimdorr
authored andcommitted
Allow action to be typed with any (#219)
* Allow action to be typed with any * Adding more tests
1 parent 6142453 commit e546d62

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Middleware, Action, AnyAction } from "redux";
22

33
export interface ThunkDispatch<S, E, A extends Action> {
4-
<T extends A>(action: T): T;
54
<R>(thunkAction: ThunkAction<R, S, E, A>): R;
5+
<T extends A>(action: T): T;
66
}
77

88
export type ThunkAction<R, S, E, A extends Action> = (

test/typescript.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createStore, applyMiddleware } from 'redux';
2-
import thunk, { ThunkAction, ThunkMiddleware } from '../index';
2+
import thunk, { ThunkAction, ThunkMiddleware, ThunkDispatch } from '../index';
33

44
type State = {
55
foo: string;
@@ -73,3 +73,17 @@ storeThunkArg.dispatch((dispatch, getState, extraArg) => {
7373
store.dispatch({ type: 'BAZ'});
7474
console.log(extraArg);
7575
});
76+
77+
const callDispatchAsync_anyAction = (dispatch: ThunkDispatch<State, undefined, any>) => {
78+
const asyncThunk = (): ThunkResult<Promise<void>> => () => ({} as Promise<void>);
79+
dispatch(asyncThunk()).then(() => console.log('done'))
80+
}
81+
const callDispatchAsync_specificActions = (dispatch: ThunkDispatch<State, undefined, Actions>) => {
82+
const asyncThunk = (): ThunkResult<Promise<void>> => () => ({} as Promise<void>);
83+
dispatch(asyncThunk()).then(() => console.log('done'))
84+
}
85+
const callDispatchAny = (dispatch: ThunkDispatch<State, undefined, Actions>) => {
86+
const asyncThunk = (): any => () => ({} as Promise<void>);
87+
dispatch(asyncThunk()) // result is any
88+
.then(() => console.log('done'))
89+
}

0 commit comments

Comments
 (0)