我正在尝试使用流程0.53.1。你能帮我解释一下这个奇怪的行为吗?流程中的递归类型定义
此代码示例:
/* @flow */
type AnySupportedType =
| AnySupportedPrimitive
| AnySupportedObject
| AnySupportedArray;
type AnySupportedArray = Array<AnySupportedType>;
type AnySupportedObject = { [string]: AnySupportedType };
type AnySupportedPrimitive = boolean | number | string | void;
type DataID = string
type Data = {
id: DataID
}
const y: Data = { id: "123" }
const x: AnySupportedType = y;
呈现这样的错误:
17: const x: AnySupportedType = y;
^object type. This type is incompatible with
17: const x: AnySupportedType = y;
^union: AnySupportedPrimitive | AnySupportedObject | AnySupportedArray
Link到flow.org基于web的例子一起玩。
是的,这似乎是这种情况。我想我应该明确地将数据对象的类型转换为“AnySupportedObject”,因为我知道这些类型应该兼容。如果Flow提供了一种方法来检查这种显式转换的正确性,那么它会很好,但现在它似乎缺乏。 – MOZGIII
这不太对。 Flow支持记录样式对象和地图样式对象之间的子类型关系。这里的问题实际上是可变的。我留下了一个解释的答案。 –