I'm looking for a way to create a Typescript type from ENUM values. Currently I'm having to manually construct the type and keep it in sync with the ENUM.
Is there a way to create a type as a oneof from ENUM values? End result being when I update the ENUM the type will automatically reflect it
enum FRUITS {
APPLE = 'apple',
PEAR = 'pear',
ORANGE = 'orange',
}
// type Fruits = FRUITS;
type Fruits = 'apple' | 'pear' | 'orange';
// This works
const fruit: Fruits = FRUITS.APPLE;
// This also works
const fruit2: Fruits = 'apple';
The type will be used in these scenarios so both have to still work:
const fruit: Fruits = FRUITS.APPLE;
const fruit2: Fruits = 'apple';