First, in the following:
const BasicColor = {
Red: 1,
Green: 2,
Blue: 4
};
Red
, Green
, and Blue
are still mutable (whereas they are not in an enum).
Enums also provide a few things:
- a closed set of well known values (that won't permit typos later on), each which has...
- a respective set of literal-like types for each member, all which are provided...
- by a single named type which encompasses all values
To get that with something like a namespace, for example, you have to do something like
export namespace Color
export const Red = 1
export type Red = typeof Red;
export const Green = 2;
export type Green = 2;
export const Blue = 3;
export type Blue = typeof Blue;
}
export type Color = Color.Red | Color.Blue | Color.Green
What you're also noting is some unfortunate legacy behavior where TypeScript permits assignment from any numeric value to a numeric enum.
But if you're using a string enum, you won't get that behavior. There are also other things like exhaustiveness checking that you can enable with union enums:
enum E {
Hello = "hello",
Beautiful = "beautiful",
World = "world"
}
// if a type has not been exhaustively tested,
// TypeScript will issue an error when passing
// that value into this function
function assertNever(x: never) {
throw new Error("Unexpected value " + x);
}
declare var x: E;
switch (x) {
case E.Hello:
case E.Beautiful:
case E.World:
// do stuff...
break;
default: assertNever(x);
}