Return the name of the key that matches the given Enum value. Can be used to emulate Typescript reverse mapping for const objects or string enums.
const object
The NormalEnum to check
NormalEnum
The value to get the key of
The name of the key with the specified value
const thing = { one: 1, two: 2,} as const;console.log(enumValueToKey(thing, 2)); // output: "two" Copy
const thing = { one: 1, two: 2,} as const;console.log(enumValueToKey(thing, 2)); // output: "two"
Error if an invalid enum value is passed to the function
If multiple keys map to the same value, the first one (in insertion order) will be retrieved, but the return type will be the union of ALL their corresponding keys.
Return the name of the key that matches the given Enum value. Can be used to emulate Typescript reverse mapping for
const object
s or string enums.