I'm looking into enums in TypeScript and wrote the following piece of code for test purposes:
enum IntMood {
good = 1,
neutral = 2,
bad = 3,
}
enum StringMood {
good = "1",
neutral = "2",
bad = "3",
}
const m1: IntMood = 1;
const m2: IntMood = 2349875; // no error?
const m3: StringMood = "1"; // Error: Type '"1"' is not assignable to type 'StringMood'
What is going on here? Why is TypeScript behaving this way?
Note that I am interested in the reasoning of the TypeScript developers and the compiler internals here, and not in how to properly convert an integer/string to an enum to an enum type. After all, the latter question already been answered extensively (though one might argue that all present solutions are quite ugly).