【问题标题】:Flow: optional prop that uses generic typesFlow:使用泛型类型的可选道具
【发布时间】:2019-06-04 09:27:28
【问题描述】:

我有一个 react 组件,它有一个名为 showModal 的函数 prop。我已经在其他地方使用泛型类型注释了这个函数,但在这里,我希望它是一个可选的道具。通常我用func?: () => void 来做这件事,但是这种语法在这种情况下不起作用(无论我把? 放在哪里,我都会得到Parsing error):

type props = {
  showModal<T>(T => React$Node, T): void,
}

如何指定showModal 是可选的?我查看了 Flow 的文档,但找不到与此问题相关的任何内容。

【问题讨论】:

    标签: reactjs flowtype


    【解决方案1】:

    您需要调整函数表达式。 Flow Try link

    type Props = {
      showModal?: <T>(T => React$Node, T) => void,
    }
    
    const foo: Props = {
      showModal: (a, b) => undefined
    }
    
    const bar: Props = {
      showModal: undefined
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 2022-01-09
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多