gpt4 book ai didi

javascript - 在 Flow 0.34 中定义自定义 `toString`

转载 作者:行者123 更新时间:2023-11-30 00:01:29 26 4
gpt4 key购买 nike

我有一些函数,我想有一个自定义的 toString 属性,就像这样:

var Foo = function () {};
Foo.prototype.toString = function () { return 'hi'; };

Here's a live example of the code.

在 Flow 0.33 中它可以正常工作,但在 Flow 0.34 中它会产生错误:

4: Foo.prototype.toString = function () { return 'hi'; };
^ Object. Covariant property `toString` incompatible with contravariant use in
4: Foo.prototype.toString = function () { return 'hi'; };
^ assignment of property `toString`

这是我的代码或 Flow 中的错误吗?

最佳答案

您可以使用 any 类型转换来解决协方差约束:

(Foo.prototype : any).toString = function () { return 'hi'; };

您的代码没有错,流程只是强制执行特定的样式。

关于javascript - 在 Flow 0.34 中定义自定义 `toString`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40323189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com