| hide_title | true |
|---|---|
| custom_edit_url | |
| pagination_prev | |
| pagination_next |
Home > @rushstack/node-core-library > Enum
A helper for looking up TypeScript enum keys/values.
Signature:
export declare class Enum TypeScript enums implement a lookup table for mapping between their keys and values:
enum Colors {
Red = 1
}
// Prints "Red"
console.log(Colors[1]);
// Prints "1"
console.log(Colors["Red]);However the compiler's "noImplicitAny" validation has trouble with these mappings, because there are so many possible types for the map elements:
function f(s: string): Colors | undefined {
// (TS 7015) Element implicitly has an 'any' type because
// index expression is not of type 'number'.
return Colors[s];
}The Enum helper provides a more specific, strongly typed way to access members:
function f(s: string): Colors | undefined {
return Enum.tryGetValueByKey(Colors, s);
}|
Method |
Modifiers |
Description |
|---|---|---|
|
|
This API is similar to Enum.tryGetKeyByNumber()<></>, except that it throws an exception if the key is undefined. | |
|
|
This API is similar to Enum.tryGetValueByKey()<></>, except that it throws an exception if the key is undefined. | |
|
|
Returns an enum string key, given its numeric value. Returns | |
|
|
Returns an enum value, given its key. Returns |