Skip to content

redux action type including init/replace types #3582

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 8 additions & 8 deletions src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Observer,
ExtendState
} from './types/store'
import { Action } from './types/actions'
import { ReduxAction } from './types/actions'
import { Reducer } from './types/reducers'
import ActionTypes from './utils/actionTypes'
import isPlainObject from './utils/isPlainObject'
Expand Down Expand Up @@ -40,7 +40,7 @@ import isPlainObject from './utils/isPlainObject'
*/
export default function createStore<
S,
A extends Action,
A extends ReduxAction,
Ext = {},
StateExt = never
>(
Expand All @@ -49,7 +49,7 @@ export default function createStore<
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
export default function createStore<
S,
A extends Action,
A extends ReduxAction,
Ext = {},
StateExt = never
>(
Expand All @@ -59,7 +59,7 @@ export default function createStore<
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
export default function createStore<
S,
A extends Action,
A extends ReduxAction,
Ext = {},
StateExt = never
>(
Expand Down Expand Up @@ -221,7 +221,7 @@ export default function createStore<
* Note that, if you use a custom middleware, it may wrap `dispatch()` to
* return something else (for example, a Promise you can await).
*/
function dispatch(action: A) {
function dispatch(action: A | ReduxAction) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
Expand All @@ -242,7 +242,7 @@ export default function createStore<

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

const store = ({
dispatch: dispatch as Dispatch<A>,
Expand Down
9 changes: 9 additions & 0 deletions src/types/actions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ActionTypes from "src/utils/actionTypes";

/**
* An *action* is a plain object that represents an intention to change the
* state. Actions are the only way to get data into the store. Any data,
Expand All @@ -19,6 +21,13 @@ export interface Action<T = any> {
type: T
}

/**
* An action to assure that the reducer won't need unecessary internal coercion
* and may assure more gracefully a reduce with all actions handled and no
* unexpected state.
*/
export type ReduxAction = Action | { type: typeof ActionTypes.REPLACE } | { type: typeof ActionTypes.INIT }

/**
* An Action type which accepts any other properties.
* This is mainly for the use of the `Reducer` type.
Expand Down