如何从 Flow 中的对象数组获取 Union 类型?

问题描述 投票:0回答:0

例如,假设有以下项目列表:

const items = [
   {
       key: "amalgama"
   },
   {
       key: "torin"
   },
   {
       key: "mascarpone"
   }
]

什么流类型

ItemsKeyTuple
定义应该得到以下检查?

type ItemsKeyTuple = ...some stuff with items...

let a: ItemsKeyTuple;

a = "amalgama" // OK;

a = "tomatoes" // ERROR;

主要思想是在使用内部 if-else、switch 语句时获得即时检查和帮助,考虑到将来某个时刻更改密钥的可能性,并确保我会被

Flow
注意到我需要的地方调整。

附言更新了问题标题,我用错了名字

flowtype
© www.soinside.com 2019 - 2024. All rights reserved.