xdash / EnumFromList
Type alias: EnumFromList<T, CapitalizeKeys, CapitalizeValues>
EnumFromList<
T
,CapitalizeKeys
,CapitalizeValues
>:{ [K in T[number] as CapitalizeKeys extends true ? Capitalize<K> : K]: CapitalizeValues extends true ? Capitalize<K> : K }
Converts a list of strings to an enum type.
Example
const Colors = toEnum(['red', 'green', 'blue'])
console.log(Colors.Red) // 'red'
console.log(Colors.Green) // 'green'
console.log(Colors.Blue) // 'blue'
const Colors = toEnum(['red', 'green', 'blue'], { CapitalizeKeys: true, CapitalizeValues: true })
console.log(Colors.Red) // 'Red'
console.log(Colors.Green) // 'Green'
console.log(Colors.Blue) // 'Blue'
Type parameters
• T extends readonly string
[]
list of strings to convert to an enum
• CapitalizeKeys extends boolean
= true
whether to capitalize the keys of the enum
• CapitalizeValues extends boolean
= false
whether to capitalize the values of the enum