In the execution environment I'm working with, there are several global constants that represent different directions:
TOP = 1
TOP_RIGHT = 2
RIGHT = 3
BOTTOM_RIGHT = 4
BOTTOM = 5
BOTTOM_LEFT = 6
LEFT = 7
TOP_LEFT = 8
These constants are not just random values, but actually represent specific directions. I want to categorize them under the DIRECTION
type so that I can assign them like this:
let test: DIRECTION = TOP_LEFT;
let foo: DIRECTION = TOP;
let bar: DIRECTION = LEFT;
let target: DIRECTION = RIGHT;
I have explored different options on how to achieve this in the direction.d.ts
file:
- Using
enum
s requires a lot of syntax and boilerplate, making it less desirable in this scenario. - The
type
keyword seems limited in creating a new type that is not just an alias of another type. For example,type DIRECTION=integer;
works buttype DIRECTION;
does not. This approach is not ideal as it doesn't prevent nonsensical operations likeBOTTOM-2
. - Creating an
interface DIRECTION {}
seems to provide the desired outcome by defining a new type. However, it may be seen as abusing the typing system as interfaces are commonly used for classes.
What is the most TypeScript-friendly way to implement this?