无法模拟不变多态类的数组

时间:2017-04-02 17:56:53

标签: javascript flowtype

有没有办法在没有注释A< + T>?

的情况下对此进行建模
class A<T> {
  static value: T  
}

declare var Bar: Class<A<string>>
declare var Baz: Class<A<number>>

function foo<T, U: T>(...rest: $ReadOnlyArray<Class<A<U>>>): Array<T> {
  return rest.reduce((init, { value }) => ([ ...init, value ]), [])
}

(foo(Bar, Baz): Array<string | number>)

错误

5: declare var Bar: Class<A<string>>
                            ^ string. This type is incompatible with
6: declare var Baz: Class<A<number>>
                            ^ number
6: declare var Baz: Class<A<number>>
                            ^ number. This type is incompatible with
5: declare var Bar: Class<A<string>>
                            ^ string

https://flow.org/try/#0MYGwhgzhAECCA8AVAfNA3gKGtCAXMuAlsNAG5ggCuApgFzSLYYC+GGAJtaGAE7Vm9oAIV70AwuCjwEeHoQB2Ac2TIOXcHwE9hYAF7jJEafHmUAtgCNqPFVmz2MAM0rzgRAPbzoj9+6QAaaABVehQACgA6KL48egASACVqMHYAeXkQAE9YHh4wTPgJSCMEIJVkAEp6HLyClHQ7PlxKHi8Y3Ai+dkpgajCwhUJcQLQBKn5mCugAXlQwgG1oKIjB4bGaaABdCsD57ZY7ewcwn3cwkR5AkV0quFz8+FkFRWgAH2hTS2tKoA

1 个答案:

答案 0 :(得分:0)

我不是Flow的专家,但我相信你可以通过改变

来实现这一目标
$ReadOnlyArray<Class<A<U>>>

$ReadOnlyArray<{+value: U}>