If you're keen on pursuing this approach, one option is to define the values as any
:
enum AllDirections {
TOP = top as any,
BOTTOM = bottom as any,
LEFT = left as any,
RIGHT = right as any
}
An issue with this method arises if you are assigning these values as strings, necessitating an additional assertion to convert them to string type. Not an ideal situation:
let str: string = AllDirections.TOP as any as string;
Alternatively, while a bit more verbose, you can ensure that the members have the correct type by utilizing an object:
// exclude explicit string types for proper typing of
// string literal values
const top = 'top';
const bottom = 'bottom';
const left = 'left';
const right = 'right';
type AllDirections = Readonly<{
TOP: typeof top,
BOTTOM: typeof bottom,
LEFT: typeof left,
RIGHT: typeof right
}>;
const AllDirections: AllDirections = {
TOP: top,
BOTTOM: bottom,
LEFT: left,
RIGHT: right
};
Another approach would be flipping where the string values are stored:
enum AllDirections {
TOP = 'top',
BOTTOM = 'bottom',
LEFT = 'left',
RIGHT = 'right',
}
const top = AllDirections.TOP;
const bottom = AllDirections.BOTTOM;
const left = AllDirections.LEFT;
const right = AllDirections.RIGHT;