使用React.Node时流类型错误

ala*_*udi 1 reactjs flowtype

我已经编写了一个我喜欢的实用程序函数,但是由于某种原因,我无法实现它的流类型。下面的代码产生错误。

// @flow
import React from 'react';
import type { Node } from 'react';

export const partializeComponent = (partialProps: any) =>
  (Component: Node) =>
    (props: any): Node => (
      <Component
        {...partialProps}
        {...props}
      />
    );
Run Code Online (Sandbox Code Playgroud)

由于错误非常冗长,我改用了屏幕截图在此处输入图片说明

Jon*_*les 6

您的问题是,将其Node应用于自变量组件时使用的类型不正确。该类型Node表示一个JSX元素,例如...。它是React组件render方法的正确返回类型。

实际上,您应该使用该类型ComponentType并传递一个Prop类型来反映组件的实现。

我已经更新了您的示例,并为您提供了一些空白。

// @flow
import React from 'react';
import type { ComponentType, Node } from 'react';

type PartialProps = {
  prop1: string,
  prop2: number,
};

type Props = PartialProps & {
  otherProps: string,
};

export const partializeComponent = (partialProps: PartialProps) =>
  (Component: ComponentType<Props>) =>
    (props: Props): Node => (
      <Component
        {...partialProps}
        {...props}
      />
    );
Run Code Online (Sandbox Code Playgroud)

请注意,这是未经测试的,它来自内存。