index.d.ts 24 KB

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