Skip to content

Commit 3706a0b

Browse files
committed
feat: add const modifier to Enum function to simplify the enum initialization
Closed #6
1 parent ee29e78 commit 3706a0b

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
- 🔥 introduce an official plugin `enum-plus-plugin` that provides a collection of highly shareable plugins.
1515
- These plugins are designed to work in all scenarios and environments (including browser and Node.js), without introducing dependencies on third-party frameworks, component libraries, or platforms.
1616
- If you have a good idea that might be specific to a certain framework or platform rather than universally applicable, we recommend creating a new plugin project (e.g., `enum-plus-plugin-xxx`). We are happy to see that, and hope that you submit a PR to `enum-plus-plugin` to link your project back.
17+
- 🛠 Add `const` modifier to Enum function to simplify the enum initialization. Inline initializations no longer requires `as const`. Thanks to @otomad.
18+
19+
```diff
20+
import { Enum } from 'enum-plus';
21+
22+
const MyEnum = Enum({
23+
Foo: 1,
24+
Bar: 2,
25+
- } as const);
26+
+ });
27+
```
28+
1729
- ✨ Introduced **UMD module format** support, enabling direct browser usage without requiring a module bundler. Two variants are now available:
1830

1931
- `enum-plus.min.js` (ES2020) optimized for modern browsers.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "enum-plus",
3-
"version": "3.0.0-alpha.1",
3+
"version": "3.0.0-alpha.2",
44
"description": "A drop-in replacement for native enum. Like native enum but much better!",
55
"keywords": [
66
"enum",

src/enum.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { defaultLocalize } from './utils';
3131
* @returns Enum collection | 枚举集合
3232
*/
3333
export function Enum<
34-
T extends EnumInit<K, V>,
34+
const T extends EnumInit<K, V>,
3535
K extends EnumKey<T> = EnumKey<T>,
3636
V extends EnumValue = ValueTypeFromSingleInit<T[K], K>,
3737
>(init: T, options?: EnumInitOptions<T, K, V>): IEnum<T, K, V> & EnumExtension<T, K, V>;
@@ -53,15 +53,15 @@ export function Enum<
5353
*/
5454
export function Enum<
5555
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56-
T extends Record<string, any>,
56+
const T extends Record<string, any>,
5757
K extends EnumKey<T> = EnumKey<T>,
5858
V extends EnumValue = ValueTypeFromSingleInit<T[K], K>,
5959
>(
6060
init: T[],
6161
options?: EnumInitOptions<T, K, V>
6262
): IEnum<StandardEnumInit<string, V>, string, V> & EnumExtension<T, K, V>;
6363
export function Enum<
64-
T extends EnumInit<K, V>,
64+
const T extends EnumInit<K, V>,
6565
K extends EnumKey<T> = EnumKey<T>,
6666
V extends EnumValue = ValueTypeFromSingleInit<T[K], K>,
6767
>(init: T | T[], options?: EnumInitOptions<T, K, V>): IEnum<T, K, V> & EnumExtension<T, K, V> {

0 commit comments

Comments
 (0)