Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow action to be typed with any #219

Merged
merged 2 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'))
}