Rich enums with methods labels and serialization
npm install @philiprehberger/enumRich enums with methods, labels, and serialization
npm install @philiprehberger/enum
import { defineEnum, type EnumValue } from '@philiprehberger/enum';
const Status = defineEnum({
ACTIVE: { value: 'active', label: 'Active' },
INACTIVE: { value: 'inactive', label: 'Inactive' },
});
type Status = EnumValue<typeof Status>; // 'active' | 'inactive'
Status.ACTIVE; // 'active' (literal type)
Status.label('active'); // 'Active'
Status.options(); // [{ value: 'active', label: 'Active' }, ...]
Status.parse('active'); // { ok: true, value: 'active' }
Status.is('active'); // true (type guard)
const Color = defineEnum({
RED: 'red',
GREEN: 'green',
BLUE: 'blue',
});
Color.RED; // 'red'
Color.values(); // ['red', 'green', 'blue']
| Method | Description |
|---|---|
defineEnum(definition) | Create a frozen enum with methods |
.values() | All enum values |
.keys() | All enum keys |
.entries() | Key-value pairs |
.parse(input) | Safe parse with result type |
.label(value) | Get display label |
.is(value) | Type guard |
.options() | { value, label }[] for UI selects |
.meta(value) | Get metadata for a value |
npm install
npm run build
npm test
If you find this project useful: