import { Action } from 'redux' /** * An action with a string type and an associated payload. This is the * type of action returned by `createAction()` action creators. * * @template P The type of the action's payload. * @template T the type used for the action type. */ export interface PayloadAction
extends Action {
(): Action
type: T
}
/**
* A utility function to create an action creator for the given action type
* string. The action creator accepts a single argument, which will be included
* in the action object as a field called payload. The action creator function
* will also have its toString() overriden so that it returns the action type,
* allowing it to be used in reducer logic that is looking for that action type.
*
* @param type The action type to use for created actions.
*/
export function createAction (
type: T
): PayloadActionCreator {
function actionCreator(): Action
function actionCreator(payload?: P): Action {
return { type, payload }
}
actionCreator.toString = (): T => `${type}` as T
actionCreator.type = type
return actionCreator
}
/**
* Returns the action type of the actions created by the passed
* `createAction()`-generated action creator (arbitrary action creators
* are not supported).
*
* @param action The action creator whose action type to get.
* @returns The action type used by the action creator.
*/
export function getType