【发布时间】:2021-07-09 13:41:33
【问题描述】:
在 Typescript 中,有一种方法可以获取数组或元组的大小并将其用作类型:
type Tuple = [number, string, boolean];
type Arr = [0, 1, 2];
type LengthOf<T extends any[]> = T["length"];
type Test = LengthOf<Tuple>; // 3
type Test2 = LengthOf<Arr>; // 3
有没有办法用枚举做同样的事情(提取长度/大小)? 枚举示例:
enum Example {
how = "how",
to = "to",
count = "count",
enum = "enum",
entries = "entries"
}
为了清楚起见,我有兴趣将其作为类型 NOT 值。我知道Object.keys(Example).length。
【问题讨论】:
标签: typescript enums