打字稿:根据枚举成员的数量获取具有固定长度的元组

时间:2020-06-01 03:58:48

标签: typescript types enums tuples

假设我有一个枚举

enum Chest {
  ID,
  NAME,
  CAPACITY,
  OPEN_WIN_POINTS,
}

我想制作一个与枚举中的值数长度相同的元组。因此,对于上面的示例,我希望使用长度为4的字符串元组:

type ChestStringTuple = EnumTuple<string, Chest>

const correctTuple: ChestStringTuple = ['foo', 'bar', 'baz', 'goo']
const incorrectTuple1: ChestStringTuple = ['foo', 'bar', 'baz']  // The number of tuple members differs from 4
const incorrectTuple2: ChestStringTuple = ['foo', 'bar', 'baz', false]  // The tuple has non-string members

有什么方法可以获取枚举成员的数量并将其用作元组长度吗?

0 个答案:

没有答案