export enum OperationType {
HEAL = 1,
ATTACK
DEFEND = 20,
RETREAT,
// ...plus 30 more values of varying numbers.
};
I have the above numeric enum in TypeScript.
I have a string DEFEND
, I want to get the enum equivalent from OperationType
with this DEFEND
string. Is there a way to get the enum easily and reliably, other than creating a function and manually do a big switch/if-else loop to return the enum itself and maintaining that?
Edit:
This is my current code to loop through each of the enums to use it like OperationType["DEFEND"]
:
Object.keys(OperationType).filter((v) => isNaN(Number(v))).map((value) => {
return <MenuOption onSelect={() => setOperationType(OperationType[value])} text={value} />
})
and I am getting the following error for OperationType[value]
, Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)
.
I am assuming OperationType[value]
's value
is failing because it expects a string from key that matches OperationType
enums and not just "any" string. Is there a way to fix it?