How can I dynamically assign a value to a TypeScript enum variable?
Given:
enum options { 'one' = 'one', 'two' = 'two', 'three' = 'three'}
let selected = options.one
I want to set the variable "selected" based on a string value, and the following function accomplishes this:
function setSelected (newOption: string): void {
switch (newOption) {
case 'one':
selected = userThemePreferences.one
break
case 'two':
selected = userThemePreferences.two
break
default:
selected = userThemePreferences. three
break
}
}
Is it possible to create a function that remains unaffected by changes in the enum items? Currently, if we add 'four' to our enum, we would need to update the switch statement cases. How can we avoid this scenario?