将 属性 与枚举传递给期望 属性 与字符串的函数

Passing property with enum to function expecting property with string

有人能解释一下吗,为什么流不接受限制性更强类型的参数来运行期望限制性更小的类型,当它是一个对象时 属性?

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCSAdjgK4ZgC8YA3qmGNvgFxgDOGATgJbEDmAGlQBfANzpmBAPIVylGvUZS2AcgByAQwBuq4SPQBjOMU5gocONTAAKAJRtZGedQB8tpU1x41W3frsJVGNTSgAjTS5rGz55NhJ5Ozc6BjAQ9jgYPAA6eH4bVViKPTAijECwUXQIrhsLOHtAoA

/* @flow */

type Input = {
  type: string,
};

type Output = {
  type: 'Nav',
}

const foo = (): Output => ({
  type: 'Nav',
});

const bar = (input: Input) => {
  console.log('input', input); 
}

bar(foo());

错误:

19: bar(foo());
        ^ Cannot call `bar` with `foo()` bound to `input` because string literal `Nav` [1] is incompatible with string [2] in property `type`.
References:
8:   type: 'Nav',
           ^ [1]
4:   type: string,
           ^ [2]

我是否遗漏了文档中的内容?

variance 上的文档应该让您开始理解为什么更具体的输入会很麻烦。在您的情况下,Flow 不知道您的函数将如何处理输入,因此担心 bar 可能会修改其输入。例如,bar 可能会将 input.type 更改为 'some string',这将违反 Output 类型。您可以将 input 标记为 $ReadOnly<Input> 类型,以向 Flow 保证 bar 不会修改 input.