How to check a value against a list of enum values, in TypeScript?
I.E., Java:
public enum Animal {
CAT, DOG, BIRD, SNAKE
public static EnumSet<Animal> Mammals = EnumSet.of(CAT, DOG);
}
Usage: Animal.Mammals.contains(myPet)
Is there a way to do something like that without predefining a helper class for each enum (which can feel a little accessive with multiple enum types)?
The best solution I have so far is:
export enum Animal {
CAT, DOG, BIRD, SNAKE
}
export namespace Animal {
export const Mammals = [Animal.CAT, Animal.DOG];
}
//Usage: Animal.Mammals.includes(myPet)
...Which is nice because the usage unifies the enum with the list but still requires defining both namespace and enum separately. Also, namespaces are considered outdated.