Is there any way to enforce strict usage of enum? Some examples:
enum AnimalType {
Cat,
Dog,
Lion
}
// Example 1
function doSomethingWithAnimal(animal: AnimalType) {
switch (animal) {
case Animal.Cat: // ...
case Animal.Dog: // ...
case 99: // This should be a type error
}
}
// Example 2
someAnimal.animalType = AnimalType.Cat; // This should be fine
someAnimal.animalType = 1; // This should be a type error
someAnimal.animalType = 15; // This should be a type error
Basically, if I say that something has an enum type, then I want the TypeScript compiler (or alternatively tslint) to make sure that it is used correctly. With the current behavior I don't really understand the point of enums as they are not enforced. What am I missing?