What’s New in enum-plus v3.0? Simplify Enum Initialization and Explore the New Plugin System
enum-plus v3.0 introduces a milestone set of features—including removal of the as const assertion, new enum.named and enum.meta properties, revamped enum.values behavior, additional helper methods, a global configuration API, and a modular plugin ecosystem—while also detailing breaking changes and bug fixes for developers.
New Features
Enum initialization no longer requires the as const assertion.
// before
const WeekEnum = Enum({
Monday: { value: 1, label: '星期一' },
Tuesday: { value: 2, label: '星期二' },
} as const);
// now
const WeekEnum = Enum({
Monday: { value: 1, label: '星期一' },
Tuesday: { value: 2, label: '星期二' },
});Added enum.named to aggregate all enum items for direct access, e.g., WeekEnum.named.Monday.
// before
const monday = WeekEnum.items.find(item => item.value === 1);
// now
const monday = WeekEnum.named.Monday;Introduced enum.meta to expose custom fields (e.g., hex) without iteration.
const ColorEnum = Enum({
Red: { value: 1, label: 'Red', hex: '#FF0000' },
Green: { value: 2, label: 'Green', hex: '#00FF00' },
Blue: { value: 3, label: 'Blue', hex: '#0000FF' },
});
ColorEnum.meta.hex; // ['#FF0000', '#00FF00', '#0000FF']Changed enum.values to return an array of raw values; the previous behavior is now available via enum.items.
WeekEnum.values; // [1, 2, 3, 4, 5, 6, 7]Added enum.labels to return a read‑only array of labels.
WeekEnum.labels; // ['星期一', '星期二', ...]New enum.toList method replaces toSelect, toMenu and toFilter.
WeekEnum.toList();
// [ { value: 1, label: '星期一' }, { value: 2, label: '星期二' }, ... ]
WeekEnum.toList({ valueField: 'id', labelField: 'name' });
// [ { id: 1, name: '星期一' }, { id: 2, name: '星期二' }, ... ]New enum.toMap method replaces enum.toValueMap.
WeekEnum.toMap();
// { "1": '星期一', "2": '星期二', ... }
WeekEnum.toMap({ keySelector: 'key', valueSelector: 'value' });
// { "Monday": 1, "Tuesday": 2, ... }Added Enum.isEnum to check if an object is an Enum instance.
Enum.isEnum(WeekEnum); // trueAdded Enum.install for plugin installation.
Enum.install(plugin);Global Configuration
Use Enum.config to manage global settings. Enum.config.autoLabel toggles automatic label generation based on a prefix. options.labelPrefix defines a custom prefix when creating enums. options.autoLabel enables/disables auto‑label generation per enum, overriding the global setting.
Extended EnumItemLabel type for better IDE IntelliSense.
Plugin System
New modular plugin architecture; each feature can be added via an independent npm package. @enum-plus/plugin-antd: Ant Design helpers ( enum.toSelect, enum.toMenu, enum.toFilter, enum.toValueMap). @enum-plus/plugin-i18next: i18next localization support. @enum-plus/plugin-react-i18next: Automatic react‑i18next integration. @enum-plus/plugin-react: React bindings, including Enum.localize component. @enum-plus/plugin-i18next-vue and @enum-plus/plugin-vue-i18n: Vue i18n integration.
Breaking Changes
enum.valuesnow returns raw values; use enum.items for the previous output.
Renamed internal symbols for clarity: ENUM_COLLECTION →
IS_ENUM ENUM_ITEM→
IS_ENUM_ITEM ENUM_ITEMS→ IS_ENUM_ITEMS Methods enum.toSelect, enum.toMenu, enum.toFilter, and enum.toValueMap have been moved to the @enum-plus/plugin-antd package.
Deprecated properties enum.options, enum.menus, enum.filters, and enum.valuesEnum have been removed.
Bug Fixes
Fixed an issue where source‑map files in the lib directory could not be parsed.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
