有没有办法在流泛型中设置混合协方差?

Is there a way to setup mixed covariance in flow generics?

我正在使用 // @flow strict 并且禁止使用 *any

我想创建一个具有可变 return 类型的函数数组,但仍以严格的方式使用这些函数。

Here's an example

我需要用 mixed return 类型存储函数。我试过协方差,但我不确定它在流程中是如何工作的。有没有办法在不隐藏 return 类型的情况下修复此示例中的错误?

为了推入那个数组,你需要mark Selector's template argument as covariant。你可以改变

type Selector<T> = (state: S) => T;

成为

type Selector<+T> = (state: S) => T;

如您所见,<T> 现在是 <+T>,因此您可以安全地从 Y 转换为 mixed