Enum access question

If I have
export enum Options {
'bool' = 'Boolean',
'num' = 'Number'
'str' = 'String'
}
export enum Options {
'bool' = 'Boolean',
'num' = 'Number'
'str' = 'String'
}
and I want to do something like this
['bool', 'num', 'str'].map(i => console.log(Options.i))
['bool', 'num', 'str'].map(i => console.log(Options.i))
Is there a way to do this? like if I am iterating and know the values I'm iterating will be keys to the enum, is there a way to us the iteration data as the key to the enum??
2 Replies
Donny D
Donny DOP15mo ago
and I mean in a simple way, I know I can do it with something like Object.values(Options)[Object.keys(Options).indexOf(i)] but that seems needless complex
Matvey
Matvey15mo ago
Just don't use enums. Use an object. It's just better. Enums are one of the worst features of TS.
const optionsMap = {
'bool': 'Boolean',
'num': 'Number'
'str': 'String'
} as const;

type Option = keyof optionsMap;

const options: Option[] = ['bool', 'num', 'str'];

options.forEach(i => console.log(optionsMap[i]))
const optionsMap = {
'bool': 'Boolean',
'num': 'Number'
'str': 'String'
} as const;

type Option = keyof optionsMap;

const options: Option[] = ['bool', 'num', 'str'];

options.forEach(i => console.log(optionsMap[i]))
Want results from more Discord servers?
Add your server