Javascript 为什么';不知道可为null的属性与不可为null的对象属性兼容吗?

Javascript 为什么';不知道可为null的属性与不可为null的对象属性兼容吗?,javascript,flowtype,refinement-type,Javascript,Flowtype,Refinement Type,我试图理解为什么flow抱怨两个非常相似的对象()的属性之间不兼容: Flow给出了上述代码的以下错误: 17: foo(elsa) ^ NamedCow. This type is incompatible with the expected param type of 10: function foo (c: Cow): string { ^ Cow Property `name` is incompatible:

我试图理解为什么flow抱怨两个非常相似的对象()的属性之间不兼容:

Flow给出了上述代码的以下错误:

17: foo(elsa)
        ^ NamedCow. This type is incompatible with the expected param type of
10: function foo (c: Cow): string {
                     ^ Cow
    Property `name` is incompatible:
        7:   name: string
                   ^ string. This type is incompatible with
        3:   name: ?string
                   ^ null or undefined
为什么更具体的
名称:string
与不太具体的
名称:?string
不兼容?由于
NamedCow
的name属性是Cow的name属性的子集,因此
NamedCow
在name属性上不是与
Cow
协变的吗

相关文件:
对象类型是不变的。这是因为可以读取和写入对象属性

为了了解协方差是不安全的,考虑一个函数,该函数在<代码> Foo函数

中赋予<代码> null >代码>属性>代码>名称>代码>。 传递
Cow
类型:分配给
name=null
不会引起问题,因为
name
可以是
?字符串

但是,如果您传递一个
NamedCow
类型,则对
name=null
的赋值将违反
name
的类型,该类型仅为
string



通过在
+
前面加上前缀,即
+名称:?字符串,可以将
名称注释为协变。这表示不会对其执行写入操作。这将清除您当前看到的错误。(属性方差修饰符为)

感谢您的解释。文档似乎推断方差修饰符只适用于接口,是这样吗?另外,我在文档中发现了一行提到了对象属性不变性的内容,希望它能得到更突出的解释,或者至少在方差部分得到解释:方差修饰符将应用于对象类型,如图所示
17: foo(elsa)
        ^ NamedCow. This type is incompatible with the expected param type of
10: function foo (c: Cow): string {
                     ^ Cow
    Property `name` is incompatible:
        7:   name: string
                   ^ string. This type is incompatible with
        3:   name: ?string
                   ^ null or undefined