【发布时间】:2021-09-05 08:52:14
【问题描述】:
我正在使用的库导出这样的类型:
type Pet = 'dog' | 'cat' | 'rat';
我现在想在我的 UI 中创建一个包含所有这些值的下拉菜单。我无法枚举此 const 类型。所以我想创建一个这些值的数组并键入它,这样它必须在类型 Pet 中的每个 const 中都有一个,但无法弄清楚。
我尝试了以下方法,但是当更多键添加到Pet 时不会导致错误。当我更新 lib 并且 lib 决定添加更多 const 时,我希望 typescript 在构建时失败。
type Pet = 'dog' | 'cat' | 'rat';
const pets: Pet[] = ['dog', 'dog']; // this should error as its missing "cat" and "rat". and also it has "dog" twice.
【问题讨论】:
标签: typescript