Flow javascript类型使用单个元素类型的数组的并集检查递归定义

Flow javascript类型使用单个元素类型的数组的并集检查递归定义,javascript,flowtype,Javascript,Flowtype,我有一个树结构,定义如下。options属性是ParentActions数组或选项数组。但是我不知道如何让flow typechecker正确地检查这一点 // @flow type Option = { label: string, action: string, type: 'leaf' } type ParentActions = { prompt: string, depth: number, label: string, options: Array<

我有一个树结构,定义如下。options属性是ParentActions数组或选项数组。但是我不知道如何让flow typechecker正确地检查这一点

// @flow
type Option = {
  label: string,
  action: string,
  type: 'leaf'
}

type ParentActions = {
  prompt: string,
  depth: number,
  label: string,
  options: Array<ParentActions> | Array<Option>,
  type: 'parent'
}

type RootActions = {
  prompt: string,
  options: Array<ParentActions> | Array<Option>,
  type: 'root'
}

const thisDoesNotTypeCheckCorrectly : RootActions = {
  options: [
    {
      label: 'string',
      action: 'string',
      type: 'leaf'
    },
    {
      label: 'string',
      action: 'string',
      type: 'leaf'
    }
  ],
  type: 'root'
};
/* Could not decide which case to select
union type: test.js: 15
Case 1 may work:
array type: test.js: 15
But if it doesn't, case 2 looks promising too:
array type: test.js: 15
Please provide additional annotation(s) to determine whether case 1 works (or consider merging it with case 2):
inferred union of array element types (alternatively, provide an annotation to summarize the array element type) */
/@flow
类型选项={
标签:字符串,
动作:字符串,
类型:“叶”
}
类型ParentActions={
提示:字符串,
深度:数字,
标签:字符串,
选项:数组|数组,
类型:“父级”
}
类型RootActions={
提示:字符串,
选项:数组|数组,
类型:“根”
}
const thisDoetTypeCheck正确:RootActions={
选项:[
{
标签:“字符串”,
动作:“字符串”,
类型:“叶”
},
{
标签:“字符串”,
动作:“字符串”,
类型:“叶”
}
],
类型:“根”
};
/*无法决定要选择的案例
接头类型:test.js:15
案例1可能适用于:
数组类型:test.js:15
但如果不是这样,案例2看起来也很有希望:
数组类型:test.js:15
请提供额外的注释来确定案例1是否有效(或考虑将其与案例2合并):
数组元素类型的推断并集(或者,提供注释以汇总数组元素类型)*/
试试这个:

type RootActions = {
  prompt: string,
  type: 'root',
} & ({ options: Array<ParentAction> } | { options: Array<Option> })
类型RootActions={
提示:字符串,
类型:'根',
}&({options:Array}{options:Array})