Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Recompose.cjs.js.flow 9.0 KiB

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* eslint-disable */
  2. /* @flow */
  3. /**
  4. * 1) Types give additional constraint on a language, recompose was written on the untyped language
  5. * as a consequence of this fact
  6. * for some recompose HOCs is near impossible to add correct typings.
  7. * 2) flow sometimes does not work as expected.
  8. *
  9. * So any help and suggestions will be very appreciated.
  10. *
  11. * -----------------------------------------------------------------------------------
  12. * Type definition of recompose HOCs are splitted into 2 parts,
  13. * "HOCs with good flow support" - in most cases you can use them without big issues,
  14. * see `test_${hocName}.js` for the idea.
  15. * Some known issues:
  16. * see test_mapProps.js - inference work but type errors are not detected in hocs
  17. *
  18. * SUPPORTED HOCs:
  19. * defaultProps, mapProps, withProps, withStateHandlers, withHandlers, pure,
  20. * onlyUpdateForKeys, shouldUpdate, renderNothing, renderComponent, branch, withPropsOnChange,
  21. * onlyUpdateForPropTypes, toClass, withContext, getContext,
  22. * setStatic, setPropTypes, setDisplayName,
  23. * -----------------------------------------------------------------------------------
  24. * "TODO (UNSUPPORTED) HOCs" - you need to provide type information
  25. * (no automatic type inference), voodoo dancing etc
  26. * see `test_voodoo.js` for the idea
  27. *
  28. * remember that:
  29. * flattenProp,renameProp, renameProps can easily be replaced with withProps
  30. * withReducer, withState -> use withStateHandlers instead
  31. * lifecycle -> you don't need recompose if you need a lifecycle, just use React class instead
  32. * mapPropsStream -> see test_mapPropsStream.js
  33. * -----------------------------------------------------------------------------------
  34. *
  35. * utils:
  36. * getDisplayName, wrapDisplayName, shallowEqual,
  37. * isClassComponent, createSink, componentFromProp,
  38. * nest, hoistStatics,
  39. */
  40. //-------------------
  41. // -----------------------------------------------------------------
  42. // Private declarations
  43. // -----------------------------------------------------------------
  44. declare type ExtractToVoid = <A, B, C, R>(
  45. v: (a: A, b: B, c: C) => R
  46. ) => (A, B, C) => void
  47. declare type ExtractStateHandlersCodomain = <State, Enhanced, V>(
  48. v: (state: State, props: Enhanced) => V
  49. ) => V
  50. declare type ExtractHandlersCodomain = <Enhanced, V>(
  51. v: (props: Enhanced) => V
  52. ) => V
  53. declare type UnaryFn<A, R> = (a: A) => R
  54. // -----------------------------------------------------------------
  55. // Public declarations
  56. // -----------------------------------------------------------------
  57. export type Component<A> = React$ComponentType<A>
  58. export type HOC<Base, Enhanced> = UnaryFn<Component<Base>, Component<Enhanced>>
  59. declare export var compose: $Compose
  60. // ---------------------------------------------------------------------------
  61. // ----------------===<<<HOCs with good flow support>>>===--------------------
  62. // ---------------------------------------------------------------------------
  63. declare export function defaultProps<Default, Enhanced>(
  64. defProps: Default
  65. ): HOC<{ ...$Exact<Enhanced>, ...Default }, Enhanced>
  66. declare export function mapProps<Base, Enhanced>(
  67. propsMapper: (ownerProps: Enhanced) => Base
  68. ): HOC<Base, Enhanced>
  69. declare export function withProps<BaseAdd, Enhanced>(
  70. propsMapper: ((ownerProps: Enhanced) => BaseAdd) | BaseAdd
  71. ): HOC<{ ...$Exact<Enhanced>, ...BaseAdd }, Enhanced>
  72. declare export function withStateHandlers<
  73. State,
  74. Enhanced,
  75. StateHandlers: {
  76. [key: string]: (
  77. state: State,
  78. props: Enhanced
  79. ) => (...payload: any[]) => $Shape<State>,
  80. }
  81. >(
  82. initialState: ((props: Enhanced) => State) | State,
  83. stateUpdaters: StateHandlers
  84. ): HOC<
  85. {
  86. ...$Exact<Enhanced>,
  87. ...$Exact<State>,
  88. ...$ObjMap<
  89. $ObjMap<StateHandlers, ExtractStateHandlersCodomain>,
  90. ExtractToVoid
  91. >,
  92. },
  93. Enhanced
  94. >
  95. declare export function withHandlers<
  96. Enhanced,
  97. Handlers:
  98. | ((
  99. props: Enhanced
  100. ) => {
  101. [key: string]: (props: Enhanced) => Function,
  102. })
  103. | {
  104. [key: string]: (props: Enhanced) => Function,
  105. }
  106. >(
  107. handlers: ((props: Enhanced) => Handlers) | Handlers
  108. ): HOC<
  109. {
  110. ...$Exact<Enhanced>,
  111. ...$ObjMap<Handlers, ExtractHandlersCodomain>,
  112. },
  113. Enhanced
  114. >
  115. declare export function pure<A>(a: Component<A>): Component<A>
  116. declare export function onlyUpdateForPropTypes<A>(a: Component<A>): Component<A>
  117. declare export function onlyUpdateForKeys<A>(Array<$Keys<A>>): HOC<A, A>
  118. declare export function shouldUpdate<A>(
  119. (props: A, nextProps: A) => boolean
  120. ): HOC<A, A>
  121. declare export function toClass<A>(a: Component<A>): Component<A>
  122. declare export function withContext<A, ContextPropTypes, ContextObj>(
  123. childContextTypes: ContextPropTypes,
  124. getChildContext: (props: A) => ContextObj
  125. ): HOC<A, A>
  126. declare export function getContext<CtxTypes, Enhanced>(
  127. contextTypes: CtxTypes
  128. ): HOC<{ ...$Exact<Enhanced>, ...CtxTypes }, Enhanced>
  129. /**
  130. * It's wrong declaration but having that renderNothing and renderComponent are somehow useless
  131. * outside branch enhancer, we just give it an id type
  132. * so common way of using branch like
  133. * `branch(testFn, renderNothing | renderComponent(Comp))` will work as expected.
  134. * Tests are placed at test_branch.
  135. */
  136. declare export function renderNothing<A>(C: Component<A>): Component<A>
  137. declare export function renderComponent<A>(a: Component<A>): HOC<A, A>
  138. /**
  139. * We make an assumtion that left and right have the same type if exists
  140. */
  141. declare export function branch<Base, Enhanced>(
  142. testFn: (props: Enhanced) => boolean,
  143. // not a HOC because of inference problems, this works but HOC<Base, Enhanced> is not
  144. left: (Component<Base>) => Component<Enhanced>,
  145. // I never use right part and it can be a problem with inference as should be same type as left
  146. right?: (Component<Base>) => Component<Enhanced>
  147. ): HOC<Base, Enhanced>
  148. // test_statics
  149. declare export function setStatic<A>(key: string, value: any): HOC<A, A>
  150. declare export function setPropTypes<A>(propTypes: Object): HOC<A, A>
  151. declare export function setDisplayName<A>(displayName: string): HOC<A, A>
  152. declare export function withPropsOnChange<BaseAdd, Enhanced>(
  153. shouldMapOrKeys:
  154. | ((props: Enhanced, nextProps: Enhanced) => boolean)
  155. | Array<$Keys<Enhanced>>,
  156. propsMapper: (ownerProps: Enhanced) => BaseAdd
  157. ): HOC<{ ...$Exact<Enhanced>, ...BaseAdd }, Enhanced>
  158. // ---------------------------------------------------------------------------
  159. // ----------------===<<<TODO (UNSUPPORTED) HOCs>>>===------------------------
  160. // ---------------------------------------------------------------------------
  161. // use withProps instead
  162. declare export function flattenProp<Base, Enhanced>(
  163. propName: $Keys<Enhanced>
  164. ): HOC<Base, Enhanced>
  165. // use withProps instead
  166. declare export function renameProp<Base, Enhanced>(
  167. oldName: $Keys<Enhanced>,
  168. newName: $Keys<Base>
  169. ): HOC<Base, Enhanced>
  170. // use withProps instead
  171. declare export function renameProps<Base, Enhanced>(nameMap: {
  172. [key: $Keys<Enhanced>]: $Keys<Base>,
  173. }): HOC<Base, Enhanced>
  174. // use withStateHandlers instead
  175. declare export function withState<Base, Enhanced, T>(
  176. stateName: string,
  177. stateUpdaterName: string,
  178. initialState: T | ((props: Enhanced) => T)
  179. ): HOC<Base, Enhanced>
  180. // use withStateHandlers instead
  181. declare export function withReducer<A, B, Action, State>(
  182. stateName: string,
  183. dispatchName: string,
  184. reducer: (state: State, action: Action) => State,
  185. initialState: State
  186. ): HOC<A, B>
  187. // lifecycle use React instead
  188. declare export function lifecycle<A, B>(spec: Object): HOC<A, B>
  189. // Help needed, as explicitly providing the type
  190. // errors not detected, see TODO at test_mapPropsStream.js
  191. declare export function mapPropsStream<Base, Enhanced>(
  192. (props$: any) => any
  193. ): HOC<Base, Enhanced>
  194. // ---------------------------------------------------------------------------
  195. // -----------------------------===<<<Utils>>>===-----------------------------
  196. // ---------------------------------------------------------------------------
  197. declare export function getDisplayName<A>(C: Component<A>): string
  198. declare export function wrapDisplayName<A>(
  199. C: Component<A>,
  200. wrapperName: string
  201. ): string
  202. declare export function shallowEqual(objA: mixed, objB: mixed): boolean
  203. declare export function isClassComponent(value: any): boolean
  204. declare export function createSink<A>(
  205. callback: (props: A) => void
  206. ): Component<A>
  207. declare export function componentFromProp<A>(propName: string): Component<A>
  208. declare export function nest<A>(
  209. ...Components: Array<Component<any> | string>
  210. ): Component<A>
  211. declare export function hoistStatics<A, B, H: HOC<A, B>>(hoc: H): H
  212. declare export function componentFromStream<T>(
  213. (props$: any) => any
  214. ): T => React$Element<any>
  215. declare export function createEventHandler(): {
  216. stream: any,
  217. handler: Function,
  218. }
  219. declare export function toRenderProps<B, E>(
  220. hoc: HOC<B, E>
  221. ): React$ComponentType<{|
  222. ...$Exact<E>,
  223. children: (b: B) => React$Node,
  224. |}>
  225. declare export function fromRenderProps<B, E, RenderPropName, Props>(
  226. RenderPropsComponent: Component<{
  227. [RenderPropName]: (...props: Props) => React$Node,
  228. }>,
  229. propsMapper: ((...props: Props) => B),
  230. renderPropName?: RenderPropName
  231. ): HOC<{ ...$Exact<E>, ...B }, E>