Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. # Common Flow Issues
  2.  
  3. Below are a list of common flow issues with a brief explanations of the problem and possible solutions. The dynamic parts of the error message have all been replaced with a wildcard character `*` in the error message to aid with finding it on this page
  4.  
  5. ## Cannot resolve module *
  6.  
  7. ```js
  8. // $FlowError: Cannot resolve module *
  9. import example from "example";
  10. ```
  11.  
  12. ### Problem
  13.  
  14. Flow is not aware of the module "example"
  15.  
  16. ### Solutions
  17.  
  18. - Install flow-typed module `yarn flow-typed install example`
  19. - Or create a stub module with `yarn flow-typed create-stub example`
  20. - Or blacklist module in your projects `.flowconfig`. Not ideal but sometimes required when vendors ship their own invalid types
  21.  
  22. ## Missing type annotation for *. * is a type parameter declared in array type [1] and was implicitly instantiated at call of method * [2]
  23.  
  24. ```js
  25. export function test() {
  26. // $FlowError: Missing type annotation for *. * is a type parameter declared in array type [1] and was implicitly instantiated at call of method * [2]
  27. return [].map(a => a);
  28. }
  29. ```
  30.  
  31. ### Problem
  32.  
  33. Flow does not know your `Array.map` or `filter` call won't change the type of the array
  34.  
  35. ### Solutions
  36.  
  37. - Provide return type annotation for the result of your map or filter call
  38.  
  39. ```js
  40. export function test(): Array<{}> {
  41. return [].map(a => a);
  42. }
  43. ```
  44.  
  45. ## Missing type annotation for *
  46.  
  47. ```js
  48. // $FlowError Missing type annotation for *. * is a type parameter declared in function type [1] and was implicitly instantiated at call of * [2].
  49. export default connect(mapStateToProps, mapDispatchToProps);
  50. ```
  51.  
  52. ### Problem
  53.  
  54. Flow 0.85 introduced stricter requirements for annotating exported functions. Functions with generic parameters like `connect`, `createSelector` and `defineRequest` require annotation if the return value is exported. Not all parameters are require annotation. Optional paramters can be omitted with `_`, but providing all paramerters is recommended because it usually leads to better error messages
  55.  
  56. ### Solution
  57.  
  58. - Provide required annotations
  59.  
  60. #### `connect`
  61.  
  62. ```js
  63. export default connect<Props, OwnProps, StateProps, DispatchProps, State, Dispatch>(mapStateToProps, mapDispatchToProps);
  64. ```
  65.  
  66. #### `connect` with no dispatch
  67.  
  68. ```js
  69. export default connect<Props, OwnProps, StateProps, _, State, Dispatch>(mapStateToProps);
  70. ```
  71.  
  72. #### `connect` with no state
  73.  
  74. ```js
  75. export default connect<Props, OwnProps, _, DispatchProps, State, Dispatch>(undefined, mapDispatchToProps);
  76. ```
  77.  
  78. #### `createSelector` / `createCachedSelector`
  79.  
  80. ```js
  81. // TBA
  82. ```
  83.  
  84. #### `defineRequest`
  85.  
  86. ```js
  87. export default defineRequest<PerformParams, IdParams>({/* ... */});
  88. ```
  89.  
  90. ## Missing type annotation for call of compose
  91.  
  92. ```js
  93. // $FlowError: Missing type annotation for call of compose.
  94. export default compose(
  95. connect<Props, OwnProps, StateProps, DispatchProps, State, Dispatch>(
  96. mapStateToProps,
  97. mapDispatchToProps
  98. ),
  99. withData(makeInput)
  100. );
  101. ```
  102.  
  103. ### Problem
  104.  
  105. Same problem as above. See: [Missing type annotation for *](#missing-type-annotation-for-)
  106.  
  107. ### Solution
  108.  
  109. - Export annotated compose using a variable or helper liker `createConnector`
  110.  
  111. ```js
  112. const connector: (React.AbstractComponent<Props>) => React.AbstractComponent<OwnProps> = compose(
  113. connect<Props, OwnProps, StateProps, DispatchProps, State, Dispatch>(
  114. mapStateToProps,
  115. mapDispatchToProps
  116. ),
  117. withData(makeInput)
  118. );
  119. ```
  120.  
  121. Or more succinctly with a helper around `compose` called `createConnector` for convention:
  122.  
  123. ```js
  124. export default createConnector<Props, OwnProps>(
  125. connect<Props, OwnProps, StateProps, DispatchProps, State, Dispatch>(
  126. mapStateToProps,
  127. mapDispatchToProps
  128. ),
  129. withData(makeInput)
  130. );
  131. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement