what is use of enum in typescript. If it's purpose is only to make code redable can't we just use constant for same purpose
enum Color {
Red = 1, Green = 2, Blue = 4
};
let obj1: Color = Color.Red;
obj1 = 100; // does not show any error in IDE while enum should accept some specific values
If there is no advantage of typechecking, Can't it be just written as.
const BasicColor = {
Red: 1,
Green: 2,
Blue: 4
};
let obj2 = BasicColor.Red;
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:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With