UNPKG

8.2 kBTypeScriptView Raw
1import type { Action } from 'redux';
2import type { IsUnknownOrNonInferrable, IfMaybeUndefined, IfVoid, IsAny } from './tsHelpers';
3/**
4 * An action with a string type and an associated payload. This is the
5 * type of action returned by `createAction()` action creators.
6 *
7 * @template P The type of the action's payload.
8 * @template T the type used for the action type.
9 * @template M The type of the action's meta (optional)
10 * @template E The type of the action's error (optional)
11 *
12 * @public
13 */
14export declare type PayloadAction<P = void, T extends string = string, M = never, E = never> = {
15 payload: P;
16 type: T;
17} & ([M] extends [never] ? {} : {
18 meta: M;
19}) & ([E] extends [never] ? {} : {
20 error: E;
21});
22/**
23 * A "prepare" method to be used as the second parameter of `createAction`.
24 * Takes any number of arguments and returns a Flux Standard Action without
25 * type (will be added later) that *must* contain a payload (might be undefined).
26 *
27 * @public
28 */
29export declare type PrepareAction<P> = ((...args: any[]) => {
30 payload: P;
31}) | ((...args: any[]) => {
32 payload: P;
33 meta: any;
34}) | ((...args: any[]) => {
35 payload: P;
36 error: any;
37}) | ((...args: any[]) => {
38 payload: P;
39 meta: any;
40 error: any;
41});
42/**
43 * Internal version of `ActionCreatorWithPreparedPayload`. Not to be used externally.
44 *
45 * @internal
46 */
47export declare type _ActionCreatorWithPreparedPayload<PA extends PrepareAction<any> | void, T extends string = string> = PA extends PrepareAction<infer P> ? ActionCreatorWithPreparedPayload<Parameters<PA>, P, T, ReturnType<PA> extends {
48 error: infer E;
49} ? E : never, ReturnType<PA> extends {
50 meta: infer M;
51} ? M : never> : void;
52/**
53 * Basic type for all action creators.
54 *
55 * @inheritdoc {redux#ActionCreator}
56 */
57export interface BaseActionCreator<P, T extends string, M = never, E = never> {
58 type: T;
59 match: (action: Action<unknown>) => action is PayloadAction<P, T, M, E>;
60}
61/**
62 * An action creator that takes multiple arguments that are passed
63 * to a `PrepareAction` method to create the final Action.
64 * @typeParam Args arguments for the action creator function
65 * @typeParam P `payload` type
66 * @typeParam T `type` name
67 * @typeParam E optional `error` type
68 * @typeParam M optional `meta` type
69 *
70 * @inheritdoc {redux#ActionCreator}
71 *
72 * @public
73 */
74export interface ActionCreatorWithPreparedPayload<Args extends unknown[], P, T extends string = string, E = never, M = never> extends BaseActionCreator<P, T, M, E> {
75 /**
76 * Calling this {@link redux#ActionCreator} with `Args` will return
77 * an Action with a payload of type `P` and (depending on the `PrepareAction`
78 * method used) a `meta`- and `error` property of types `M` and `E` respectively.
79 */
80 (...args: Args): PayloadAction<P, T, M, E>;
81}
82/**
83 * An action creator of type `T` that takes an optional payload of type `P`.
84 *
85 * @inheritdoc {redux#ActionCreator}
86 *
87 * @public
88 */
89export interface ActionCreatorWithOptionalPayload<P, T extends string = string> extends BaseActionCreator<P, T> {
90 /**
91 * Calling this {@link redux#ActionCreator} with an argument will
92 * return a {@link PayloadAction} of type `T` with a payload of `P`.
93 * Calling it without an argument will return a PayloadAction with a payload of `undefined`.
94 */
95 (payload?: P): PayloadAction<P, T>;
96}
97/**
98 * An action creator of type `T` that takes no payload.
99 *
100 * @inheritdoc {redux#ActionCreator}
101 *
102 * @public
103 */
104export interface ActionCreatorWithoutPayload<T extends string = string> extends BaseActionCreator<undefined, T> {
105 /**
106 * Calling this {@link redux#ActionCreator} will
107 * return a {@link PayloadAction} of type `T` with a payload of `undefined`
108 */
109 (noArgument: void): PayloadAction<undefined, T>;
110}
111/**
112 * An action creator of type `T` that requires a payload of type P.
113 *
114 * @inheritdoc {redux#ActionCreator}
115 *
116 * @public
117 */
118export interface ActionCreatorWithPayload<P, T extends string = string> extends BaseActionCreator<P, T> {
119 /**
120 * Calling this {@link redux#ActionCreator} with an argument will
121 * return a {@link PayloadAction} of type `T` with a payload of `P`
122 */
123 (payload: P): PayloadAction<P, T>;
124}
125/**
126 * An action creator of type `T` whose `payload` type could not be inferred. Accepts everything as `payload`.
127 *
128 * @inheritdoc {redux#ActionCreator}
129 *
130 * @public
131 */
132export interface ActionCreatorWithNonInferrablePayload<T extends string = string> extends BaseActionCreator<unknown, T> {
133 /**
134 * Calling this {@link redux#ActionCreator} with an argument will
135 * return a {@link PayloadAction} of type `T` with a payload
136 * of exactly the type of the argument.
137 */
138 <PT extends unknown>(payload: PT): PayloadAction<PT, T>;
139}
140/**
141 * An action creator that produces actions with a `payload` attribute.
142 *
143 * @typeParam P the `payload` type
144 * @typeParam T the `type` of the resulting action
145 * @typeParam PA if the resulting action is preprocessed by a `prepare` method, the signature of said method.
146 *
147 * @public
148 */
149export declare type PayloadActionCreator<P = void, T extends string = string, PA extends PrepareAction<P> | void = void> = IfPrepareActionMethodProvided<PA, _ActionCreatorWithPreparedPayload<PA, T>, IsAny<P, ActionCreatorWithPayload<any, T>, IsUnknownOrNonInferrable<P, ActionCreatorWithNonInferrablePayload<T>, IfVoid<P, ActionCreatorWithoutPayload<T>, IfMaybeUndefined<P, ActionCreatorWithOptionalPayload<P, T>, ActionCreatorWithPayload<P, T>>>>>>;
150/**
151 * A utility function to create an action creator for the given action type
152 * string. The action creator accepts a single argument, which will be included
153 * in the action object as a field called payload. The action creator function
154 * will also have its toString() overriden so that it returns the action type,
155 * allowing it to be used in reducer logic that is looking for that action type.
156 *
157 * @param type The action type to use for created actions.
158 * @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
159 * If this is given, the resulting action creator will pass its arguments to this method to calculate payload & meta.
160 *
161 * @public
162 */
163export declare function createAction<P = void, T extends string = string>(type: T): PayloadActionCreator<P, T>;
164/**
165 * A utility function to create an action creator for the given action type
166 * string. The action creator accepts a single argument, which will be included
167 * in the action object as a field called payload. The action creator function
168 * will also have its toString() overriden so that it returns the action type,
169 * allowing it to be used in reducer logic that is looking for that action type.
170 *
171 * @param type The action type to use for created actions.
172 * @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
173 * If this is given, the resulting action creator will pass its arguments to this method to calculate payload & meta.
174 *
175 * @public
176 */
177export declare function createAction<PA extends PrepareAction<any>, T extends string = string>(type: T, prepareAction: PA): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>;
178export declare function isFSA(action: unknown): action is {
179 type: string;
180 payload?: unknown;
181 error?: unknown;
182 meta?: unknown;
183};
184/**
185 * Returns the action type of the actions created by the passed
186 * `createAction()`-generated action creator (arbitrary action creators
187 * are not supported).
188 *
189 * @param action The action creator whose action type to get.
190 * @returns The action type used by the action creator.
191 *
192 * @public
193 */
194export declare function getType<T extends string>(actionCreator: PayloadActionCreator<any, T>): T;
195declare type IfPrepareActionMethodProvided<PA extends PrepareAction<any> | void, True, False> = PA extends (...args: any[]) => any ? True : False;
196export {};