Skip to content

Commit

Permalink
Allow action to be typed with any (#219)
Browse files Browse the repository at this point in the history
* Allow action to be typed with any

* Adding more tests
  • Loading branch information
laat authored and timdorr committed Jan 13, 2019
1 parent 6142453 commit e546d62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Middleware, Action, AnyAction } from "redux";

export interface ThunkDispatch<S, E, A extends Action> {
<T extends A>(action: T): T;
<R>(thunkAction: ThunkAction<R, S, E, A>): R;
<T extends A>(action: T): T;
}

export type ThunkAction<R, S, E, A extends Action> = (
Expand Down
16 changes: 15 additions & 1 deletion test/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createStore, applyMiddleware } from 'redux';
import thunk, { ThunkAction, ThunkMiddleware } from '../index';
import thunk, { ThunkAction, ThunkMiddleware, ThunkDispatch } from '../index';

type State = {
foo: string;
Expand Down Expand Up @@ -73,3 +73,17 @@ storeThunkArg.dispatch((dispatch, getState, extraArg) => {
store.dispatch({ type: 'BAZ'});
console.log(extraArg);
});

const callDispatchAsync_anyAction = (dispatch: ThunkDispatch<State, undefined, any>) => {
const asyncThunk = (): ThunkResult<Promise<void>> => () => ({} as Promise<void>);
dispatch(asyncThunk()).then(() => console.log('done'))
}
const callDispatchAsync_specificActions = (dispatch: ThunkDispatch<State, undefined, Actions>) => {
const asyncThunk = (): ThunkResult<Promise<void>> => () => ({} as Promise<void>);
dispatch(asyncThunk()).then(() => console.log('done'))
}
const callDispatchAny = (dispatch: ThunkDispatch<State, undefined, Actions>) => {
const asyncThunk = (): any => () => ({} as Promise<void>);
dispatch(asyncThunk()) // result is any
.then(() => console.log('done'))
}

0 comments on commit e546d62

Please sign in to comment.