I would like dirVectors[Turn.Straight]
to fail at compile-time.
enum Direction {
Up,
Down,
Right,
Left,
}
enum Turn {
Clockwise,
Counterclockwise,
Straight,
}
const dirVectors = {
[Direction.Up]: [0, 1],
[Direction.Down]: [0, -1],
[Direction.Right]: [1, 0],
[Direction.Left]: [-1, 0]
} as Record<Direction, [number, number]>;
I'm assuming the reason dirVectors[Turn.Straight]
is OK is because they're both numbers, with Straight = 2
, which is a subset of Direction {0,...,3}
. When I assign a unique string to each enum's value, it does fail at compile-time. However, is it possible to get the compile-time error without going the string route?