我使用的JSON数据模型是一个由分支节点和特殊节点组成的嵌套结构,它们基于$type
键的存在而消除歧义,例如:
{ "a": { "b": { "$type": "special" } } }
我想使用Flow对此进行建模,但标记的联合似乎要求不同的标签具有相同的类型,例如:
// @flow
type Special = { $type: 'special'};
type Branch = { $type: ?null, [key:string]: any };
function isSpecial(param: Special | Branch): ?Special {
if (param.$type === 'special') {
return param;
}
}
给出以下错误:
$ node_modules/.bin/flow check-contents < example.js
-:2
2: type Special = { $type: 'special'};
^^^^^^^^^ string literal `special`. This type is incompatible with
3: type NotSpecial = { $type: ?null, [key:string]: any };
^^^^ null
-:3
3: type NotSpecial = { $type: ?null, [key:string]: any };
^^^^ null. This type is incompatible with
2: type Special = { $type: 'special'};
^^^^^^^^^ string literal `special`
-:3
3: type NotSpecial = { $type: ?null, [key:string]: any };
^^^^ undefined. This type is incompatible with
2: type Special = { $type: 'special'};
^^^^^^^^^ string literal `special`
Found 3 errors
有没有办法处理这样的案例?
答案 0 :(得分:0)
虽然理论上这应该有用,但Flow不支持。 Flow要求标签具有相同的类型。