I'm attempting to achieve a setup similar to the following example using Typescript:
const fruitApple:string = 'Apple';
const fruitOrange:string = 'Orange';
export type FruitOptions = fruitApple | fruitOrange //the compiler doesn't allow this.
//my goal is to have the type defined through variables rather than hard-coded strings, making it simpler for future refactoring
const chosenFruit:FruitOptions = fruitOrange;
Instead of directly entering the union types as string literals for FruitOptions
, my objective is to utilize variables for more flexibility, enabling easy modifications without rewriting as fixed strings. I was exploring a potential alternative to using numeric enums in Typescript.
Is it feasible to utilize a variable's value as one of the options in a union type?