如何满足Flow中的“不精确函数”错误?

huangapple go评论103阅读模式
英文:

How do I satisfy an "inexact function" error in Flow?

问题

以下是翻译好的部分:

我想在我的React组件中显示一个StatusImage,如果组件的用户通过statusImage属性提供了图像。否则,我只想保留图标所在位置的空白处。

它是有效的,但我得到了以下Flow错误,我无法解决。我正在使用flow-bin版本0.113.0

  1. 无法将props.statusImage ? props.statusImage : (...) => <div />分配给StatusImage,因为不精确的函数[1]与精确的React.Element[2]不兼容。
  2. [2] 121| const StatusImage: React$Element<*> =
  3. [1] 122| props.statusImage ? (props.statusImage) : ((): React$Element<*> => <div/>);

我已经在Google上搜索了不精确的函数Flow错误,但无法找到任何解决我的问题的信息。这似乎是一个相对较新的错误,最近才开始在Flow中检查。

英文:

Ok so I've got a pretty straightforward scenario which works but my issue is keeping flow happy.

  1. const StatusImage: React$Element&lt;*&gt; =
  2. props.statusImage ? (props.statusImage) : ((): React$Element&lt;*&gt; =&gt; &lt;div/&gt;);

I want to display a StatusImage within my React component if the user of the component supplies one through the statusImage prop. Otherwise I just want to leave the space where the icon would be blank.

It works but I get the following flow error which I can't solve. I'm using flow-bin version 0.113.0

  1. Cannot assign props.statusImage ? props.statusImage : (...) =&gt; &lt;div /&gt; to StatusImage because inexact function [1] is
  2. incompatible with exact React.Element [2].
  3. [2] 121| const StatusImage: React$Element&lt;*&gt; =
  4. [1] 122| props.statusImage ? (props.statusImage) : ((): React$Element&lt;*&gt; =&gt; &lt;div/&gt;);

I've google the inexact function flow error but can't find anything anywhere that solves my issue. This appears to be quite a new error which has only started to be checked for recently in flow.

答案1

得分: 1

I would recommend using the React.ComponentType&lt;Props&gt; type if props.statusImage can contain a stateless functional component or a class component. If props.statusImage can only be a stateless functional component, then you could use React.StatelessFunctionalComponent&lt;Props&gt;.

  1. import * as React from "react";
  2. type StatusImageComponent = React.ComponentType<{}>;
  3. type Props = { statusImage?: StatusImageComponent };
  4. function Foo(props: Props) {
  5. const StatusImage =
  6. props.statusImage === undefined ? () => <div /> : props.statusImage;
  7. return (
  8. <div>
  9. <StatusImage />
  10. </div>
  11. );
  12. }

Try Flow

英文:

I would recommend using the React.ComponentType&lt;Props&gt; type if props.statusImage can contain a stateless functional component or a class component. If props.statusImage can only be a stateless functional component then you could use React.StatelessFunctionalComponent&lt;Props&gt;.

  1. import * as React from &quot;react&quot;;
  2. type StatusImageComponent = React.ComponentType&lt;{}&gt;;
  3. type Props = { statusImage?: StatusImageComponent };
  4. function Foo(props: Props) {
  5. const StatusImage =
  6. props.statusImage === undefined ? () =&gt; &lt;div /&gt; : props.statusImage;
  7. return (
  8. &lt;div&gt;
  9. &lt;StatusImage /&gt;
  10. &lt;/div&gt;
  11. );
  12. }

Try Flow

答案2

得分: 0

这似乎通过一些进一步的试验和错误来工作...

英文:

With some further trial and error this seems to work....

  1. const StatusImage: React$Element&lt;*&gt; | () =&gt; React$Element&lt;*&gt; =
  2. props.statusImage ? (props.statusImage) : ((): React$Element&lt;*&gt; =&gt; &lt;div/&gt;);

huangapple
  • 本文由 发表于 2020年1月3日 17:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定