@abraham/reflection

中文文档
0.14.0发布于 2026/11/02MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 10.5万

Lightweight ES Module implementation of reflect-metadata

原始使用文档
文档目录9 节
  1. Why?
  2. Install
  3. Which import?
  4. Usage
  5. Experimental decorators
  6. Standard decorators
  7. API
  8. @abraham/reflection/legacy
  9. @abraham/reflection/standard

Version Status Build Status npm bundle size (minified + gzip)

Reflection

Lightweight ES Module implementation of reflect-metadata to work with TypeScript's experimental decorator support, plus a matching API for standard decorators and decorator metadata.

Why?

The main reason for this library is to provide a much smaller implementation that can be included as a module.

  • ES module
    • reflection can be loaded with <script type="module" src="..."></script>
  • Size (uncompressed)

Read about how to drop 20K from your production Angular app by switching to this.

Install

npm install @abraham/reflection

Which import?

Use the one that matches how your project compiles decorators. A project uses one or the other, not both.

@abraham/reflection/legacy (default)@abraham/reflection/standard
DecoratorsLegacy: experimentalDecorators: trueStandard: TypeScript 5.2+ without experimentalDecorators, or Babel
Type metadatadesign:type, design:paramtypes, design:returntype with emitDecoratorMetadataNot available
Parameter decoratorsYesNo
GlobalsAdds methods to ReflectDefines Symbol.metadata and Function.prototype[Symbol.metadata] (null) if missing, as in the spec
Module formatsES module and UMD/CommonJSES module

Use @abraham/reflection/legacy for code written for reflect-metadata, including frameworks that rely on emitDecoratorMetadata for dependency injection. The bare @abraham/reflection import is the same module, so existing imports keep working and both can be mixed.

Use @abraham/reflection/standard for new code on standard decorators. They don't pass the class to member decorators, so Reflect.defineMetadata can't be used; metadata goes through the decorator's context.metadata instead.

Usage

Experimental decorators

tsconfig.json:

{
  "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"
  },
}
import '@abraham/reflection/legacy'; // or '@abraham/reflection'
Reflect.defineMetadata(metadataKey, metadataValue, target);

You can also import Reflection:

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

Standard decorators

Requires TypeScript 5.2 or later. tsconfig.json:

{
  "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"],
  },
}

Import it before any decorated class is defined, so Symbol.metadata exists when the class is created.

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'

Targets follow reflect-metadata: the class for class and static member metadata, and the prototype or an instance for instance member metadata. Subclasses inherit metadata and can override it. Values are stored privately, so the class's shared Symbol.metadata object is left for other libraries.

API

@abraham/reflection/legacy

The following methods are available:

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

@abraham/reflection/standard

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 共发布 21 个版本,首次发布于 2018/07/24;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.14.0latest2026/11/024 天前
0.13.02025/03/091.7 年前
0.12.02023/02/063 年前
0.11.02022/10/054 年前
0.10.02022/03/224 年前
0.9.12022/02/204 年前

关于 @abraham/reflection 的常见问题

@abraham/reflection 是什么?

Lightweight ES Module implementation of reflect-metadata

如何安装 @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 和提交记录判断。