on-internet
← Community

How to check value against a group of enum values?

Stack Overflow ·

How to check a value against a list of enum values, in TypeScript? I.e., 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 do something like that without predefining a helper class for each enum (which can feel a little excessive 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 becaus

Comments

Sign in or sign up to comment and upvote.

No comments yet.