index.d.ts 24 KB

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