小编Jay*_*Lee的帖子

如何限制数组包含 TypeScript 中枚举的每个成员

enum AllowedFruits {
  Apple = 'APPLE',
  Banana = 'BANANA',
  Pear = 'PEAR'
}

const allowedFruits: AllowedFruits[] = [
  AllowedFruits.Apple, AllowedFruits.Banana, AllowedFruits.Pear
]
Run Code Online (Sandbox Code Playgroud)

我想要实现的是将数组限制为具有特定枚举的每个字段。我期望allowedFruits通过添加或删除 的字段来显示类型错误AllowedFruits

有什么办法可以实现吗?

如果有任何文章或文档可供我参考,请告诉我。

enums typescript mapped-types

5
推荐指数
1
解决办法
1851
查看次数

标签 统计

enums ×1

mapped-types ×1

typescript ×1