What you've created here is a numeric enum. The generated JavaScript looks like this:
var Gender;
(function (Gender) {
Gender[Gender["MALE"] = 1] = "MALE";
Gender[Gender["FEMALE"] = 0] = "FEMALE";
})(Gender || (Gender = {}));
In the console, this object looks like this:
Object
0: "FEMALE"
1: "MALE"
FEMALE: 0
MALE: 1
Under the hood, the enum is really just a JavaScript object with properties. It has the named properties you defined,
and they are assigned a number representing the position in the enum that they exist (FEMALE being 0, MALE being 1), but
the object also has number keys with a string value representing the named constant.
Therefore, you can pass in numbers to a function that expects an enum. The enum itself is both a number and a defined constant.
This appears to defeat the type-safe aspect of TypeScript, since you can pass an arbitrary number to a function expecting this enum.
There are two reasons why this is useful, though:
- When you receive numeric data (say, in a JSON payload from some service), you can convert it to an enum.
- Since the enum members have numeric values, you can also use them as bitflags (although since you can set the numeric value
for each enum member, as you have done here, this may or may not work well).
Regarding point (1): if you declare an enum without numeric values, e.g. this:
enum Gender {
MALE = "Male",
FEMALE = "Female"
}
then you can no longer do this:
const gender: Gender = "Male";
You'll have to do an explicit cast:
const gender = "Male" as Gender;