Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/node-core-library > Enum > tryGetValueByKey

Enum.tryGetValueByKey() method

Returns an enum value, given its key. Returns undefined if no matching key is found.

Signature:

static tryGetValueByKey<TEnumValue>(enumObject: {
[key: string]: TEnumValue | string;
[key: number]: TEnumValue | string;
}, key: string): TEnumValue | undefined;

Parameters

ParameterTypeDescription
enumObject{ [key: string]: TEnumValue | string; [key: number]: TEnumValue | string; }
keystring

Returns:

TEnumValue | undefined

Example

Example usage:

enum Colors {
Red = 1
}

// Prints "1"
console.log(Enum.tryGetValueByKey(Colors, "Red"));

// Prints "undefined"
console.log(Enum.tryGetValueByKey(Colors, "Black"));