注意:现在装饰器和装饰器元数据都已在 TC39 中达到第 3 阶段,下面提出的 API 不再考虑标准化。然而,本包将继续支持使用 TypeScript 旧版 --experimentalDecorators 选项的项目,因为某些项目可能无法迁移到使用标准装饰器。
npm install reflect-metadata
// - 修改全局 `Reflect` 对象(或在 ES5 运行时中定义一个)。 // - 支持 ESM 和 CommonJS。 // - 包含用于旧运行时的 `Map`、`Set` 和 `WeakMap` 内部 polyfills。 import "reflect-metadata"; // - 修改全局 `Reflect` 对象(或在 ES5 运行时中定义一个)。 // - 支持 ESM 和 CommonJS。 // - 需要运行时支持 `package.json` 中的 `"exports"`。 // - 不包含内部 polyfills。 import "reflect-metadata/lite";
// - 修改全局 `Reflect` 对象(或在 ES5 运行时中定义一个)。 // - 包含用于旧运行时的 `Map`、`Set` 和 `WeakMap` 内部 polyfills。 require("reflect-metadata"); // - 修改全局 `Reflect` 对象(或在 ES5 运行时中定义一个)。 // - 需要运行时支持 `package.json` 中的 `"exports"`。 // - 不包含内部 polyfills。 require("reflect-metadata/lite");
<script> 在浏览器中使用HTML
<!-- 修改全局 `Reflect` 对象(或在 ES5 运行时中定义一个)。 --> <!-- 包含用于旧运行时的 `Map`、`Set` 和 `WeakMap` 内部 polyfills。 --> <script src="path/to/reflect-metadata/Reflect.js"></script> <!-- 修改全局 `Reflect` 对象(或在 ES5 运行时中定义一个)。 --> <!-- 不包含内部 polyfills。 --> <script src="path/to/reflect-metadata/ReflectLite.js"></script>
脚本
// - 使编辑器中的类型可用。 /// <reference path="path/to/reflect-metadata/standalone.d.ts" />
class C { @Reflect.metadata(metadataKey, metadataValue) method() { } }
Reflect.defineMetadata(metadataKey, metadataValue, C.prototype, "method");
let obj = new C(); let metadataValue = Reflect.getMetadata(metadataKey, obj, "method");
// 在对象或属性上定义元数据 Reflect.defineMetadata(metadataKey, metadataValue, target); Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey); // 检查对象或属性的原型链上是否存在元数据键 let result = Reflect.hasMetadata(metadataKey, target); let result = Reflect.hasMetadata(metadataKey, target, propertyKey); // 检查对象或属性是否存在自身的元数据键 let result = Reflect.hasOwnMetadata(metadataKey, target); let result = Reflect.hasOwnMetadata(metadataKey, target, propertyKey); // 获取对象或属性原型链上的元数据键的元数据值 let result = Reflect.getMetadata(metadataKey, target); let result = Reflect.getMetadata(metadataKey, target, propertyKey); // 获取对象或属性自身的元数据键的元数据值 let result = Reflect.getOwnMetadata(metadataKey, target); let result = Reflect.getOwnMetadata(metadataKey, target, propertyKey); // 获取对象或属性原型链上的所有元数据键 let result = Reflect.getMetadataKeys(target); let result = Reflect.getMetadataKeys(target, propertyKey); // 获取对象或属性自身的所有元数据键 let result = Reflect.getOwnMetadataKeys(target); let result = Reflect.getOwnMetadataKeys(target, propertyKey); // 删除对象或属性的元数据 let result = Reflect.deleteMetadata(metadataKey, target); let result = Reflect.deleteMetadata(metadataKey, target, propertyKey); // 通过装饰器将元数据应用于构造函数 @Reflect.metadata(metadataKey, metadataValue) class C { // 通过装饰器将元数据应用于方法(属性) @Reflect.metadata(metadataKey, metadataValue) method() { } }
function ParamTypes(...types) { return (target, propertyKey) => { const symParamTypes = Symbol.for("design:paramtypes"); if (propertyKey === undefined) { target[symParamTypes] = types; } else { const symProperties = Symbol.for("design:properties"); let properties, property; if (Object.prototype.hasOwnProperty.call(target, symProperties)) { properties = target[symProperties]; } else { properties = target[symProperties] = {}; } if (Object.prototype.hasOwnProperty.call(properties, propertyKey)) { property = properties[propertyKey]; } else { property = properties[propertyKey] = {}; } property[symParamTypes] = types; } }; }
function ParamTypes(...types) { // 由于propertyKey实际上是可选的,所以在这里使用更容易 return (target, propertyKey) => { Reflect.defineMetadata("design:paramtypes", types, target, propertyKey); } // 相比之下,将目标和键放在前面会有多个重载: // // return (target, propertyKey) => { // if (propertyKey === undefined) { // Reflect.defineMetadata(target, "design:paramtypes", types); // } // else { // Reflect.defineMetadata(target, propertyKey, "design:paramtypes", types); // } // } // // 或者为类和属性使用不同的方法: // // return (target, propertyKey) => { // if (propertyKey === undefined) { // Reflect.defineMetadata(target, "design:paramtypes", types); // } // else { // Reflect.definePropertyMetadata(target, propertyKey, "design:paramtypes", types); // } // } }
"experimentalDecorators": true。"emitDecoratorMetadata": true。


你的AI Agent团队
Vecbase 是专为 AI 团队打造的智能工作空间,将数据管理、模型协作与知识沉淀整合于一处。算法、产品与业务在同一平台无缝协同,让从数据到 AI 应用的落地更快一步。


全球首个AI音乐社区
音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。


阿里Qoder团队推出的桌面端AI智能体
QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。


一站式搞定所有学习需求
不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。


为AI短剧协作而生
专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效 等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。


能听懂你表达的视频模型
Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。


国内直接访问,限时3折
输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动

