@philiprehberger/ts-enum
v0.1.3
Published
Rich enums with methods labels and serialization
Downloads
56
Readme
@philiprehberger/ts-enum
Rich enums with methods, labels, and serialization.
Installation
npm install @philiprehberger/ts-enumUsage
import { defineEnum, type EnumValue } from '@philiprehberger/ts-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)Simple Enums
const Color = defineEnum({
RED: 'red',
GREEN: 'green',
BLUE: 'blue',
});
Color.RED; // 'red'
Color.values(); // ['red', 'green', 'blue']API
| 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 |
Development
npm install
npm run build
npm testLicense
MIT
