diff --git a/src/types/utils.d.ts b/src/types/utils.d.ts index e306d02..bd16325 100644 --- a/src/types/utils.d.ts +++ b/src/types/utils.d.ts @@ -3,3 +3,12 @@ import type { ComputedRef, Ref } from 'vue' export type DynamicProps = { [P in keyof T]: Ref | T[P] | ComputedRef } + +type StringLiteralsToType + = T extends 'string' + ? string + : T extends 'number' + ? number + : T extends 'boolean' + ? boolean + : never diff --git a/src/utils/dict.ts b/src/utils/dict.ts index c4c2895..0790ca7 100644 --- a/src/utils/dict.ts +++ b/src/utils/dict.ts @@ -2,6 +2,7 @@ * 数据字典工具类 */ import { useDictStoreWithOut } from '@/store/modules/dict' +import type { StringLiteralsToType } from '@/types/utils' const dictStore = useDictStoreWithOut() @@ -11,10 +12,10 @@ const dictStore = useDictStoreWithOut() * @param dictType 数据类型 * @returns {*|Array} 数据字典数组 */ -export interface DictDataType { +export interface DictDataType { dictType: string label: string - value: string | number | boolean + value: T key?: any colorType: string cssClass: string @@ -39,8 +40,8 @@ export function getDictOpts(dictType: string) { return getDictDatas(dictType) } -export function getDictOptions(dictType: string, valueType?: 'string' | 'number' | 'boolean'): any { - const dictOption: DictDataType[] = [] +export function getDictOptions(dictType: string, valueType?: T) { + const dictOption: DictDataType>[] = [] const dictOptions: DictDataType[] = getDictDatas(dictType) if (dictOptions && dictOptions.length > 0) { dictOptions.forEach((dict: DictDataType) => { @@ -53,7 +54,7 @@ export function getDictOptions(dictType: string, valueType?: 'string' | 'number' : valueType === 'boolean' ? `${dict.value}` === 'true' : Number.parseInt(`${dict.value}`), - }) + } as DictDataType>) }) } return dictOption