I've found that whenever I need to check if a value exists in an enum, I don't really need an enum and that a type is a better solution. So my enum in my original answer becomes:
export type ValidColors =
| "red"
| "orange"
| "yellow"
| "green"
| "blue"
| "purple";
For clarity, I like to break the values
and includes
calls onto separate lines. Here's an example:
export enum ValidColors {
Red = "red",
Orange = "orange",
Yellow = "yellow",
Green = "green",
Blue = "blue",
Purple = "purple",
}
function isValidColor(color: string): boolean {
const options: string[] = Object.values(ButtonColors);
return options.includes(color);
}