|
1 | 1 | import { EnumCollectionClass, EnumExtensionClass } from './enum-collection'; |
2 | 2 | import type { |
| 3 | + ArrayToMap, |
3 | 4 | EnumInit, |
4 | 5 | EnumInitOptions, |
5 | 6 | EnumKey, |
6 | 7 | EnumValue, |
7 | 8 | IEnum, |
8 | 9 | LabelOnlyEnumItemInit, |
9 | | - StandardEnumInit, |
10 | 10 | ValueTypeFromSingleInit, |
11 | 11 | } from './types'; |
12 | 12 | import { defaultLocalize } from './utils'; |
@@ -43,22 +43,20 @@ export function Enum< |
43 | 43 | * const Week = Enum([ |
44 | 44 | * { value: 0, label: 'Sunday', key: 'Sun' }, |
45 | 45 | * { value: 1, label: 'Monday', key: 'Mon' }, |
46 | | - * ]); |
| 46 | + * ] as const); |
47 | 47 | * |
48 | 48 | * @param init Init objects array | 初始化对象数组 |
49 | 49 | * @param options Generate options | 生成选项 |
50 | 50 | * |
51 | 51 | * @returns Enum collection | 枚举集合 |
52 | 52 | */ |
53 | 53 | 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>; |
62 | 60 | export function Enum< |
63 | 61 | T extends EnumInit<K, V>, |
64 | 62 | K extends EnumKey<T> = EnumKey<T>, |
|
0 commit comments