How can you compare a value to a list of enum values, using TypeScript?
For example, in 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 achieve this functionality without creating a separate helper class for each enum type (especially when dealing with multiple enum types)?
My current best solution is:
export enum Animal {
CAT, DOG, BIRD, SNAKE
}
export namespace Animal {
export const Mammals = [Animal.CAT, Animal.DOG];
}
//Usage: Animal.Mammals.includes(myPet)
This approach combines the enum and the list together for easier usage but still requires defining both a namespace and an enum separately. Additionally, namespaces are considered outdated.