redux.d.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. These must be strings, as strings are serializable.
  10. *
  11. * Other than `type`, the structure of an action object is really up to you.
  12. * If you're interested, check out Flux Standard Action for recommendations on
  13. * how actions should be constructed.
  14. *
  15. * @template T the type of the action's `type` tag.
  16. */
  17. type Action<T extends string = string> = {
  18. type: T;
  19. };
  20. /**
  21. * An Action type which accepts any other properties.
  22. * This is mainly for the use of the `Reducer` type.
  23. * This is not part of `Action` itself to prevent types that extend `Action` from
  24. * having an index signature.
  25. */
  26. interface UnknownAction extends Action {
  27. [extraProps: string]: unknown;
  28. }
  29. /**
  30. * An Action type which accepts any other properties.
  31. * This is mainly for the use of the `Reducer` type.
  32. * This is not part of `Action` itself to prevent types that extend `Action` from
  33. * having an index signature.
  34. * @deprecated use Action or UnknownAction instead
  35. */
  36. interface AnyAction extends Action {
  37. [extraProps: string]: any;
  38. }
  39. /**
  40. * An *action creator* is, quite simply, a function that creates an action. Do
  41. * not confuse the two terms—again, an action is a payload of information, and
  42. * an action creator is a factory that creates an action.
  43. *
  44. * Calling an action creator only produces an action, but does not dispatch
  45. * it. You need to call the store's `dispatch` function to actually cause the
  46. * mutation. Sometimes we say *bound action creators* to mean functions that
  47. * call an action creator and immediately dispatch its result to a specific
  48. * store instance.
  49. *
  50. * If an action creator needs to read the current state, perform an API call,
  51. * or cause a side effect, like a routing transition, it should return an
  52. * async action instead of an action.
  53. *
  54. * @template A Returned action type.
  55. */
  56. interface ActionCreator<A, P extends any[] = any[]> {
  57. (...args: P): A;
  58. }
  59. /**
  60. * Object whose values are action creator functions.
  61. */
  62. interface ActionCreatorsMapObject<A = any, P extends any[] = any[]> {
  63. [key: string]: ActionCreator<A, P>;
  64. }
  65. /**
  66. * A *reducer* is a function that accepts
  67. * an accumulation and a value and returns a new accumulation. They are used
  68. * to reduce a collection of values down to a single value
  69. *
  70. * Reducers are not unique to Redux—they are a fundamental concept in
  71. * functional programming. Even most non-functional languages, like
  72. * JavaScript, have a built-in API for reducing. In JavaScript, it's
  73. * `Array.prototype.reduce()`.
  74. *
  75. * In Redux, the accumulated value is the state object, and the values being
  76. * accumulated are actions. Reducers calculate a new state given the previous
  77. * state and an action. They must be *pure functions*—functions that return
  78. * the exact same output for given inputs. They should also be free of
  79. * side-effects. This is what enables exciting features like hot reloading and
  80. * time travel.
  81. *
  82. * Reducers are the most important concept in Redux.
  83. *
  84. * *Do not put API calls into reducers.*
  85. *
  86. * @template S The type of state consumed and produced by this reducer.
  87. * @template A The type of actions the reducer can potentially respond to.
  88. * @template PreloadedState The type of state consumed by this reducer the first time it's called.
  89. */
  90. type Reducer<S = any, A extends Action = UnknownAction, PreloadedState = S> = (state: S | PreloadedState | undefined, action: A) => S;
  91. /**
  92. * Object whose values correspond to different reducer functions.
  93. *
  94. * @template S The combined state of the reducers.
  95. * @template A The type of actions the reducers can potentially respond to.
  96. * @template PreloadedState The combined preloaded state of the reducers.
  97. */
  98. type ReducersMapObject<S = any, A extends Action = UnknownAction, PreloadedState = S> = keyof PreloadedState extends keyof S ? {
  99. [K in keyof S]: Reducer<S[K], A, K extends keyof PreloadedState ? PreloadedState[K] : never>;
  100. } : never;
  101. /**
  102. * Infer a combined state shape from a `ReducersMapObject`.
  103. *
  104. * @template M Object map of reducers as provided to `combineReducers(map: M)`.
  105. */
  106. type StateFromReducersMapObject<M> = M[keyof M] extends Reducer<any, any, any> | undefined ? {
  107. [P in keyof M]: M[P] extends Reducer<infer S, any, any> ? S : never;
  108. } : never;
  109. /**
  110. * Infer reducer union type from a `ReducersMapObject`.
  111. *
  112. * @template M Object map of reducers as provided to `combineReducers(map: M)`.
  113. */
  114. type ReducerFromReducersMapObject<M> = M[keyof M] extends Reducer<any, any, any> | undefined ? M[keyof M] : never;
  115. /**
  116. * Infer action type from a reducer function.
  117. *
  118. * @template R Type of reducer.
  119. */
  120. type ActionFromReducer<R> = R extends Reducer<any, infer A, any> ? A : never;
  121. /**
  122. * Infer action union type from a `ReducersMapObject`.
  123. *
  124. * @template M Object map of reducers as provided to `combineReducers(map: M)`.
  125. */
  126. type ActionFromReducersMapObject<M> = ActionFromReducer<ReducerFromReducersMapObject<M>>;
  127. /**
  128. * Infer a combined preloaded state shape from a `ReducersMapObject`.
  129. *
  130. * @template M Object map of reducers as provided to `combineReducers(map: M)`.
  131. */
  132. type PreloadedStateShapeFromReducersMapObject<M> = M[keyof M] extends Reducer<any, any, any> | undefined ? {
  133. [P in keyof M]: M[P] extends (inputState: infer InputState, action: UnknownAction) => any ? InputState : never;
  134. } : never;
  135. /**
  136. * A *dispatching function* (or simply *dispatch function*) is a function that
  137. * accepts an action or an async action; it then may or may not dispatch one
  138. * or more actions to the store.
  139. *
  140. * We must distinguish between dispatching functions in general and the base
  141. * `dispatch` function provided by the store instance without any middleware.
  142. *
  143. * The base dispatch function *always* synchronously sends an action to the
  144. * store's reducer, along with the previous state returned by the store, to
  145. * calculate a new state. It expects actions to be plain objects ready to be
  146. * consumed by the reducer.
  147. *
  148. * Middleware wraps the base dispatch function. It allows the dispatch
  149. * function to handle async actions in addition to actions. Middleware may
  150. * transform, delay, ignore, or otherwise interpret actions or async actions
  151. * before passing them to the next middleware.
  152. *
  153. * @template A The type of things (actions or otherwise) which may be
  154. * dispatched.
  155. */
  156. interface Dispatch<A extends Action = UnknownAction> {
  157. <T extends A>(action: T, ...extraArgs: any[]): T;
  158. }
  159. /**
  160. * Function to remove listener added by `Store.subscribe()`.
  161. */
  162. interface Unsubscribe {
  163. (): void;
  164. }
  165. type ListenerCallback = () => void;
  166. declare global {
  167. interface SymbolConstructor {
  168. readonly observable: symbol;
  169. }
  170. }
  171. /**
  172. * A minimal observable of state changes.
  173. * For more information, see the observable proposal:
  174. * https://github.com/tc39/proposal-observable
  175. */
  176. type Observable<T> = {
  177. /**
  178. * The minimal observable subscription method.
  179. * @param {Object} observer Any object that can be used as an observer.
  180. * The observer object should have a `next` method.
  181. * @returns {subscription} An object with an `unsubscribe` method that can
  182. * be used to unsubscribe the observable from the store, and prevent further
  183. * emission of values from the observable.
  184. */
  185. subscribe: (observer: Observer<T>) => {
  186. unsubscribe: Unsubscribe;
  187. };
  188. [Symbol.observable](): Observable<T>;
  189. };
  190. /**
  191. * An Observer is used to receive data from an Observable, and is supplied as
  192. * an argument to subscribe.
  193. */
  194. type Observer<T> = {
  195. next?(value: T): void;
  196. };
  197. /**
  198. * A store is an object that holds the application's state tree.
  199. * There should only be a single store in a Redux app, as the composition
  200. * happens on the reducer level.
  201. *
  202. * @template S The type of state held by this store.
  203. * @template A the type of actions which may be dispatched by this store.
  204. * @template StateExt any extension to state from store enhancers
  205. */
  206. interface Store<S = any, A extends Action = UnknownAction, StateExt extends {} = {}> {
  207. /**
  208. * Dispatches an action. It is the only way to trigger a state change.
  209. *
  210. * The `reducer` function, used to create the store, will be called with the
  211. * current state tree and the given `action`. Its return value will be
  212. * considered the **next** state of the tree, and the change listeners will
  213. * be notified.
  214. *
  215. * The base implementation only supports plain object actions. If you want
  216. * to dispatch a Promise, an Observable, a thunk, or something else, you
  217. * need to wrap your store creating function into the corresponding
  218. * middleware. For example, see the documentation for the `redux-thunk`
  219. * package. Even the middleware will eventually dispatch plain object
  220. * actions using this method.
  221. *
  222. * @param action A plain object representing “what changed”. It is a good
  223. * idea to keep actions serializable so you can record and replay user
  224. * sessions, or use the time travelling `redux-devtools`. An action must
  225. * have a `type` property which may not be `undefined`. It is a good idea
  226. * to use string constants for action types.
  227. *
  228. * @returns For convenience, the same action object you dispatched.
  229. *
  230. * Note that, if you use a custom middleware, it may wrap `dispatch()` to
  231. * return something else (for example, a Promise you can await).
  232. */
  233. dispatch: Dispatch<A>;
  234. /**
  235. * Reads the state tree managed by the store.
  236. *
  237. * @returns The current state tree of your application.
  238. */
  239. getState(): S & StateExt;
  240. /**
  241. * Adds a change listener. It will be called any time an action is
  242. * dispatched, and some part of the state tree may potentially have changed.
  243. * You may then call `getState()` to read the current state tree inside the
  244. * callback.
  245. *
  246. * You may call `dispatch()` from a change listener, with the following
  247. * caveats:
  248. *
  249. * 1. The subscriptions are snapshotted just before every `dispatch()` call.
  250. * If you subscribe or unsubscribe while the listeners are being invoked,
  251. * this will not have any effect on the `dispatch()` that is currently in
  252. * progress. However, the next `dispatch()` call, whether nested or not,
  253. * will use a more recent snapshot of the subscription list.
  254. *
  255. * 2. The listener should not expect to see all states changes, as the state
  256. * might have been updated multiple times during a nested `dispatch()` before
  257. * the listener is called. It is, however, guaranteed that all subscribers
  258. * registered before the `dispatch()` started will be called with the latest
  259. * state by the time it exits.
  260. *
  261. * @param listener A callback to be invoked on every dispatch.
  262. * @returns A function to remove this change listener.
  263. */
  264. subscribe(listener: ListenerCallback): Unsubscribe;
  265. /**
  266. * Replaces the reducer currently used by the store to calculate the state.
  267. *
  268. * You might need this if your app implements code splitting and you want to
  269. * load some of the reducers dynamically. You might also need this if you
  270. * implement a hot reloading mechanism for Redux.
  271. *
  272. * @param nextReducer The reducer for the store to use instead.
  273. */
  274. replaceReducer(nextReducer: Reducer<S, A>): void;
  275. /**
  276. * Interoperability point for observable/reactive libraries.
  277. * @returns {observable} A minimal observable of state changes.
  278. * For more information, see the observable proposal:
  279. * https://github.com/tc39/proposal-observable
  280. */
  281. [Symbol.observable](): Observable<S & StateExt>;
  282. }
  283. /**
  284. * A store creator is a function that creates a Redux store. Like with
  285. * dispatching function, we must distinguish the base store creator,
  286. * `createStore(reducer, preloadedState)` exported from the Redux package, from
  287. * store creators that are returned from the store enhancers.
  288. *
  289. * @template S The type of state to be held by the store.
  290. * @template A The type of actions which may be dispatched.
  291. * @template PreloadedState The initial state that is passed into the reducer.
  292. * @template Ext Store extension that is mixed in to the Store type.
  293. * @template StateExt State extension that is mixed into the state type.
  294. */
  295. interface StoreCreator {
  296. <S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<Ext, StateExt>): Store<S, A, StateExt> & Ext;
  297. <S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(reducer: Reducer<S, A, PreloadedState>, preloadedState?: PreloadedState | undefined, enhancer?: StoreEnhancer<Ext>): Store<S, A, StateExt> & Ext;
  298. }
  299. /**
  300. * A store enhancer is a higher-order function that composes a store creator
  301. * to return a new, enhanced store creator. This is similar to middleware in
  302. * that it allows you to alter the store interface in a composable way.
  303. *
  304. * Store enhancers are much the same concept as higher-order components in
  305. * React, which are also occasionally called “component enhancers”.
  306. *
  307. * Because a store is not an instance, but rather a plain-object collection of
  308. * functions, copies can be easily created and modified without mutating the
  309. * original store. There is an example in `compose` documentation
  310. * demonstrating that.
  311. *
  312. * Most likely you'll never write a store enhancer, but you may use the one
  313. * provided by the developer tools. It is what makes time travel possible
  314. * without the app being aware it is happening. Amusingly, the Redux
  315. * middleware implementation is itself a store enhancer.
  316. *
  317. * @template Ext Store extension that is mixed into the Store type.
  318. * @template StateExt State extension that is mixed into the state type.
  319. */
  320. type StoreEnhancer<Ext extends {} = {}, StateExt extends {} = {}> = <NextExt extends {}, NextStateExt extends {}>(next: StoreEnhancerStoreCreator<NextExt, NextStateExt>) => StoreEnhancerStoreCreator<NextExt & Ext, NextStateExt & StateExt>;
  321. type StoreEnhancerStoreCreator<Ext extends {} = {}, StateExt extends {} = {}> = <S, A extends Action, PreloadedState>(reducer: Reducer<S, A, PreloadedState>, preloadedState?: PreloadedState | undefined) => Store<S, A, StateExt> & Ext;
  322. /**
  323. * @deprecated
  324. *
  325. * **We recommend using the `configureStore` method
  326. * of the `@reduxjs/toolkit` package**, which replaces `createStore`.
  327. *
  328. * Redux Toolkit is our recommended approach for writing Redux logic today,
  329. * including store setup, reducers, data fetching, and more.
  330. *
  331. * **For more details, please read this Redux docs page:**
  332. * **https://redux.js.org/introduction/why-rtk-is-redux-today**
  333. *
  334. * `configureStore` from Redux Toolkit is an improved version of `createStore` that
  335. * simplifies setup and helps avoid common bugs.
  336. *
  337. * You should not be using the `redux` core package by itself today, except for learning purposes.
  338. * The `createStore` method from the core `redux` package will not be removed, but we encourage
  339. * all users to migrate to using Redux Toolkit for all Redux code.
  340. *
  341. * If you want to use `createStore` without this visual deprecation warning, use
  342. * the `legacy_createStore` import instead:
  343. *
  344. * `import { legacy_createStore as createStore} from 'redux'`
  345. *
  346. */
  347. declare function createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<Ext, StateExt>): Store<S, A, StateExt> & Ext;
  348. /**
  349. * @deprecated
  350. *
  351. * **We recommend using the `configureStore` method
  352. * of the `@reduxjs/toolkit` package**, which replaces `createStore`.
  353. *
  354. * Redux Toolkit is our recommended approach for writing Redux logic today,
  355. * including store setup, reducers, data fetching, and more.
  356. *
  357. * **For more details, please read this Redux docs page:**
  358. * **https://redux.js.org/introduction/why-rtk-is-redux-today**
  359. *
  360. * `configureStore` from Redux Toolkit is an improved version of `createStore` that
  361. * simplifies setup and helps avoid common bugs.
  362. *
  363. * You should not be using the `redux` core package by itself today, except for learning purposes.
  364. * The `createStore` method from the core `redux` package will not be removed, but we encourage
  365. * all users to migrate to using Redux Toolkit for all Redux code.
  366. *
  367. * If you want to use `createStore` without this visual deprecation warning, use
  368. * the `legacy_createStore` import instead:
  369. *
  370. * `import { legacy_createStore as createStore} from 'redux'`
  371. *
  372. */
  373. declare function createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(reducer: Reducer<S, A, PreloadedState>, preloadedState?: PreloadedState | undefined, enhancer?: StoreEnhancer<Ext, StateExt>): Store<S, A, StateExt> & Ext;
  374. /**
  375. * Creates a Redux store that holds the state tree.
  376. *
  377. * **We recommend using `configureStore` from the
  378. * `@reduxjs/toolkit` package**, which replaces `createStore`:
  379. * **https://redux.js.org/introduction/why-rtk-is-redux-today**
  380. *
  381. * The only way to change the data in the store is to call `dispatch()` on it.
  382. *
  383. * There should only be a single store in your app. To specify how different
  384. * parts of the state tree respond to actions, you may combine several reducers
  385. * into a single reducer function by using `combineReducers`.
  386. *
  387. * @param {Function} reducer A function that returns the next state tree, given
  388. * the current state tree and the action to handle.
  389. *
  390. * @param {any} [preloadedState] The initial state. You may optionally specify it
  391. * to hydrate the state from the server in universal apps, or to restore a
  392. * previously serialized user session.
  393. * If you use `combineReducers` to produce the root reducer function, this must be
  394. * an object with the same shape as `combineReducers` keys.
  395. *
  396. * @param {Function} [enhancer] The store enhancer. You may optionally specify it
  397. * to enhance the store with third-party capabilities such as middleware,
  398. * time travel, persistence, etc. The only store enhancer that ships with Redux
  399. * is `applyMiddleware()`.
  400. *
  401. * @returns {Store} A Redux store that lets you read the state, dispatch actions
  402. * and subscribe to changes.
  403. */
  404. declare function legacy_createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<Ext, StateExt>): Store<S, A, StateExt> & Ext;
  405. /**
  406. * Creates a Redux store that holds the state tree.
  407. *
  408. * **We recommend using `configureStore` from the
  409. * `@reduxjs/toolkit` package**, which replaces `createStore`:
  410. * **https://redux.js.org/introduction/why-rtk-is-redux-today**
  411. *
  412. * The only way to change the data in the store is to call `dispatch()` on it.
  413. *
  414. * There should only be a single store in your app. To specify how different
  415. * parts of the state tree respond to actions, you may combine several reducers
  416. * into a single reducer function by using `combineReducers`.
  417. *
  418. * @param {Function} reducer A function that returns the next state tree, given
  419. * the current state tree and the action to handle.
  420. *
  421. * @param {any} [preloadedState] The initial state. You may optionally specify it
  422. * to hydrate the state from the server in universal apps, or to restore a
  423. * previously serialized user session.
  424. * If you use `combineReducers` to produce the root reducer function, this must be
  425. * an object with the same shape as `combineReducers` keys.
  426. *
  427. * @param {Function} [enhancer] The store enhancer. You may optionally specify it
  428. * to enhance the store with third-party capabilities such as middleware,
  429. * time travel, persistence, etc. The only store enhancer that ships with Redux
  430. * is `applyMiddleware()`.
  431. *
  432. * @returns {Store} A Redux store that lets you read the state, dispatch actions
  433. * and subscribe to changes.
  434. */
  435. declare function legacy_createStore<S, A extends Action, Ext extends {} = {}, StateExt extends {} = {}, PreloadedState = S>(reducer: Reducer<S, A, PreloadedState>, preloadedState?: PreloadedState | undefined, enhancer?: StoreEnhancer<Ext, StateExt>): Store<S, A, StateExt> & Ext;
  436. /**
  437. * Turns an object whose values are different reducer functions, into a single
  438. * reducer function. It will call every child reducer, and gather their results
  439. * into a single state object, whose keys correspond to the keys of the passed
  440. * reducer functions.
  441. *
  442. * @template S Combined state object type.
  443. *
  444. * @param reducers An object whose values correspond to different reducer
  445. * functions that need to be combined into one. One handy way to obtain it
  446. * is to use `import * as reducers` syntax. The reducers may never
  447. * return undefined for any action. Instead, they should return their
  448. * initial state if the state passed to them was undefined, and the current
  449. * state for any unrecognized action.
  450. *
  451. * @returns A reducer function that invokes every reducer inside the passed
  452. * object, and builds a state object with the same shape.
  453. */
  454. declare function combineReducers<M>(reducers: M): M[keyof M] extends Reducer<any, any, any> | undefined ? Reducer<StateFromReducersMapObject<M>, ActionFromReducersMapObject<M>, Partial<PreloadedStateShapeFromReducersMapObject<M>>> : never;
  455. /**
  456. * Turns an object whose values are action creators, into an object with the
  457. * same keys, but with every function wrapped into a `dispatch` call so they
  458. * may be invoked directly. This is just a convenience method, as you can call
  459. * `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
  460. *
  461. * For convenience, you can also pass an action creator as the first argument,
  462. * and get a dispatch wrapped function in return.
  463. *
  464. * @param actionCreators An object whose values are action
  465. * creator functions. One handy way to obtain it is to use `import * as`
  466. * syntax. You may also pass a single function.
  467. *
  468. * @param dispatch The `dispatch` function available on your Redux
  469. * store.
  470. *
  471. * @returns The object mimicking the original object, but with
  472. * every action creator wrapped into the `dispatch` call. If you passed a
  473. * function as `actionCreators`, the return value will also be a single
  474. * function.
  475. */
  476. declare function bindActionCreators<A, C extends ActionCreator<A>>(actionCreator: C, dispatch: Dispatch): C;
  477. declare function bindActionCreators<A extends ActionCreator<any>, B extends ActionCreator<any>>(actionCreator: A, dispatch: Dispatch): B;
  478. declare function bindActionCreators<A, M extends ActionCreatorsMapObject<A>>(actionCreators: M, dispatch: Dispatch): M;
  479. declare function bindActionCreators<M extends ActionCreatorsMapObject, N extends ActionCreatorsMapObject>(actionCreators: M, dispatch: Dispatch): N;
  480. interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
  481. dispatch: D;
  482. getState(): S;
  483. }
  484. /**
  485. * A middleware is a higher-order function that composes a dispatch function
  486. * to return a new dispatch function. It often turns async actions into
  487. * actions.
  488. *
  489. * Middleware is composable using function composition. It is useful for
  490. * logging actions, performing side effects like routing, or turning an
  491. * asynchronous API call into a series of synchronous actions.
  492. *
  493. * @template DispatchExt Extra Dispatch signature added by this middleware.
  494. * @template S The type of the state supported by this middleware.
  495. * @template D The type of Dispatch of the store where this middleware is
  496. * installed.
  497. */
  498. interface Middleware<_DispatchExt = {}, // TODO: see if this can be used in type definition somehow (can't be removed, as is used to get final dispatch type)
  499. S = any, D extends Dispatch = Dispatch> {
  500. (api: MiddlewareAPI<D, S>): (next: (action: unknown) => unknown) => (action: unknown) => unknown;
  501. }
  502. /**
  503. * Creates a store enhancer that applies middleware to the dispatch method
  504. * of the Redux store. This is handy for a variety of tasks, such as expressing
  505. * asynchronous actions in a concise manner, or logging every action payload.
  506. *
  507. * See `redux-thunk` package as an example of the Redux middleware.
  508. *
  509. * Because middleware is potentially asynchronous, this should be the first
  510. * store enhancer in the composition chain.
  511. *
  512. * Note that each middleware will be given the `dispatch` and `getState` functions
  513. * as named arguments.
  514. *
  515. * @param middlewares The middleware chain to be applied.
  516. * @returns A store enhancer applying the middleware.
  517. *
  518. * @template Ext Dispatch signature added by a middleware.
  519. * @template S The type of the state supported by a middleware.
  520. */
  521. declare function applyMiddleware(): StoreEnhancer;
  522. declare function applyMiddleware<Ext1, S>(middleware1: Middleware<Ext1, S, any>): StoreEnhancer<{
  523. dispatch: Ext1;
  524. }>;
  525. declare function applyMiddleware<Ext1, Ext2, S>(middleware1: Middleware<Ext1, S, any>, middleware2: Middleware<Ext2, S, any>): StoreEnhancer<{
  526. dispatch: Ext1 & Ext2;
  527. }>;
  528. declare function applyMiddleware<Ext1, Ext2, Ext3, S>(middleware1: Middleware<Ext1, S, any>, middleware2: Middleware<Ext2, S, any>, middleware3: Middleware<Ext3, S, any>): StoreEnhancer<{
  529. dispatch: Ext1 & Ext2 & Ext3;
  530. }>;
  531. declare function applyMiddleware<Ext1, Ext2, Ext3, Ext4, S>(middleware1: Middleware<Ext1, S, any>, middleware2: Middleware<Ext2, S, any>, middleware3: Middleware<Ext3, S, any>, middleware4: Middleware<Ext4, S, any>): StoreEnhancer<{
  532. dispatch: Ext1 & Ext2 & Ext3 & Ext4;
  533. }>;
  534. declare function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>(middleware1: Middleware<Ext1, S, any>, middleware2: Middleware<Ext2, S, any>, middleware3: Middleware<Ext3, S, any>, middleware4: Middleware<Ext4, S, any>, middleware5: Middleware<Ext5, S, any>): StoreEnhancer<{
  535. dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5;
  536. }>;
  537. declare function applyMiddleware<Ext, S = any>(...middlewares: Middleware<any, S, any>[]): StoreEnhancer<{
  538. dispatch: Ext;
  539. }>;
  540. type Func<T extends any[], R> = (...a: T) => R;
  541. /**
  542. * Composes single-argument functions from right to left. The rightmost
  543. * function can take multiple arguments as it provides the signature for the
  544. * resulting composite function.
  545. *
  546. * @param funcs The functions to compose.
  547. * @returns A function obtained by composing the argument functions from right
  548. * to left. For example, `compose(f, g, h)` is identical to doing
  549. * `(...args) => f(g(h(...args)))`.
  550. */
  551. declare function compose(): <R>(a: R) => R;
  552. declare function compose<F extends Function>(f: F): F;
  553. declare function compose<A, T extends any[], R>(f1: (a: A) => R, f2: Func<T, A>): Func<T, R>;
  554. declare function compose<A, B, T extends any[], R>(f1: (b: B) => R, f2: (a: A) => B, f3: Func<T, A>): Func<T, R>;
  555. declare function compose<A, B, C, T extends any[], R>(f1: (c: C) => R, f2: (b: B) => C, f3: (a: A) => B, f4: Func<T, A>): Func<T, R>;
  556. declare function compose<R>(f1: (a: any) => R, ...funcs: Function[]): (...args: any[]) => R;
  557. declare function compose<R>(...funcs: Function[]): (...args: any[]) => R;
  558. declare function isAction(action: unknown): action is Action<string>;
  559. /**
  560. * @param obj The object to inspect.
  561. * @returns True if the argument appears to be a plain object.
  562. */
  563. declare function isPlainObject(obj: any): obj is object;
  564. /**
  565. * These are private action types reserved by Redux.
  566. * For any unknown actions, you must return the current state.
  567. * If the current state is undefined, you must return the initial state.
  568. * Do not reference these action types directly in your code.
  569. */
  570. declare const ActionTypes: {
  571. INIT: string;
  572. REPLACE: string;
  573. PROBE_UNKNOWN_ACTION: () => string;
  574. };
  575. export { Action, ActionCreator, ActionCreatorsMapObject, ActionFromReducer, ActionFromReducersMapObject, AnyAction, Dispatch, Middleware, MiddlewareAPI, Observable, Observer, PreloadedStateShapeFromReducersMapObject, Reducer, ReducerFromReducersMapObject, ReducersMapObject, StateFromReducersMapObject, Store, StoreCreator, StoreEnhancer, StoreEnhancerStoreCreator, UnknownAction, Unsubscribe, ActionTypes as __DO_NOT_USE__ActionTypes, applyMiddleware, bindActionCreators, combineReducers, compose, createStore, isAction, isPlainObject, legacy_createStore };