【问题标题】:how to use Flow with external Higher Order Component Wrapper如何将 Flow 与外部高阶组件包装器一起使用
【发布时间】:2020-04-10 20:08:12
【问题描述】:

Flow documentation 仅展示了如何声明自定义高阶组件以使用自定义类及其道具。就我而言,我有一个自定义类,例如:

type Props = {
  navigation: Object,
  isFocused: boolean
}

type State = {
  config: AppConfig,
  pack: Package,
  init: boolean,
}

class MainAppScreen extends React.Component<Props, State> {
...
}
export default withNavigationFocus(MainAppScreen);

并希望用来自“react-navigation”的外部 HOC 包装它;

我应该怎么做(除了//$FlowFixMe)来摆脱这个消息:

错误:(111, 16) 无法为此模块构建类型化接口。您应该使用类型注释此模块的导出。无法确定此调用表达式的类型。请提供注释,例如,通过在此表达式周围添加类型转换。 (signature-verification-failure)

谢谢。

更新:
正如@user11307804 指出的正确方向,我也一直在尝试将流类型用于外部库。但是,似乎仍然无法为(HOC)函数导入类型,例如:

  declare export function withNavigationFocus<Props: {...}, ComponentType: React$ComponentType<Props>>(
    Component: ComponentType
  ): React$ComponentType<$Diff<React$ElementConfig<ComponentType>, {| isFocused: ?boolean |}>>;

当我尝试像这样导入它时:(following this example)

import type {withNavigationFocus} from 'react-navigation';

我得到错误:

Error:(22, 14) Cannot import the value `withNavigationFocus` as a type. `import type` only works on type exports like type aliases, interfaces, and classes. If you intended to import the type of a value use `import typeof` instead.

如果我尝试使用 typeof,我会得到:

import typeof {withNavigationFocus} from 'react-navigation';

我得到错误:

Error:(22, 16) Cannot declare  `withNavigationFocus` [1] because the name is already bound.
Error:(112, 16) Cannot call `withNavigationFocus` because  Named import from module `react-navigation` [1] is not a function.
Error:(112, 16) Cannot build a typed interface for this module. You should annotate the exports of this module with types. Cannot determine the type of this call expression. Please provide an annotation, e.g., by adding a type cast around this expression. (`signature-verification-failure`)

谢谢。

【问题讨论】:

  • withNavigationFocus的类型是什么?
  • 我不知道,API 参考不是很具体:reactnavigation.org/docs/en/with-navigation-focus.html
  • “当我尝试导入它时”和“如果我尝试使用 typeof”的代码示例是相同的;他们都有typeof。这只是复制粘贴错误,还是您忘记尝试import { withNavigationFocus } from 'react-navigation';
  • 另请注意,您正在尝试导入withNavigationFocus,但您之前引用的declare 语句是针对withNavigation 的,这是一个不同的函数。 withNavigationFocus does have a type declaration 不过。
  • @RoryO'Kane 很好的观察,这里只是复制和浪费。我会更新问题,谢谢!

标签: javascript react-native react-navigation flowtype


【解决方案1】:

Flow 抱怨 withNavigationFocused 没有类型。幸运的是,flow-typed 项目有react-navigation types。 (对于react-navigationflow 的不同版本,还有其他定义文件;我链接的是react-navigation^4.0.0flow^0.114.0。)您可以在Library Definitions documentation 之后的项目中包含库定义(本质上,只需将文件保存在&lt;PROJECT_ROOT&gt;/flow-typed 目录中)。

【讨论】:

  • 谢谢,这正是缺少的链接。
  • 实际上它看起来是正确的解决方案,但它似乎不适用于 HOC 功能,我更新了问题,也许我使用错了?
  • 不确定放置此更新的最佳位置在哪里...无论如何,您不需要导入类型withNavigationFocus。 Flow 将自动知道查看declare type withNavigationFocus,因为declare module 'react-navigation' 行对应于导入语句import { withNavigationFocus } from 'react-navigation'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 2020-02-01
  • 1970-01-01
相关资源
最近更新 更多