Flow无法识别在需要回调的回调参数中实现类 [英] Flow doesn't recognize implementing class inside a callback parameter that takes a callback

查看:85
本文介绍了Flow无法识别在需要回调的回调参数中实现类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

/* @flow */
interface ISelectable {
onSelectUntil(callback: (data : ISelectable) => void, until : (callToRemoveCallback : Function, callback: (data : ISelectable) => void) => void): void;
}

class BaseFileTile implements ISelectable {
  onSelectUntil(callback: (data : BaseFileTile) => void, until : (callToRemoveCallback : Function, callback: (data : BaseFileTile) => void) => void) : void {}
}

问题在于,这引发了:

7:   onSelectUntil(callback: (data : BaseFileTile) => void, until : (callToRemoveCallback : Function, callback: (data : BaseFileTile) => void) => void) : void {}
                                                                                                                        ^ Cannot implement `ISelectable` [1] with `BaseFileTile` because `BaseFileTile` [2] is incompatible with `ISelectable` [3] in the first argument of the second argument of the second argument of property `onSelectUntil`.
References:
6: class BaseFileTile implements ISelectable {
                                 ^ [1]
7:   onSelectUntil(callback: (data : BaseFileTile) => void, until : (callToRemoveCallback : Function, callback: (data : BaseFileTile) => void) => void) : void {}
                                                                                                                        ^ [2]
3: onSelectUntil(callback: (data : ISelectable) => void, until : (callToRemoveCallback : Function, callback: (data : ISelectable) => void) => void): void;

尝试

在第一个回调中,Flow正确地识别出BaseFileTile的类型为ISelectable,但是在另一个回调中,将引发错误.是否需要此行为或它是一个错误?

While in the first callback Flow correctly recognizes that BaseFileTile is of type ISelectable but in the other callback an error is thrown. Is this behavior wanted or is it a bug?

推荐答案

这是预期的行为.这必须解决callback函数修改其data参数的可能性.具体来说,如果回调函数接受ISelectable,则它可以以任何保留其ISelectable类型的方式与data进行交互.因此,callback可能会修改BaseFileTile,将其类型更改为BaseFileTile,但将其类型保留为ISelectable.

This is expected behavior. This has to do the possibility that the callback function modifies its data argument. Specifically, if the callback function accepts an ISelectable it can interact with data in any way that preserves its ISelectable type. Thus, callback could potentially modify a BaseFileTile, breaking its type as a BaseFileTile but maintaining its type as an ISelectable.

要执行此操作,您将需要使用 实用型(尝试Flow ).这告诉Flow不会修改data参数,因此BaseFileTile将保留为BaseFileTile.

To make this work, you will want to mark the data argument as "read only" by using the $ReadOnly utility type (Try Flow). This tells Flow that the data argument will not be modified and so a BaseFileTile will remain as a BaseFileTile.

这篇关于Flow无法识别在需要回调的回调参数中实现类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