实例化组件而不传递任何prop时,出现流错误“ props与空不兼容”

Fra*_*nto 5 reactjs flowtype react-native react-redux

我有一个包装在react-redux“连接”中的组件。该组件的所有道具均由mapStateToProps和MapDispatchToProps提供,因此没有“ ownProps”传递给该组件。

但是,我收到以下流错误:

Cannot create Credits element because props [1] is incompatible with empty [2].

     src/components/ChildrenView/index.js
     323?         />
     324?
     325?         {/* Credits */}
 [1] 326?         <Credits />
     327?
     328?         {/* fullscreen pictures rhat open with onClick on tge image */}
     329?         <PhotoViewer />

     flow-typed/npm/react-redux_v5.x.x.js
 [2] 110?     CP: $Diff<ElementConfig<Com>, RSP>,
Run Code Online (Sandbox Code Playgroud)

1中的道具不是已经空了吗?

我正在使用流类型,您可以在错误中指出。

这是类定义和connect调用:

type Props = {|
  ...mapStateToPropsType,
  pricingModal: typeof pricingModal,
  offlineModal: typeof offlineModal,
|}


class Credits extends React.Component<Props> { ... }


type mapStateToPropsType = {|
  balance: number,
  isVisiblePricing: boolean,
  isConnected: boolean,
  isVisibleOffline: boolean,
|}

const mapStateToProps = ({ parents, pricing, appState }: TP.State): mapStateToPropsType => ({
  balance: parents.balance || 0,
  isVisiblePricing: pricing.modalPricing,
  isConnected: appState.isConnected,
  isVisibleOffline: appState.modalOffline,
})

export default connect(mapStateToProps, { pricingModal, offlineModal })(Credits)
Run Code Online (Sandbox Code Playgroud)

如何消除此错误(不使用$ FlowFixMe:/)?

编辑1

如果我对react-redux_v5.xxjs的第110行和第114行的“连接”类型定义进行以下更改,则类型检查将按预期工作。

之前:

  declare export function connect<
    Com: ComponentType<*>,
    S: Object,
    SP: Object,
    RSP: Object,
    MDP: Object,
    CP: $Diff<ElementConfig<Com>, RSP>,
  >(
    mapStateToProps: MapStateToProps<S, SP, RSP>,
    mapDispatchToProps: MDP,
  ): (component: Com) => ComponentType<$Diff<CP, MDP> & SP>
Run Code Online (Sandbox Code Playgroud)

后:

  declare export function connect<
    Com: ComponentType<*>,
    S: Object,
    SP: Object,
    RSP: Object,
    MDP: Object,
    CP: $Diff<$Diff<ElementConfig<Com>, RSP>, MDP>, /*  <-- here */
  >(
    mapStateToProps: MapStateToProps<S, SP, RSP>,
    mapDispatchToProps: MDP,
  ): (component: Com) => ComponentType<CP & SP> /*  <-- here */
Run Code Online (Sandbox Code Playgroud)

Jam*_*aus 1

您使用的是最新版本的 Flow 和 Typedef 吗?当我在Flow.org/try上的组合示例中使用最新版本的 Flow 和最新的 typedef 时,我没有看到任何错误。如果这没有帮助,请尝试在 Flow.org/try(首选)或 Github 上创建您的问题的工作示例。