javascript - flowtype 绑定(bind)导致错误 `Convariant property incompatible with contravariant use in assignment of property`

标签 javascript reactjs react-native flowtype

这个表达式对于 javascript/react 来说非常简单,将函数绑定(bind)到 this 范围。

this.toggle = this.toggle.bind(this);

但是当引入 flowtype 时,会导致错误:

enter image description here

我该怎么做才能通过流量测试?

toggle 可以是任何函数,甚至可以是空函数。

toggle() {
    /// do nothing
}

最佳答案

你必须在你的类中将你的toggle声明为Function(紧凑的方式):

class Foo {

    toggle: Function = () {
        ...
    }

}

或者,您可以将签名和实际方法分开:

class Foo {

    toggle: Function;

    toggle() {
        ...
    }

}

或者,如你所说

参见 this issue了解更多详情

关于javascript - flowtype 绑定(bind)导致错误 `Convariant property incompatible with contravariant use in assignment of property`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45856887/

相关文章:

javascript - 将二维数组填充到表中(输出所有可能的组合)

javascript - 用 vanilla javascript 重写 React 功能组件

android - 无法在额外属性扩展上获取属性 'ndkVersion',因为它不存在于... react-native 0.64.0

android - 处理cameraX的图像分析时,JS UI被阻止

javascript - React Native : . map() 函数在渲染中不显示任何内容

javascript - 未找到隧道 URL,回退到 LAN URL

javascript - 使用 jQuery 控制 .fadeOut()

javascript - react : replace links in a text

ReactJS <输入类型 ="date"> 格式化日期

javascript - 使用对话框提示返回未定义