# @dominicstop/ts-event-emitter 中文文档

规范地址：https://npmcn.com/package/%40dominicstop/ts-event-emitter/

版本：1\.1\.0
npm 元数据更新时间：2022-04-05T05:14:06.443Z
中文译文更新时间：2026-11-12T15:33:34.927Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40dominicstop/ts-event-emitter
原文对照：https://npmcn.com/package/%40dominicstop/ts-event-emitter/?lang=original
项目主页：https://github.com/dominicstop/ts-event-emitter#readme
源码仓库：https://github.com/dominicstop/ts-event-emitter

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：27.4 kB，17 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2021-08-15T15:44:32.610Z
- 最新版本发布：2021-08-24T02:45:15.415Z
- 近 30 天下载量：769508（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：12（近 12 个月 0 个）

## 中文简介

还有一个事件发送器用打字符写成 .

## 安装

```sh
npm install @dominicstop/ts-event-emitter
```

也可以使用：`pnpm add @dominicstop/ts-event-emitter`、`yarn add @dominicstop/ts-event-emitter`、`bun add @dominicstop/ts-event-emitter`

## README

# `ts-event-emitter`

用打字符写出的事件发送器 qq

  

## 安装

```sh
# install via npm...
npm install @dominicstop/ts-event-emitter

# or install via yarn.
yarn add @dominicstop/ts-event-emitter
```

  

## 使用量

### 基本用途

```typescript
type EventKeys = 'Foo' | 'Baz';

export const emitter: TSEventEmitter<EventKeys, {
  Foo: string;
  Baz: { age: number } | null;
}> = new TSEventEmitter();

emitter.once('Foo', (event) => { console.log(event) });
emitter.emit('Foo', 'hello world');

emitter.once('Baz', (event) => { console.log(event?.age) });
emitter.emit('Baz', { age: 23 });
```

### 深度使用

```typescript
// 1. Create a string enum...
enum Events { Foo = 'Foo', Bar = 'Bar', Baz = 'Baz' };

// 1.1. ...or alt., a union of strings (whichever you prefer).
type EventKeys = 'Foo' | 'Bar' | 'Baz';

// 2. Create the emitter + event map...
// * For each key in the event enum, define the type of the 
//    `event` argument that the listener will receive.
// * As mentioned in 1.1. you can also use the `EventKeys` union 
//   in place of the `Events` enum.
export const emitter: TSEventEmitter<EventKeys, {

  // 2.1. For the event `TestEnum.Foo`, this will be its event object.
  Foo: { name: string },

  // 2.2. If you don't want to pass an event argument, 
  //      then set it to `null` or `undefined`.
  // * This means the event listener won't receive any arguments
  //   and you can't pass a data argument to `emit` (see step 4.2.)
  Bar: null, 

  // 2.3. or alt., you can make the event param "optional".
  Baz: null | { age: number }
}> = new TSEventEmitter();

// 3.1. `event` arg. will be inferred as `(event: { name: string }) => void`
emitter.once('Foo', (event) => { console.log(event.name) });

// 3.2.`event` arg. will be inferred as `() => void`
// * Note: If you try to add an event param, TS produces an error.
emitter.once('Bar', () => { /** no-op */ });

// 3.3. `event` will be inferred as 
//      `(event: { age: number } | null) => void`.
// * As such, we need to use the optional chaining operator to access `event`.
emitter.once(Events.Baz, (event) => { console.log(event?.age) });
   
// 4. Next, lets broadcast some events.
emitter.emit(Events.Foo, { name: 'd'});
emitter.emit('Baz', { age: 1 });
 
// 4.1. Note: You can't pass a data argument with the 'Bar' event
// because in step 2.2., the event object is defined as `null`.
emitter.emit('Bar');
 
// 4.2. We must explicitly pass a null/undefined value for the data
// argument since it can be optional (e.g. step 2.3.)
emitter.emit('Baz', null);
```

## 文档

### `TSEvenEmitter`

| 财产/方法 | 说明 |
| --- | --- |
| 🔤 `addListener`  
  
⚛️ `(eventKey, listener) => { unsubscribe: () => void }` |  |
| 🔤 `removeListener`  
  
⚛️ `(eventKey, listenerToRemove) => void` |  |
| 🔤 `once`  
  
⚛️ `(eventKey, listener) => void` |  |
| 🔤 `removeAllListeners`  
  
⚛️ `() => void` |  |
| 🔤 `emit`  
  
⚛️ `(eventKey, data) => void` |  |

## 常见问题

### @dominicstop/ts-event-emitter 是什么？

还有一个事件发送器用打字符写成 \.

### 如何安装 @dominicstop/ts-event-emitter？

在项目目录运行 npm install @dominicstop/ts-event-emitter，或使用 pnpm add / yarn add / bun add。

### @dominicstop/ts-event-emitter 支持 TypeScript 吗？

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

### @dominicstop/ts-event-emitter 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @dominicstop/ts-event-emitter 可以商用吗？

@dominicstop/ts-event-emitter 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @dominicstop/ts-event-emitter 的下载量有多少？

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

### @dominicstop/ts-event-emitter 还在维护吗？

最新版本 1\.1\.0 发布于 2021/08/24（5 年前）；至今共发布 12 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/08/15。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

