How to Get Enum Key by Value in TypeScript?

Do you need to retrieve the key name from an enum value in TypeScript? As a TypeScript developer with many years of hands-on experience, I’ve faced this situation countless times—especially when working with data from APIs or user input that only provides enum values.

In this tutorial, I’ll walk you through multiple methods to get the key by value from a TypeScript enum with real examples.

What Are Enums in TypeScript?

Before we jump into solutions, let’s quickly recap what enums are in TypeScript. Enums (short for enumerations) allow you to define a set of named constants. They’re ideal for representing a fixed set of related values—like days of the week, order statuses, or US state codes.

Here’s a quick example:

enum OrderStatus {
  Pending = 'PENDING',
  Shipped = 'SHIPPED',
  Delivered = 'DELIVERED',
  Cancelled = 'CANCELLED'
}

Enums make your code more readable and maintainable, especially when working with a limited set of options.

Here is an example where you need to get the key from a value in a TypeScript Enum.

Let’s say you receive the value 'SHIPPED' from an API, but you need to display the more user-friendly key OrderStatus.Shipped in your UI or logs. Or maybe you’re mapping values to display labels and need to know which enum key matches a given value.

Check out How to Iterate Over Enums in TypeScript?

Method 1: Using Object.keys() and Object.values() (String Enums)

For TypeScript string enums, the most common approach is to use Object.keys() and Object.values() to search for the key by its value.

Here’s how I do it in real-world projects:

enum USState {
  California = 'CA',
  Texas = 'TX',
  NewYork = 'NY',
  Florida = 'FL'
}

function getEnumKeyByValue<T extends { [index: string]: string }>(
  enumObj: T,
  value: string
): keyof T | undefined {
  return (Object.keys(enumObj) as Array<keyof T>).find(
    key => enumObj[key] === value
  );
}

// Example usage:
const stateKey = getEnumKeyByValue(USState, 'TX'); // Returns "Texas"
console.log(stateKey); // Output: Texas

Why it works:
This method loops over the keys and checks which key’s value matches the search value. It’s simple and works well for most string enums.

Here is the exact output in the screenshot below:

typescript enum get key from value

Read Convert String to Enum in TypeScript

Method 2: Using Object.entries() for Direct Key-Value Mapping

If you want a more direct approach, especially when you may want both key and value, use Object.entries():

Here is an example.

enum USState {
  California = 'CA',
  Texas = 'TX',
  NewYork = 'NY',
  Florida = 'FL'
}

function getEnumKeyByValueEntries<T extends { [index: string]: string }>(
  enumObj: T,
  value: string
): string | undefined {
  return Object.entries(enumObj).find(([key, val]) => val === value)?.[0];
}

// Example usage:
const key = getEnumKeyByValueEntries(USState, 'NY'); // Returns "NewYork"
console.log(key); // Output: NewYork

When to use:
This is perfect when you need both the key and value or want to process multiple matches.

You can see the exact output in the screenshot below:

typescript get enum key from value

Check out Merge Enums in TypeScript with Different Values

Method 3: For Numeric Enums (Reverse Mapping)

TypeScript’s numeric enums have a built-in reverse mapping, so you can access the key by value directly:

enum ResponseCode {
  Success = 200,
  NotFound = 404,
  Forbidden = 403
}

const key = ResponseCode[404]; // Returns "NotFound"
console.log(key); // Output: NotFound

This only works for numeric enums, not string enums. For string enums, you’ll need to use one of the previous methods.

I executed the above code and you can see the exact output in the screenshot below:

typescript get enum key by value

Read How to Compare String to Enum in TypeScript

Method 4: Utility Function for Reusability

If you find yourself needing this functionality often (I certainly do!), create a reusable utility:

function getEnumKeyByValueGeneric<T extends { [index: string]: string | number }>(
  enumObj: T,
  value: string | number
): string | undefined {
  return Object.keys(enumObj).find(key => enumObj[key] === value);
}

Use this utility for both string and numeric enums in your codebase for consistent results.

Method 5: With Type Safety and Generics

For projects where type safety is paramount, leverage TypeScript’s generics:

function getEnumKeyByValueSafe<T extends Record<string, string | number>>(
  enumObj: T,
  value: T[keyof T]
): keyof T | undefined {
  return (Object.keys(enumObj) as Array<keyof T>).find(
    key => enumObj[key] === value
  );
}

// Example:
const statusKey = getEnumKeyByValueSafe(OrderStatus, 'DELIVERED'); // "Delivered"
console.log(statusKey); // Output: Delivered

In this tutorial, I explained how to get the Enum Key by Value in TypeScript using different methods. Whether you’re dealing with string or numeric enums, you can confidently map values back to their keys using the approaches above. Do let me know in the comments below if it helps.

You may also like the following tutorials:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App