createAction.d.ts 7.5 KB

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