Skip to content

Commit 7c89e4a

Browse files
committed
redux action including init/replace types
1 parent 25f8a6c commit 7c89e4a

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/createStore.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Observer,
99
ExtendState
1010
} from './types/store'
11-
import { Action } from './types/actions'
11+
import { ReduxAction } from './types/actions'
1212
import { Reducer } from './types/reducers'
1313
import ActionTypes from './utils/actionTypes'
1414
import isPlainObject from './utils/isPlainObject'
@@ -40,7 +40,7 @@ import isPlainObject from './utils/isPlainObject'
4040
*/
4141
export default function createStore<
4242
S,
43-
A extends Action,
43+
A extends ReduxAction,
4444
Ext = {},
4545
StateExt = never
4646
>(
@@ -49,7 +49,7 @@ export default function createStore<
4949
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
5050
export default function createStore<
5151
S,
52-
A extends Action,
52+
A extends ReduxAction,
5353
Ext = {},
5454
StateExt = never
5555
>(
@@ -59,7 +59,7 @@ export default function createStore<
5959
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
6060
export default function createStore<
6161
S,
62-
A extends Action,
62+
A extends ReduxAction,
6363
Ext = {},
6464
StateExt = never
6565
>(
@@ -221,7 +221,7 @@ export default function createStore<
221221
* Note that, if you use a custom middleware, it may wrap `dispatch()` to
222222
* return something else (for example, a Promise you can await).
223223
*/
224-
function dispatch(action: A) {
224+
function dispatch(action: A | ReduxAction) {
225225
if (!isPlainObject(action)) {
226226
throw new Error(
227227
'Actions must be plain objects. ' +
@@ -242,7 +242,7 @@ export default function createStore<
242242

243243
try {
244244
isDispatching = true
245-
currentState = currentReducer(currentState, action)
245+
currentState = currentReducer(currentState, action as A)
246246
} finally {
247247
isDispatching = false
248248
}
@@ -283,7 +283,7 @@ export default function createStore<
283283
// Any reducers that existed in both the new and old rootReducer
284284
// will receive the previous state. This effectively populates
285285
// the new state tree with any relevant data from the old one.
286-
dispatch({ type: ActionTypes.REPLACE } as A)
286+
dispatch({ type: ActionTypes.REPLACE })
287287
// change the type of the store by casting it to the new store
288288
return (store as unknown) as Store<
289289
ExtendState<NewState, StateExt>,
@@ -337,7 +337,7 @@ export default function createStore<
337337
// When a store is created, an "INIT" action is dispatched so that every
338338
// reducer returns their initial state. This effectively populates
339339
// the initial state tree.
340-
dispatch({ type: ActionTypes.INIT } as A)
340+
dispatch({ type: ActionTypes.INIT })
341341

342342
const store = ({
343343
dispatch: dispatch as Dispatch<A>,

src/types/actions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ActionTypes from "src/utils/actionTypes";
2+
13
/**
24
* An *action* is a plain object that represents an intention to change the
35
* state. Actions are the only way to get data into the store. Any data,
@@ -19,6 +21,13 @@ export interface Action<T = any> {
1921
type: T
2022
}
2123

24+
/**
25+
* An action to assure that the reducer won't need unecessary internal coercion
26+
* and may assure more gracefully a reduce with all actions handled and no
27+
* unexpected state.
28+
*/
29+
export type ReduxAction = Action | { type: typeof ActionTypes.REPLACE } | { type: typeof ActionTypes.INIT }
30+
2231
/**
2332
* An Action type which accepts any other properties.
2433
* This is mainly for the use of the `Reducer` type.

0 commit comments

Comments
 (0)