前言

在 TypeScript – Decorator 装饰器 里,我有提到 TypeScript 只实现了 decorate 的特性,把 metadata 的特性独立了出来。

本来我以为还需要等待很长的时间他们才会实现,没想到 v5.2 既然推出了。哎哟,不错哦!

声明: Decorator 不是 TypeScript 语法,它是 ECMAScript (AKA JavaScript) 标准。Metadata 也是,目前是 stage 3

参考

Docs – Announcing TypeScript 5.2

什么是 Decorator Metadata?

Decorator Metadata 类似于 C# 的 Attribute。就是在 class 属性上写标签(简单说就是记入一些资料)。然后通过反射获取到这些资料并且使用它们。

通常我们用它来实现 Metaprogramming。

搭建环境

上一篇的 Disposable 做法类似。

必须使用 TypeScript Compiler (AKA tsc),esbuild 还不支持 Decorator。

tsconfig.json 加上 compilerOptions.lib "ESNext.Decorators"

{
"compilerOptions": {
"target": "ES2017",
"lib": [
"ES2017",
"ESNext.Decorators",
"DOM"
],
}
}

再加上 runtime polyfill

(Symbol as { metadata: symbol }).metadata ??= Symbol("Symbol.metadata");

使用方式

定义 decorator function

function setMetadata(_target: unknown, context: ClassMemberDecoratorContext) {
context.metadata[context.name] = true;
}

metadata 这个属性是新的。之前的 Decorator 没有。

apply to class member

class Person {
@setMetadata
firstName = 'Derrick';
}

反射

console.log(JSON.stringify(Person[Symbol.metadata])); // {"firstName":true}

简单明了。

实战场景

TODO future... 等以后有用上了,再补。

TypeScript – Decorator Metadata的更多相关文章

  1. [TypeScript] Reflection and Decorator Metadata

    TypeScript allows you to emit decorator metadata which enables more powerful features through reflec ...

  2. angular2 学习笔记 (Typescript - Attribute & reflection & decorator)

    更新 : 2018-11-27 { date: Date } 之前好像搞错了,这个是可以用 design:type 拿到的 { date: Date | null } 任何类型一但配上了 | 就 de ...

  3. TypeScript 1.5 Beta带来修饰元数据支持

    (此文章同时发表在本人微信公众号"dotNET每日精华文章") 今天由于有点小感冒,就不长篇大论了,简单介绍一下和VS 2015 RC一同发布的TypeScript 1.5 Bet ...

  4. angular2 学习笔记 (Typescript - Attribute & reflection)

    refer : https://www.npmjs.com/package/reflect-metadata refer : https://www.typescriptlang.org/docs/h ...

  5. ES-Next @Decorator All In One

    ES-Next @Decorator All In One @装饰器 import { logged } from "./logged.mjs"; class C { @logge ...

  6. [Angular 2] Understanding @Injectable

    In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...

  7. Angular2经典文章集锦

    Angular Metadata 等基础知识 http://www.jianshu.com/p/aeb11061b82c Metadata告诉Angular如何处理一个类,只有我们将它通告给Angul ...

  8. 聊聊IOC中依赖注入那些事 (Dependency inject)

    What is Dependency injection 依赖注入定义为组件之间依赖关系由容器在运行期决定,形象的说即由容器动态的将某个依赖关系注入到组件之中在面向对象编程中,我们经常处理的问题就是解 ...

  9. Markdown常用语法学习

    Markdown常用语法学习,这些就够用了. 演示地址: https://github.com/YalongYan/Markdown-- 特别提示:  标题'##'后面必须加一个空格,否则编译不对.# ...

  10. [Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript

    Vue models, v-model, allow us to use two-way data binding, which is useful in some cases such as for ...

随机推荐

  1. leetcode简单(双指针):[88, 202, 345, 392, 455, 905, 922, 917, 925, 942]

    [toc 88. 合并两个有序数组 var merge = function(nums1, m, nums2, n) { let A1 = nums1.slice(0, m) let A2 = num ...

  2. [WUSTCTF2020]朴实无华(命令执行)

    请求头问题 去查了一下资料了解了一下没有什莫用 robots.txt 中有东西 假flag 但是请求头里有重要消息 访问页面/fl4g.php <img src="/img.jpg&q ...

  3. PointNet笔记

    可能遇到的问题 在windows上运行pointnet的代码时,可能会遇到一些问题: 1.比如提示OSError: no file with expected extension, 这是因为可视化的s ...

  4. elementui中实现loding实现局部加载,以el-dialog为例

    效果 封装loading加载(也可以直接使用,封装为了方便多次调用) 组件定义:loadDiy.js import { Loading } from "element-ui"; e ...

  5. layout文本相关

    Textview t=findViewById(R.id.t); ONE设置文本内容: 在XML中android:text直接写 在java中setText()中修改 注意点1继承appcompata ...

  6. wordpress站点转移

    title: wordpress站点转移 date: 2024/7/13 11:11:11 tag: linux学习 categories: wordpress建设 description: 搭建后的 ...

  7. 压力测试工具httperf使用方法

    目录 压力测试工具httperf使用方法 通过tar zxvf解压httperf-0.9.0.tar.gz 进入目录 安装c++编译环境 开始编译 进入编译后的bin目录 开始测试 压力测试工具htt ...

  8. 【SQL】 牛客网SQL训练Part1 简单难度

    地址位置: https://www.nowcoder.com/exam/oj?difficulty=2 查找入职员工时间排名倒数第三的员工所有信息 -- 准备脚本 drop table if exis ...

  9. 【H5】10 嵌入技术

    到目前为止,您应该掌握了将图像.视频和音频嵌入到网页上的诀窍了. 此刻,让我们继续深入学习,来看一些能让您在网页中嵌入各种内容类型的元素: <iframe>, <embed>  ...

  10. 【Redis】02 Redis 搭建与操作

    Redis的安装及启动停止 官网地址: https://redis.io/download 使用wget命令下载redis wget 下载地址 下载: [root@VM-0-7-centos ~]# ...