# @abraham/reflection 中文文档

规范地址：https://npmcn.com/package/%40abraham/reflection/

版本：0\.14\.0
npm 元数据更新时间：2026-11-02T05:17:03.479Z
中文译文更新时间：2026-11-05T18:08:41.778Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：69.4 kB，16 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=22
- 运行依赖：0 个
- 首次发布：2018-07-23T21:28:25.497Z
- 最新版本发布：2026-11-02T05:17:03.154Z
- 近 30 天下载量：105271（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：21（近 12 个月 1 个）

## 中文简介

反射元数据的轻量级ES模块

## 安装

```sh
npm install @abraham/reflection
```

也可以使用：`pnpm add @abraham/reflection`、`yarn add @abraham/reflection`、`bun add @abraham/reflection`

## README

[![Version Status](https://img.shields.io/npm/v/@abraham/reflection.svg?style=flat&label=version&colorB=4bc524)](https://npmjs.com/package/@abraham/reflection) [![Build Status](https://github.com/abraham/reflection/workflows/Index/badge.svg)](https://github.com/abraham/reflection/actions) [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@abraham/reflection.svg?style=flat&colorB=4bc524)](https://bundlephobia.com/result?p=@abraham/reflection)

# 反思

轻量级ES模块 [反射元数据](https://github.com/rbuckton/reflect-metadata/) 用于使用 TypeScript 的 [实验装饰器支持](https://www.typescriptlang.org/docs/handbook/decorators.html),加上匹配的 API [标准装饰器](https://github.com/tc39/proposal-decorators) 财务报告和审定财务报表 [装饰元数据](https://github.com/tc39/proposal-decorator-metadata)。 。 。 .

## 为什么?

建立这个图书馆的主要原因是提供更小的执行,可以作为一个模块列入.

-   ES模块
    -   `reflection` 可装入 `<script type="module" src="..."></script>`
-   大小( 未压缩)
    -   [`reflect-metadata`](https://github.com/rbuckton/reflect-metadata) 为~50K
    -   [`core-js/es7/reflect`](https://github.com/zloirock/core-js) 为~80K
    -   `@abraham/reflection` 为~3K

阅读如何 [从您的制作中减少 20K 角应用程序](https://bendyworks.com/blog/drop-20k-from-your-production-angular-app) 切换到这个.

## 安装

```sh
npm install @abraham/reflection
```

## 什么进口?

使用匹配您的工程编译器 。 项目使用一种或另一种,而不是两种.

|  | `@abraham/reflection/legacy` (默认) | `@abraham/reflection/standard` |
| --- | --- | --- |
| 装饰者 | 遗产 : `experimentalDecorators: true` | 标准: 类型脚本 5.2+ 无 `experimentalDecorators`,或 Babel 键 |
| 类型元数据 | `design:type`, (中文(简体) ), `design:paramtypes`, (中文(简体) ), `design:returntype` 与 `emitDecoratorMetadata` | 不详 |
| 参数装饰器 | 对 | 没有 |
| 全球 | 添加方法到 `Reflect` | 定义 `Symbol.metadata` 财务报告和审定财务报表 `Function.prototype[Symbol.metadata]` (单位:千美元)`null`如果缺少, 如 spec |
| 模块格式 | ES模块和UMD/CommonJS | ES模块 |

使用 `@abraham/reflection/legacy` 用于编码 `reflect-metadata`,包括依赖 `emitDecoratorMetadata` 用于依赖性注射。 赤裸裸的 `@abraham/reflection` 导入是同一个模块,因此现有的导入可以继续工作,两者可以混合.

使用 `@abraham/reflection/standard` 用于标准装饰器的新代码。 他们不会把课传给成员装饰师,所以 `Reflect.defineMetadata` 不能使用; 元数据通过装饰器 `context.metadata` 换句话说.

## 使用量

### 实验装饰器

`tsconfig.json`数字 :

```jsonc
{
  "compilerOptions": {
    "experimentalDecorators": true,
    // Only needed for design:type, design:paramtypes, and design:returntype
    "emitDecoratorMetadata": true,
    // Needed for the ./legacy subpath; the bare import resolves with any setting
    "moduleResolution": "bundler", // or "nodenext"
  },
}
```

```ts
import '@abraham/reflection/legacy'; // or '@abraham/reflection'
Reflect.defineMetadata(metadataKey, metadataValue, target);
```

您也可以导入 `Reflection`数字 :

```ts
import { Reflection as Reflect } from '@abraham/reflection/legacy';
Reflect.defineMetadata(metadataKey, metadataValue, target);
```

### 标准装饰器

需要 TypeScript 5.2 或更晚. `tsconfig.json`数字 :

```jsonc
{
  "compilerOptions": {
    // Leave experimentalDecorators and emitDecoratorMetadata unset or false
    // Any target except ESNext, which leaves @decorator syntax untransformed
    "target": "ES2022",
    // Needed to resolve the ./standard subpath export
    "moduleResolution": "bundler", // or "nodenext"
    // Only needed to type Symbol.metadata in your own code; add to your existing lib
    "lib": ["ES2022", "ESNext.Decorators"],
  },
}
```

在任何装饰类被定义之前导入, 所以 `Symbol.metadata` 当类创建时已存在.

```ts
import { metadata, getMetadata } from '@abraham/reflection/standard';

@metadata('role', 'admin')
class User {
  @metadata('column', 'email')
  email = '';

  @metadata('route', '/users')
  static list() {}
}

getMetadata('role', User); // 'admin'
getMetadata('column', User.prototype, 'email'); // 'email'
getMetadata('column', new User(), 'email'); // 'email'
getMetadata('route', User, 'list'); // '/users'
```

目标随之而来 `reflect-metadata`: 分类和静态成员元数据类, 以及原型或例如成员元数据. 子类继承了元数据,并且可以覆盖它. 价值是私藏的,所以阶级共享 `Symbol.metadata` 对象留给其他库.

## API 密码

### `@abraham/reflection/legacy`

现有方法如下:

```ts
Reflect.decorate(...);
Reflect.defineMetadata(...);
Reflect.deleteMetadata(...);
Reflect.getMetadata(...);
Reflect.getMetadataKeys(...);
Reflect.getOwnMetadata(...);
Reflect.getOwnMetadataKeys(...);
Reflect.hasMetadata(...);
Reflect.hasOwnMetadata(...);
Reflect.metadata(...);
```

### `@abraham/reflection/standard`

```ts
metadata(metadataKey, metadataValue); // decorator for classes, methods, getters, setters, fields, and accessors
getMetadata(metadataKey, target, memberKey?);
getMetadataKeys(target, memberKey?);
getOwnMetadata(metadataKey, target, memberKey?);
getOwnMetadataKeys(target, memberKey?);
hasMetadata(metadataKey, target, memberKey?);
hasOwnMetadata(metadataKey, target, memberKey?);
```

## 常见问题

### @abraham/reflection 是什么？

反射元数据的轻量级ES模块

### 如何安装 @abraham/reflection？

在项目目录运行 npm install @abraham/reflection，或使用 pnpm add / yarn add / bun add。

### @abraham/reflection 支持 TypeScript 吗？

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

### @abraham/reflection 支持 ESM 还是 CommonJS？

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

### @abraham/reflection 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=22。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @abraham/reflection 可以商用吗？

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

### @abraham/reflection 的下载量有多少？

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

### @abraham/reflection 还在维护吗？

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

