If you really want to do this, then you could assert the values to any:
enum AllDirections {
TOP = top as any,
BOTTOM = bottom as any,
LEFT = left as any,
RIGHT = right as any
}
The problem with this, is that if you're assigning these to string values, then it will require an assertion to a string. That's not ideal:
let str: string = AllDirections.TOP as any as string;
Alternatively, it's a little verbose, but if you want the members to have the correct type you could consider using an object:
// remove the explicit string types so that these are typed
// as their 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 option is to flip where the string is 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;
topandAllDirections.TOP?