Skip to content

Commit 5d03b97

Browse files
committed
ThunkActionType prototype
1 parent d0a05b2 commit 5d03b97

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { action, ActionType } from 'typesafe-actions';
2+
3+
import { Dispatch, ActionCreatorsMapObject } from 'redux';
4+
import * as actions from './actions';
5+
6+
// CLASSIC API
7+
export const thunkIncrement = () => (dispatch: Dispatch) =>
8+
dispatch(action('THUNK_INCREMENT'));
9+
export const thunkAdd = (amount: number) => (dispatch: Dispatch) =>
10+
dispatch(action('THUNK_ADD', amount));
11+
12+
type ThunkActionType<M extends ActionCreatorsMapObject<any>> = ActionType<
13+
{
14+
[N in keyof M]: ReturnType<M[N]> extends (
15+
dispatch: Dispatch
16+
) => { type: string }
17+
? (...args: Parameters<M[N]>) => ReturnType<ReturnType<M[N]>>
18+
: M[N]
19+
}
20+
>;
21+
22+
const thunkActions = { thunkIncrement, thunkAdd, ...actions };
23+
export type Action = ThunkActionType<typeof thunkActions>;
24+
// type Action = EmptyAction<"THUNK_INCREMENT"> | PayloadAction<"THUNK_ADD", number> | EmptyAction<"counters/INCREMENT"> | PayloadAction<"counters/ADD", number>

0 commit comments

Comments
 (0)