我有一堆带枚举的数组和人类可读的对应物。
我希望能够动态地为每个可能的变体构建一个枚举联合类型,因为一些数组相当长。
interface EnumType { enumValue: string, value: string }
export type enumArrayType = Array<EnumType>
export const cpuSocket: enumArrayType = [
{ enumValue: 'LGA1150', value: 'LGA 1150' },
{ enumValue: 'LGA1151', value: 'LGA 1151' }
]
export const cpuBrand: enumArrayType = [
{ enumValue: 'INTEL', value: 'Intel' },
{ enumValue: 'AMD', value: 'Amd' }
]
type CpuBrandEnumTypes = 'INTEL' | 'AMD'
type cpuType = {
brand: CpuBrandEnumTypes
}
const cpu: cpuType = {}
cpu.brand: CpuBrandEnumTypes = INTEL // WORKS!
cpu.brand: CpuBrandEnumTypes = intel // ERROR!
cpu.brand: CpuBrandEnumTypes = amd // ERROR!
cpu.brand: CpuBrandEnumTypes = AMD // WORKS!