Is there a more efficient way to generate a Typescript type based on ENUM values? Currently, I find myself manually creating the type and ensuring it stays synchronized with the ENUM.
Can a type be automatically generated from ENUM values in such a way that any updates to the ENUM are dynamically reflected in the type definition?
enum COLORS {
RED = 'red',
BLUE = 'blue',
GREEN = 'green',
}
// type Colors = COLORS;
type Colors = 'red' | 'blue' | 'green';
// Test case 1
const color: Colors = COLORS.RED;
// Test case 2
const color2: Colors = 'red';
The defined type should seamlessly integrate into these scenarios:
const color: Colors = COLORS.RED;
const color2: Colors = 'red';