| hide_title | true |
|---|---|
| custom_edit_url | |
| pagination_prev | |
| pagination_next |
Home > @rushstack/node-core-library > Enum > tryGetKeyByNumber
Returns an enum string key, given its numeric value. Returns undefined if no matching value is found.
Signature:
static tryGetKeyByNumber<TEnumValue, TEnumObject extends {
[key: string]: TEnumValue;
}>(enumObject: TEnumObject, value: number): keyof typeof enumObject | undefined;|
Parameter |
Type |
Description |
|---|---|---|
|
enumObject |
TEnumObject | |
|
value |
number |
Returns:
keyof typeof enumObject | undefined
The TypeScript compiler only creates a reverse mapping for enum members whose value is numeric. For example:
enum E {
A = 1,
B = 'c'
}
// Prints "A"
console.log(E[1]);
// Prints "undefined"
console.log(E["c"]);Example usage:
enum Colors {
Red = 1,
Blue = 'blue'
}
// Prints "Red"
console.log(Enum.tryGetKeyByNumber(Colors, 1));
// Prints "undefined"
console.log(Enum.tryGetKeyByNumber(Colors, -1));