Skip to content

Commit 7d43821

Browse files
committed
feat: support enum item literals for array initialization format
1 parent bb098d0 commit 7d43821

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
# enum-plus Changelog
44

5+
## 2.3.2
6+
7+
2025-6-10
8+
9+
### Features
10+
11+
- ✨ Improve type IntelliSense for array initialization Enums, allowing enum items to be listed as literals.
12+
513
## 2.3.1
614

715
2025-6-7

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.3.1",
3+
"version": "2.3.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: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { EnumCollectionClass, EnumExtensionClass } from './enum-collection';
22
import type {
3+
ArrayToMap,
34
EnumInit,
45
EnumInitOptions,
56
EnumKey,
67
EnumValue,
78
IEnum,
89
LabelOnlyEnumItemInit,
9-
StandardEnumInit,
1010
ValueTypeFromSingleInit,
1111
} from './types';
1212
import { defaultLocalize } from './utils';
@@ -43,22 +43,20 @@ export function Enum<
4343
* const Week = Enum([
4444
* { value: 0, label: 'Sunday', key: 'Sun' },
4545
* { value: 1, label: 'Monday', key: 'Mon' },
46-
* ]);
46+
* ] as const);
4747
*
4848
* @param init Init objects array | 初始化对象数组
4949
* @param options Generate options | 生成选项
5050
*
5151
* @returns Enum collection | 枚举集合
5252
*/
5353
export function Enum<
54-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
55-
T extends Record<string, any>,
56-
K extends EnumKey<T> = EnumKey<T>,
57-
V extends EnumValue = ValueTypeFromSingleInit<T[K], K>,
58-
>(
59-
init: T[],
60-
options?: EnumInitOptions<T, K, V>
61-
): IEnum<StandardEnumInit<string, V>, string, V> & EnumExtension<T, K, V>;
54+
A extends Record<string, unknown>[] | readonly Record<string, unknown>[],
55+
K extends EnumKey<ArrayToMap<A>> = EnumKey<ArrayToMap<A>>,
56+
// @ts-expect-error: because no constraint on items of A, so ValueTypeFromSingleInit<ArrayToMap<A>[K], K> does not satisfy EnumValue
57+
V extends EnumValue = ValueTypeFromSingleInit<ArrayToMap<A>[K], K>,
58+
// @ts-expect-error: because no constraint on items of A, so ArrayToMap<A> does not satisfy EnumInit<K, V>
59+
>(init: A, options?: EnumInitOptions<A[number], K, V>): IEnum<ArrayToMap<A>, K, V> & EnumExtension<ArrayToMap<A>, K, V>;
6260
export function Enum<
6361
T extends EnumInit<K, V>,
6462
K extends EnumKey<T> = EnumKey<T>,

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export type {
1414
MenuItemOption,
1515
ColumnFilterItem,
1616
EnumInitOptions,
17+
FindEnumKeyByValue,
18+
ArrayToMap,
1719
} from './types';
1820
export type { EnumItemClass } from './enum-item';
1921

src/types.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,14 @@ export type ValueTypeFromEnumInit<T, K extends EnumKey<T> = EnumKey<T>> =
559559
? K
560560
: K; // Unknown format, use key as value
561561

562+
/**
563+
* **EN:** Find the key of the enumeration item by value
564+
*
565+
* **CN:** 通过值查找枚举项的key
566+
*
567+
* @template T Enum collection initialization data type | 枚举集合初始化数据的类型
568+
* @template V Enum value type | 枚举值的类型
569+
*/
562570
export type FindEnumKeyByValue<T, V extends EnumValue> = {
563571
// ValueOnly { foo:1, bar:2 }
564572
[K in keyof T]: T[K] extends V
@@ -579,3 +587,16 @@ export type FindEnumKeyByValue<T, V extends EnumValue> = {
579587
: never
580588
: never;
581589
}[keyof T];
590+
591+
/**
592+
* **EN:** Convert an array of objects to a Map-like object, where the key is the `key` field of the
593+
* object, and the value is the `value` field of the object
594+
*
595+
* **CN:** 将对象数组转换为类似Map的对象,其中key为对象的`key`字段,value为对象的`value`字段
596+
*
597+
* @template A Array type | 数组类型
598+
*/
599+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
600+
export type ArrayToMap<A extends Record<string, any>[] | readonly Record<string, any>[]> = {
601+
[K in A[number]['key']]: Extract<A[number], { key?: K }>;
602+
};

0 commit comments

Comments
 (0)