【发布时间】: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的,这是一个不同的函数。withNavigationFocusdoes have a type declaration 不过。 -
@RoryO'Kane 很好的观察,这里只是复制和浪费。我会更新问题,谢谢!
标签: javascript react-native react-navigation flowtype