Skip to content

Commit 54f6874

Browse files
committed
feat: enhance raw method typing in enums, bump to 2.2.10
1 parent 792e30a commit 54f6874

File tree

5 files changed

+72
-39
lines changed

5 files changed

+72
-39
lines changed

CHANGELOG.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22

33
# enum-plus Changelog
44

5-
## Unreleased
5+
## 2.2.10
6+
7+
2025-5-14
68

79
### Features
810

9-
-
11+
- ✨ Enhance the type inference of the `raw` method. When the input value matches a specific enum item, it only returns the raw object of that enum item instead of returning the raw objects of all enum items.
12+
13+
### Bug Fixes
14+
15+
- 🐞 The output `lib` directory structure is incorrect. This will break legacy Node.js applications using the `CommonJS` module spec, while modern Node.js applications using the `NodeNext` module spec remain unaffected. This issue was introduced in `v2.2.7`. For node.js applications, it's strongly recommended to upgrade.
1016

1117
## 2.2.9
1218

1319
2025-4-20
1420

1521
### Features
1622

17-
- 🛠 emit a warning while trying to modify an `EnumItem`
23+
- 🛠 Emit a warning while trying to modify an `EnumItem`
1824

1925
### Bug Fixes
2026

@@ -26,7 +32,7 @@
2632

2733
### Bug Fixes
2834

29-
- 🐞 fix `exports` configuration for moduleResolution `ESNext`
35+
- 🐞 Fix `exports` configuration for moduleResolution `ESNext`
3036

3137
## 2.2.7
3238

@@ -62,7 +68,7 @@
6268

6369
### Features
6470

65-
- 🛠 supports global type extension for enum localization keys, providing better editor intelligence awareness
71+
- 🛠 Support global type extension for enum localization keys, providing better editor intelligence awareness
6672

6773
## 2.2.3
6874

@@ -78,97 +84,97 @@
7884

7985
### Features
8086

81-
- 🛠 change the return type of `Enum.options.getKey`
82-
- 📖 update docs
87+
- 🛠 Change the return type of `Enum.options.getKey`
88+
- 📖 Update docs
8389

8490
## 2.2.1
8591

8692
2025-2-10
8793

88-
- 📖 update docs
94+
- 📖 Update docs
8995

9096
## 2.2.0
9197

9298
2025-2-9
9399

94100
### Features
95101

96-
- 🔥 support initializing Enum with native enums
97-
- 🔥 adds `items` array, deprecated the _`values`_ array. Reduces package size.
102+
- 🔥 Support initializing Enum with native enums
103+
- 🔥 Add `items` array, deprecated the _`values`_ array. Reduces package size.
98104

99105
## 2.1.1
100106

101107
2025-2-8
102108

103109
### Bug Fixes
104110

105-
- 🐞 fix typing error in `EnumExtension` interface
111+
- 🐞 Fix typing error in `EnumExtension` interface
106112

107113
## 2.1.0
108114

109115
2025-2-8
110116

111-
> **Note:** this version has a typing issue, please use `v2.1.1`
117+
> **Note:** This version has a typing issue, please use `v2.1.1`
112118
113119
### Features
114120

115-
- 🔥 add `toSelect` method, deprecated the _`options`_ method
116-
- 🔥 add `toMenu` method, deprecated the _`menus`_ method
117-
- 🔥 add `toFilter` method, deprecated the _`filters`_ method
118-
- 🔥 add `toValueMap` method, the _`valuesEnum`_ method
119-
- 🔥 support global extension, custom methods can be added to Enums
121+
- 🔥 Add `toSelect` method, deprecated the _`options`_ method
122+
- 🔥 Add `toMenu` method, deprecated the _`menus`_ method
123+
- 🔥 Add `toFilter` method, deprecated the _`filters`_ method
124+
- 🔥 Add `toValueMap` method, the _`valuesEnum`_ method
125+
- 🔥 Support global extension, custom methods can be added to Enums
120126

121127
## 2.0.3
122128

123129
2025-2-6
124130

125131
### Features
126132

127-
- 🛠 downgrade EcmaScript version to `ES2015`
133+
- 🛠 Downgrade EcmaScript version to `ES2015`
128134

129135
## 2.0.2
130136

131137
2025-2-3
132138

133139
### Features
134140

135-
- 🔥 the `firstOption` of `options` method supports localization
141+
- 🔥 The `firstOption` of `options` method supports localization
136142

137143
## 2.0.1
138144

139145
2025-2-2
140146

141147
### Features
142148

143-
- 🔥 support for enum text localization
149+
- 🔥 Support for enum text localization
144150
- the `Enum` method now accepts a `localize` option that can be used to localize the enum text
145151
- you can also set the `Enum.localize` static method to localize all enums in a lower priority
146-
- 🔥 add `menus` method
152+
- 🔥 Add `menus` method
147153

148154
### Breaking Changes
149155

150-
- 💣 all parameters after the first of the `Enum` method has been changed to an `options` object
156+
- 💣 All parameters after the first of the `Enum` method has been changed to an `options` object
151157

152158
## 1.0.3
153159

154160
2023-11-14
155161

156-
- 🛠 if an enum is created from array, both the `enum.raw` or `enumItem.raw` methods return the original array item
162+
- 🛠 Both the `enum.raw` or `enumItem.raw` methods return the original array item, if an enum is created from array,
157163

