• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * An *action* is a plain object that represents an intention to change the
3 * state. Actions are the only way to get data into the store. Any data,
4 * whether from UI events, network callbacks, or other sources such as
5 * WebSockets needs to eventually be dispatched as actions.
6 *
7 * Actions must have a `type` field that indicates the type of action being
8 * performed. Types can be defined as constants and imported from another
9 * module. It's better to use strings for `type` than Symbols because strings
10 * are serializable.
11 *
12 * Other than `type`, the structure of an action object is really up to you.
13 * If you're interested, check out Flux Standard Action for recommendations on
14 * how actions should be constructed.
15 *
16 * @template T the type of the action's `type` tag.
17 */
18export interface Action<T = any> {
19  type: T
20}
21
22/**
23 * An Action type which accepts any other properties.
24 * This is mainly for the use of the `Reducer` type.
25 * This is not part of `Action` itself to prevent types that extend `Action` from
26 * having an index signature.
27 */
28export interface AnyAction extends Action {
29  // Allows any extra properties to be defined in an action.
30  [extraProps: string]: any
31}
32
33/* action creators */
34
35/**
36 * An *action creator* is, quite simply, a function that creates an action. Do
37 * not confuse the two terms—again, an action is a payload of information, and
38 * an action creator is a factory that creates an action.
39 *
40 * Calling an action creator only produces an action, but does not dispatch
41 * it. You need to call the store's `dispatch` function to actually cause the
42 * mutation. Sometimes we say *bound action creators* to mean functions that
43 * call an action creator and immediately dispatch its result to a specific
44 * store instance.
45 *
46 * If an action creator needs to read the current state, perform an API call,
47 * or cause a side effect, like a routing transition, it should return an
48 * async action instead of an action.
49 *
50 * @template A Returned action type.
51 */
52export interface ActionCreator<A, P extends any[] = any[]> {
53  (...args: P): A
54}
55
56/**
57 * Object whose values are action creator functions.
58 */
59export interface ActionCreatorsMapObject<A = any, P extends any[] = any[]> {
60  [key: string]: ActionCreator<A, P>
61}
62