草庐IT

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

coder 2025-02-16 原文

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

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

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

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

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 - flowtype 绑定(bind)导致错误 `Convariant property incompatible with contravariant use in assignment of property`的更多相关文章

随机推荐