Skip to content

Commit 42dcd37

Browse files
committed
updated action and action creator helper types
1 parent a74129a commit 42dcd37

File tree

6 files changed

+48
-40
lines changed

6 files changed

+48
-40
lines changed

README.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,16 @@ export const INCREMENT_SFC = 'INCREMENT_SFC';
641641
export const DECREMENT_SFC = 'DECREMENT_SFC';
642642

643643
export type Actions = {
644-
INCREMENT_SFC: { type: typeof INCREMENT_SFC },
645-
DECREMENT_SFC: { type: typeof DECREMENT_SFC },
644+
INCREMENT_SFC: {
645+
type: typeof INCREMENT_SFC,
646+
},
647+
DECREMENT_SFC: {
648+
type: typeof DECREMENT_SFC,
649+
},
646650
};
647651

648-
export type Action = Actions[keyof Actions];
649-
650652
// Action Creators
651-
export const actionCreators = {
653+
export const actionCreatorss = {
652654
incrementSfc: (): Actions[typeof INCREMENT_SFC] => ({
653655
type: INCREMENT_SFC,
654656
}),
@@ -680,20 +682,24 @@ A more DRY approach, introducing a simple factory function to automate the creat
680682
```ts
681683
import { createActionCreator } from 'react-redux-typescript';
682684

685+
type Severity = 'info' | 'success' | 'warning' | 'error';
686+
683687
// Action Creators
684688
export const actionCreators = {
685-
increaseCounter: createActionCreator('INCREASE_COUNTER'), // { type: "INCREASE_COUNTER" }
686-
showNotification: createActionCreator('SHOW_NOTIFICATION', (payload: string, meta?: { severity: string }) => payload),
689+
incrementCounter: createActionCreator('INCREMENT_COUNTER'),
690+
showNotification: createActionCreator(
691+
'SHOW_NOTIFICATION', (message: string, severity?: Severity) => ({ message, severity }),
692+
),
687693
};
688694

689695
// Examples
690-
store.dispatch(actionCreators.increaseCounter(4)); // Error: Expected 0 arguments, but got 1.
691-
store.dispatch(actionCreators.increaseCounter()); // OK: { type: "INCREASE_COUNTER" }
692-
actionCreators.increaseCounter.type === "INCREASE_COUNTER" // true
696+
store.dispatch(actionCreators.incrementCounter(4)); // Error: Expected 0 arguments, but got 1.
697+
store.dispatch(actionCreators.incrementCounter()); // OK: { type: "INCREMENT_COUNTER" }
698+
actionCreators.incrementCounter.type === "INCREMENT_COUNTER" // true
693699

694700
store.dispatch(actionCreators.showNotification()); // Error: Supplied parameters do not match any signature of call target.
695-
store.dispatch(actionCreators.showNotification('Hello!')); // OK: { type: "SHOW_NOTIFICATION", payload: 'Hello!' }
696-
store.dispatch(actionCreators.showNotification('Hello!', { severity: 'warning' })); // OK: { type: "SHOW_NOTIFICATION", payload: 'Hello!', meta: { type: 'warning' } }
701+
store.dispatch(actionCreators.showNotification('Hello!')); // OK: { type: "SHOW_NOTIFICATION", payload: { message: 'Hello!' } }
702+
store.dispatch(actionCreators.showNotification('Hello!', 'info')); // OK: { type: "SHOW_NOTIFICATION", payload: { message: 'Hello!', severity: 'info } }
697703
actionCreators.showNotification.type === "SHOW_NOTIFICATION" // true
698704
```
699705

@@ -813,17 +819,17 @@ export default function reducer(state = 0, action: RootAction): State {
813819
// RootActions
814820
import { RouterAction, LocationChangeAction } from 'react-router-redux';
815821

816-
import { Action as CountersAction } from '@src/redux/counters';
817-
import { Action as TodosAction } from '@src/redux/todos';
818-
import { Action as ToastsAction } from '@src/redux/toasts';
822+
import { Actions as CountersActions } from '@src/redux/counters';
823+
import { Actions as TodosActions } from '@src/redux/todos';
824+
import { Actions as ToastsActions } from '@src/redux/toasts';
819825

820826
type ReactRouterAction = RouterAction | LocationChangeAction;
821827

822828
export type RootAction =
823829
| ReactRouterAction
824-
| CountersAction
825-
| TodosAction
826-
| ToastsAction;
830+
| CountersActions[keyof CountersActions]
831+
| TodosActions[keyof TodosActions]
832+
| ToastsActions[keyof ToastsActions];
827833

828834
```
829835

generator/content/2_redux.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ A more DRY approach, introducing a simple factory function to automate the creat
2323
```ts
2424
import { createActionCreator } from 'react-redux-typescript';
2525

26+
type Severity = 'info' | 'success' | 'warning' | 'error';
27+
2628
// Action Creators
2729
export const actionCreators = {
28-
increaseCounter: createActionCreator('INCREASE_COUNTER'), // { type: "INCREASE_COUNTER" }
29-
showNotification: createActionCreator('SHOW_NOTIFICATION', (payload: string, meta?: { severity: string }) => payload),
30+
incrementCounter: createActionCreator('INCREMENT_COUNTER'),
31+
showNotification: createActionCreator(
32+
'SHOW_NOTIFICATION', (message: string, severity?: Severity) => ({ message, severity }),
33+
),
3034
};
3135

3236
// Examples
33-
store.dispatch(actionCreators.increaseCounter(4)); // Error: Expected 0 arguments, but got 1.
34-
store.dispatch(actionCreators.increaseCounter()); // OK: { type: "INCREASE_COUNTER" }
35-
actionCreators.increaseCounter.type === "INCREASE_COUNTER" // true
37+
store.dispatch(actionCreators.incrementCounter(4)); // Error: Expected 0 arguments, but got 1.
38+
store.dispatch(actionCreators.incrementCounter()); // OK: { type: "INCREMENT_COUNTER" }
39+
actionCreators.incrementCounter.type === "INCREMENT_COUNTER" // true
3640

3741
store.dispatch(actionCreators.showNotification()); // Error: Supplied parameters do not match any signature of call target.
38-
store.dispatch(actionCreators.showNotification('Hello!')); // OK: { type: "SHOW_NOTIFICATION", payload: 'Hello!' }
39-
store.dispatch(actionCreators.showNotification('Hello!', { severity: 'warning' })); // OK: { type: "SHOW_NOTIFICATION", payload: 'Hello!', meta: { type: 'warning' } }
42+
store.dispatch(actionCreators.showNotification('Hello!')); // OK: { type: "SHOW_NOTIFICATION", payload: { message: 'Hello!' } }
43+
store.dispatch(actionCreators.showNotification('Hello!', 'info')); // OK: { type: "SHOW_NOTIFICATION", payload: { message: 'Hello!', severity: 'info } }
4044
actionCreators.showNotification.type === "SHOW_NOTIFICATION" // true
4145
```
4246

playground/src/redux/counters/actions.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ export const INCREMENT_SFC = 'INCREMENT_SFC';
22
export const DECREMENT_SFC = 'DECREMENT_SFC';
33

44
export type Actions = {
5-
INCREMENT_SFC: { type: typeof INCREMENT_SFC },
6-
DECREMENT_SFC: { type: typeof DECREMENT_SFC },
5+
INCREMENT_SFC: {
6+
type: typeof INCREMENT_SFC,
7+
},
8+
DECREMENT_SFC: {
9+
type: typeof DECREMENT_SFC,
10+
},
711
};
812

9-
export type Action = Actions[keyof Actions];
10-
1113
// Action Creators
12-
export const actionCreators = {
14+
export const actionCreatorss = {
1315
incrementSfc: (): Actions[typeof INCREMENT_SFC] => ({
1416
type: INCREMENT_SFC,
1517
}),

playground/src/redux/root-action.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// RootActions
22
import { RouterAction, LocationChangeAction } from 'react-router-redux';
33

4-
import { Action as CountersAction } from '@src/redux/counters';
5-
import { Action as TodosAction } from '@src/redux/todos';
6-
import { Action as ToastsAction } from '@src/redux/toasts';
4+
import { Actions as CountersActions } from '@src/redux/counters';
5+
import { Actions as TodosActions } from '@src/redux/todos';
6+
import { Actions as ToastsActions } from '@src/redux/toasts';
77

88
type ReactRouterAction = RouterAction | LocationChangeAction;
99

1010
export type RootAction =
1111
| ReactRouterAction
12-
| CountersAction
13-
| TodosAction
14-
| ToastsAction;
12+
| CountersActions[keyof CountersActions]
13+
| TodosActions[keyof TodosActions]
14+
| ToastsActions[keyof ToastsActions];

playground/src/redux/toasts/actions.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export type Actions = {
88
REMOVE_TOAST: { type: typeof REMOVE_TOAST, payload: string },
99
};
1010

11-
export type Action = Actions[keyof Actions];
12-
1311
// Action Creators
1412
export const actionCreators = {
1513
addToast: (payload: IToast): Actions[typeof ADD_TOAST] => ({

playground/src/redux/todos/actions.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export type Actions = {
1010
CHANGE_TODOS_FILTER: { type: typeof CHANGE_TODOS_FILTER, payload: ITodosFilter },
1111
};
1212

13-
export type Action = Actions[keyof Actions];
14-
1513
// Action Creators
1614
export const actionCreators = {
1715
addTodo: (payload: string): Actions[typeof ADD_TODO] => ({

0 commit comments

Comments
 (0)