158164
## 1.0.2
159165

160166
2023-07-31
161167

162-
- 🆕 added `rawType` accessor
168+
- 🆕 Add `rawType` accessor
163169

164170
## 1.0.1
165171

166172
2023-03-09
167173

168-
- 🆕 added a new `filters` method that returns an array of enumerated items that can be passed directly to the `Column.filters` of the AntDesign Table component as a list of filtered items for the column
174+
- 🆕 Add a new `filters` method that returns an array of enumerated items that can be passed directly to the `Column.filters` of the AntDesign Table component as a list of filtered items for the column
169175

170176
- ## 1.0.0
171177

172178
2022-10-08
173179

174-
- 🆕 the first release of `enum-plus` library
180+
- 🆕 The first release of `enum-plus` library

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": "2.2.9",
3+
"version": "2.2.10",
44
"description": "A drop-in replacement for native enum. Like native enum but much better!",
55
"keywords": [
66
"enum",

src/enum-collection.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
EnumItemOptions,
1010
EnumKey,
1111
EnumValue,
12+
FindEnumKeyByValue,
1213
IEnumValues,
1314
MenuItemOption,
1415
ObjectFirstOptionConfig,
@@ -157,11 +158,13 @@ export class EnumCollectionClass<
157158
}
158159

159160
raw(): T;
160-
raw(keyOrValue: V | K): T[K];
161-
raw(value: unknown): T[K] | undefined;
162-
raw(value?: unknown): T | T[K] | undefined {
163-
if (value !== undefined) {
164-
return this.items.raw(value);
161+
// eslint-disable-next-line @typescript-eslint/ban-types
162+
raw<IK extends V | K | Exclude<EnumValue, string> | (string & {})>(
163+
keyOrValue: IK
164+
): IK extends K ? T[IK] : IK extends V ? T[FindEnumKeyByValue<T, IK>] : T[K] | undefined;
165+
raw<IK extends EnumValue>(value?: IK | unknown): T | T[K] | T[FindEnumKeyByValue<T, IK>] | undefined {
166+
if (value != null) {
167+
return this.items.raw(value as keyof T | EnumValue) as T[K];
165168
} else {
166169
return this.items.raw();
167170
}

src/enum-values.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
EnumItemOptions,
1010
EnumKey,
1111
EnumValue,
12+
FindEnumKeyByValue,
1213
IEnumValues,
1314
MenuItemOption,
1415
ObjectFirstOptionConfig,
@@ -155,9 +156,11 @@ export class EnumValuesArray<
155156
}
156157

157158
raw(): T;
158-
raw(keyOrValue: V | K): T[K];
159-
raw(value: unknown): T[K] | undefined;
160-
raw(value?: V | K): T | T[K] | undefined {
159+
// eslint-disable-next-line @typescript-eslint/ban-types
160+
raw<IK extends V | K | Exclude<EnumValue, string> | (string & {})>(
161+
keyOrValue: IK
162+
): IK extends K ? T[IK] : IK extends V ? T[FindEnumKeyByValue<T, IK>] : T[K] | undefined;
163+
raw<IK extends EnumValue>(value?: IK | unknown): T | T[K] | T[FindEnumKeyByValue<T, IK>] | undefined {
161164
if (value == null) {
162165
// Return the original initialization object
163166
return this.#raw;

src/types.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ export interface IEnumValues<
307307
*
308308
* @param keyOrValue Enum key or value | 枚举key或value
309309
*/
310-
// eslint-disable-next-line @typescript-eslint/method-signature-style
311-
raw(keyOrValue: V | K): T[K];
312-
// eslint-disable-next-line @typescript-eslint/method-signature-style
313-
raw(value: unknown): T[K] | undefined;
310+
// eslint-disable-next-line @typescript-eslint/ban-types
311+
raw<IK extends V | K | Exclude<EnumValue, string> | (string & {})>(
312+
keyOrValue: IK
313+
): IK extends K ? T[IK] : IK extends V ? T[FindEnumKeyByValue<T, IK>] : T[K] | undefined;
314314

315315
/**
316316
* **EN:** The data type of all enumeration values
@@ -553,3 +553,24 @@ export type ValueTypeFromEnumInit<T, K extends EnumKey<T> = EnumKey<T>> =
553553
: T extends OmitEnumInit<K> // format: {foo: undefined, bar: undefined}
554554
? K
555555
: K; // Unknown format, use key as value
556+
557+
export type FindEnumKeyByValue<T, V extends EnumValue> = {
558+
// ValueOnly { foo:1, bar:2 }
559+
[K in keyof T]: T[K] extends V
560+
? K
561+
: // Standard: { foo:{ value:1 }, bar:{ value:2 } }
562+
// @ts-expect-error: because need to force check T[K]['value'], event value field does not exist
563+
T[K]['value'] extends V
564+
? K
565+
: // LabelOnly: { foo:{ label: 'foo' }, bar:{ label: 'bar' } }
566+
object extends T[K]
567+
? K extends V
568+
? K
569+
: never
570+
: // KeyOnly: { foo: undefined, bar: undefined }
571+
undefined extends T[K]
572+
? K extends V
573+
? K
574+
: never
575+
: never;
576+
}[keyof T];

0 commit comments

Comments
 (0)