流继承-类B扩展了与A不兼容的A

Hua*_*afu 3 javascript flowtype

我希望这可以正常工作,并且不会在流中输出错误:

/* @flow */

class TreeNode {
  root: TreeNode
}

class RootNode extends TreeNode {}

class OtherNode extends TreeNode {
   root: RootNode 
}
Run Code Online (Sandbox Code Playgroud)

但是我得到了:TreeNode. This type is incompatible with RootNode。芹苴RootNode延伸TreeNode

我究竟做错了什么?

您可以在此处看到问题https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjGBDAzrsAFQCcBTUgOTgBNSwBvVMMYuOAFwc4izKbTUAX3RY8BAEpt2VWmFIAPdqQB21AiXIy69YZhz4wA1S

Yur*_*nko 5

默认情况下,字段和属性是不变的,因为您都可以读取和写入它们。您需要将字段标记为协变以允许子类

/* @flow */

class TreeNode {
  +root: TreeNode
}

class RootNode extends TreeNode {}

class OtherNode extends TreeNode {
   root: RootNode 
}
Run Code Online (Sandbox Code Playgroud)

演示进一步阅读以获得更好的理解。