@gradecam/tsenum

中文文档
1.2.0发布于 2019/08/21ISCTypeScriptCommonJS零依赖近 30 天下载 1.1万

Super simple typescript library for string-compatible enums

原始使用文档

Description

Typescript supports enum now, but there are times when you may want a little more flexibility than built-in enums give you; this package was adapted from an example in a gist linked from a typescript issue which I cannot now find, but suffice it to say that though we have modified it a bit the idea is not original to us.

Installing

npm install --save @gradecam/tsenum

Basic Usage

import {MakeEnum, TypeFromEnum} from 'tsenum';

const Colors = MakeEnum({
    Red: 'red',
    Blue: 'blue',
    Green: 'green',
    Violet: 'violet',
    Black: 'black'
});
type Colors = TypeFromEnum<typeof Colors>;

// type Colors = 'red' | 'blue' | 'green' | 'violet' | 'black'
// value Colors is a frozen object with the keys expected
// typeof Colors.Red is 'red', et al

carColor: Colors = 'red'; // valid
carColor = Colors.Green; // valid
carColor = 'yellow'; // typescript error, not a valid color

Combining types

MakeEnum will merge multiple enum objects into one (up to 9), allowing you to combine types.

import {MakeEnum, TypeFromEnum} from 'tsenum';

const PrimaryColors = MakeEnum({
    Red: 'red',
    Blue: 'blue',
    Green: 'green',
});
type PrimaryColors = TypeFromEnum<typeof PrimaryColors>;

const SecondaryColors = MakeEnum({
    Yellow: 'yellow',
    Cyan: 'cyan',
    Magenta: 'magenta'
});
type SecondaryColors = TypeFromEnum<typeof SecondaryColors>;

const AllColors = MakeEnum(PrimaryColors, SecondaryColors);
type AllColors = TypeFromEnum<typeof AllColors>;
// type AllColors = 'red' | 'blue' | 'green' | 'yellow' | 'cyan' | 'magenta'

Getting an array of possible values

Sometimes you may want an array of possible values, such as when defining an enum type in a mongoose schema. Since the enum is an object, you can use Object.values to get that:

import {MakeEnum, TypeFromEnum} from 'tsenum';

const PrimaryColors = MakeEnum({
    Red: 'red',
    Blue: 'blue',
    Green: 'green',
});
type PrimaryColors = TypeFromEnum<typeof PrimaryColors>;

const PrimaryColorList = Object.values(PrimaryColors); // ['red', 'blue', 'green']
// typeof PrimaryColorList = Array<'red'|'blue'|'green'>

Allowed value types

Currently you can use any string, number, or boolean as a value

版本与发布记录

@gradecam/tsenum 共发布 3 个版本,首次发布于 2019/08/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2019/08/217 年前
1.1.02019/08/217 年前
1.0.02019/08/157 年前

关于 @gradecam/tsenum 的常见问题

@gradecam/tsenum 是什么?

Super simple typescript library for string-compatible enums

如何安装 @gradecam/tsenum?

在项目目录运行 npm install @gradecam/tsenum,或使用 pnpm add / yarn add / bun add。

@gradecam/tsenum 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gradecam/tsenum 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gradecam/tsenum 可以商用吗?

@gradecam/tsenum 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gradecam/tsenum 的下载量有多少?

根据 npm 官方下载统计,@gradecam/tsenum 在 2026/10/17 – 2026/11/15 这 30 天内被下载 11,457 次,日均约 382 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gradecam/tsenum 还在维护吗?

最新版本 1.2.0 发布于 2019/08/21(7 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。