道具的一些不相容的实例

时间:2017-09-14 02:02:58

标签: reactjs flowtype

为什么这不起作用:

Try Flow!

function mergeTheme<Props: {}, T: {}>(
  Component: React.ComponentType<{ theme: T } & Props>,
  injectedTheme: T
): React.ComponentType<{ theme: T } & Props> {
  const Themed = (props: { theme: T } & Props) => {
      let theme: T = injectedTheme;
      return <Component {...props} theme={theme} />;
  };
  return Themed;
}

错误:

11:         return <Component {...props} theme={theme} />;
                   ^ props of React element `Component`. This type is incompatible with
5: function mergeTheme<Props: {}, T: {}>(
   ^ some incompatible instantiation of `Props`
11:         return <Component {...props} theme={theme} />;
                   ^ props of React element `Component`. This type is incompatible with the expected param type of
5: function mergeTheme<Props: {}, T: {}>(
   ^ some incompatible instantiation of `Props`

但这有效:

Try Flow!

function mergeTheme<Props: {}, T: {}>(
  Component: React.ComponentType<{ theme: T } & Props>,
  injectedTheme: T
): React.ComponentType<{ theme: T } & Props> {
  const Themed = (props: { theme: T } & Props) => {
      const baseProps: Props = props;
      let theme: T = injectedTheme;
      return <Component {...props} theme={theme} />;
  };
  return Themed;
}

两者之间的区别是什么?我如何让第一个工作?

0 个答案:

没有答案