定义类
interface Item {
value: number;
label: string;
[x: string]: any;
}
abstract class Enum {
public static states: readonly Item[]
public static make () {
const res: any = {}
this.states.forEach(v => {
res[v.value] = v as Item
})
return res
}
public static value: () => Record<typeof this.states[number]['value'], Item> = Enum.make
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
实际类
class CPUEnum extends Enum {
public static states = [
{
value: 0,
label: 'intel',
title: 'intel i11'
}
] as const
public static value: () => Record<typeof this.states[number]['value'], Item>
}
测试效果
console.info(CPUEnum.value()[0])
结语
- 初步实现了数组枚举 ts 代码提示
- 无法实现自动填充 value