Flow无法识别需要回调的回调参数内的实现类

时间:2019-04-28 18:20:20

标签: flowtype

我有以下设置:

/* @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;

try it

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

1 个答案:

答案 0 :(得分:0)

这是预期的行为。这必然导致callback函数修改其data参数的可能性。具体而言,如果回调函数接受ISelectable,则它可以与data进行交互,并保留其ISelectable类型。因此,callback可能会修改BaseFileTile,打破其类型为BaseFileTile,但保持其类型为ISelectable

要执行此操作,您将需要使用$ReadOnly utility typeTry Flow)将data参数标记为“只读”。这告诉Flow,data参数不会被修改,因此BaseFileTile将保留为BaseFileTile