我正在使用// @flow strict
,并且禁止使用*
和any
。
我想创建一个具有可变返回类型的函数数组,但仍要严格使用这些函数。
Here's an example
我需要使用mixed
返回类型存储函数。我尝试了协方差,但是不确定如何进行。有没有一种方法可以解决此示例中的错误而不隐藏返回类型?
最佳答案
为了推入该数组,您需要mark Selector
's template argument as covariant。你可以改变
type Selector<T> = (state: S) => T;
成为
type Selector<+T> = (state: S) => T;
如您所见,
<T>
现在为<+T>
,因此您可以安全地从Y
转换为mixed
。