解决 TypeScript 下枚举值无法为对象,以及保证枚举值有序性的一些应用场景,并提供了完整的 TypeScript 类型映射。
$ npm i ts-enum-object --save
-
createEnumObject(items: Array<{ name: string; value: any; label?: string; ...other }>)
创建一个枚举对象,name
与value
为必填,name
代表枚举对象的 key,其他字段可以按业务需求扩展。TypeScript 下入参的数组【务必】【务必】【务必】加
as const
,否则无法正常推导出 Key 的类型import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.A // 1 TestEnum.B // 2 TestEnum.C // 3
-
.keys()
获取枚举所有name
。import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.keys() // ['A', 'B', 'C']
-
.values()
获取枚举所有value
。import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.values() // [1, 2, 3]
-
.items()
获取枚举列表,也就是 createEnumObject 的入参。import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.items() // [ // { name: 'A', value: 1, label: 'AA', }, // { name: 'B', value: 2, label: 'BB', }, // { name: 'C', value: 3, label: 'CC', }, // ]
-
.getItemBy(key, valueOfKey)
根据枚举配置对象中某个字段的名字及其值,获取对应的枚举配置项(如果会出现重复,只返回第一个)。import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.getItemBy('label', 'AA') // { name: 'A', value: 1, label: 'AA', }
-
.getItemByName(valueOfName)
根据枚举配置对象中字段name
的值,获取对应的枚举配置项。import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.getItemByName('A') // { name: 'A', value: 1, label: 'AA', }
-
.getItemByValue(valueOfValue)
根据枚举配置对象中字段value
的值,获取对应的枚举配置项。import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.getItemByValue(1) // { name: 'A', value: 1, label: 'AA', }
-
.getLabel(valueOfNameOrValue)
根据枚举配置对象中字段name
或者value
的值,获取对应的label
。import { createEnumObject } from 'ts-enum-object'; const TestEnum = createEnumObject([ { name: 'A', value: 1, label: 'AA', }, { name: 'B', value: 2, label: 'BB', }, { name: 'C', value: 3, label: 'CC', }, ] as const); // as const is required TestEnum.getLabel('A') // 'AA' TestEnum.getLabel(1) // 'AA'
-
EnumObjectNamesType<EnumObjectType>
Typescript 类型方法,获取枚举对象的所有 Name 类型。type Names = EnumObjectNamesType<typeof TestEnum>; // A | B | C
-
EnumObjectValuesType<EnumObjectType>
Typescript 类型方法,获取枚举对象的所有 Name 类型。type Values = EnumObjectValuesType<typeof TestEnum>; // 1 | 2 | 3
-
EnumObjectFieldValueType<EnumObjectType, FiledNameType>
Typescript 类型方法,获取枚举对象的所有 Name 类型。type Names = EnumObjectFieldValueType<typeof TestEnum, 'name'>; // A | B | C