Enum Helper

v1.0.0

Static utility for reading values and key/value options from TypeScript enums.

getEnumValue

Resolve an enum value by its own key. Unknown and inherited keys throw an error.

import { EnumHelper } from '@oardi/ts-utils';

enum Status {
	Draft,
	Published,
}

EnumHelper.getEnumValue(Status, 'Published');
// 1

getKeyValues

Convert a numeric enum into IKeyValue<string>[], which is useful for select options:

EnumHelper.getKeyValues(Status);
// [
//   { key: 0, value: 'Draft' },
//   { key: 1, value: 'Published' },
// ]

getKeyValues() relies on the reverse mapping generated for numeric TypeScript enums; string enums do not provide that mapping.

Previous:Helpers

← Download Helper

Next:Helpers

File Helper →