Explain Codes LogoExplain Codes Logo

Check if value exists in enum in TypeScript

javascript
enum-types
typescript
value-checks
Anton ShumikhinbyAnton Shumikhin·Dec 5, 2024
TLDR

Quickly determine if an enum in TypeScript contains a value by using the in operator for keys or Object.values() for values.

Check out this concise example:

enum MyEnum { A = 'Alpha', B = 'Bravo' } const inputValue = 'Alpha'; // Enum key check const isKey = 'A' in MyEnum; // true // Enum value check const isValue = Object.values(MyEnum).includes(inputValue); // true

Here, isKey determines if the enum key exists, while isValue verifies the existence of a value within the enum.

Enum types and value checks

Enums in TypeScript can be diverse, requiring different approaches for value existence checks. Let's go over the most common types: string and number-based enums.

Checking values in string enums

For string enums, Object.values() is your best mate:

enum StringEnum { First = "Uno", Second = "Dos" } const isExist = Object.values(StringEnum).includes("Dos"); // true, "Dos" is here

But stay alert! If your ECMAScript version isn't correctly set, you may encounter some errors. Don't sweat it, though. Just update your tsconfig.json:

{ "compilerOptions": { "target": "ES2017" } }

Occasionally, TypeScript may grumble about unexpected types. To hush it, use an explicit cast:

const enumValues = Object.values(StringEnum) as Array<StringEnum>;

Checking values in number-based enums

With number-based enums, it's a little different game:

enum NumberEnum { One = 1, Two } const isTwo = 2 in NumberEnum; // false, 2 is MIA

To verify a value, use the bracket notation based on enum's property lookup:

const isOne = NumberEnum[NumberEnum.One] !== undefined; // true, NumberEnum.One exists

Extra points: advanced checking techniques

What if basic usage doesn't cut it? Advanced tactics to the rescue!

Taming diverse enum values with casting

When your enum values are a motley crew of types, type assertion is a game changer:

const enumValue = Object.values(StringEnum) .find(value => value === "Dos") as StringEnum; // Look, mom, no type errors!

Enum iteration for expanded verification

When faced with complex checks or runtime enums, iteration is worth its weight in gold:

for (const [key, value] of Object.entries(NumberEnum)) { if (value === yourValue) { // Bingo! Found the value. } } // Or enlist Array's some() method const doesExist = Object.values(StringEnum).some(val => val === "Dos"); // "Dos" made the cut!

Beware the enum quirks

Enums can be a little sneaky:

  • Using const enums is tricky since they get inlined at compile time.
  • Runtime type checks might need manual iteration due to TypeScript transpiling.
  • TypeScript doesn't do reverse mapping for string enums like it kindly does for number enums.

Enums on hard mode: special cases

Sometimes enums play hardball. Here are some expert techniques:

Tackling mixed type enums

If you're dealing with a complex enum, juggle your checks:

enum MixedEnum { First = 1, Second = "Two" } // When in doubt, double check const doesExist = ('First' in MixedEnum && MixedEnum['First'] === 1); // Clunky but safe

Type narrowing for enums

User-defined type guards come in handy when using enums:

function isEnumValue(value): value is StringEnum { return Object.values(StringEnum).includes(value); // Type guard to the rescue! }