[TypeScript] Reflection and Decorator Metadata
TypeScript allows you to emit decorator metadata which enables more powerful features through reflection. This lesson show you how decorators and reflection fit together and how to configure your own decorators to use reflection.
For now, if we look at the compiled file:
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function addAge(age) {
return function (targetClass) {
return (function () {
function class_1() {
this.age = age;
this.name = new targetClass().name;
}
return class_1;
}());
};
}
var Person = (function () {
function Person() {
this.name = "Johnn";
}
Person = __decorate([
addAge(30)
], Person);
return Person;
}());
var john = new Person();
console.log(john); // {name: "Johnn", age: 30}
It decorates addAge to Person class.
Now if we enable "emitDecoratorMetadata" in tsconfig.json:
{
"compilerOptions": {
"rootDir": "src",
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": false,
"outDir": "./dist",
"noEmitOnError": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
Compile the files again, now we get:
var Person = (function () {
function Person() {
this.name = "Johnn";
}
Person = __decorate([
addAge(30),
__metadata('design:paramtypes', [])
], Person);
return Person;
}());
It also add metadata.
Install:
npm install reflect-metadata crypto --save
Index.html:
<script>
System.config({
packages: {
"dist": {
"defaultExtension": "js",
"main": "main"
},
"rxjs": {
"defaultExtension": "js"
}
},
map: {
"lodash": "https://npmcdn.com/lodash@4.13.1",
"rxjs": "node_modules/rxjs",
"reflect-metadata": "node_modules/reflect-metadata/Reflect.js",
"crypto": "node_modules/crypto/sha1.js",
}
}); System.import("dist")
</script>
main.ts:
import 'reflect-metadata';
function example(){
return function(targetClass){
const types = Reflect.getMetadata('design:paramtypes', targetClass);
console.log(types);
return targetClass
}
}
@example()
class Person{
constructor(name: string, age: number){
}
}
new Person("John", 10);
So in the example() fucntion, we console log out types, it will show:

That means we were able to make it generic, so that any class that comes through into this example decorator, we can look up its types and then use those types to modify or pass into the constructor, and return the class decorated however we want.
[TypeScript] Reflection and Decorator Metadata的更多相关文章
- TypeScript 1.5 Beta带来修饰元数据支持
(此文章同时发表在本人微信公众号"dotNET每日精华文章") 今天由于有点小感冒,就不长篇大论了,简单介绍一下和VS 2015 RC一同发布的TypeScript 1.5 Bet ...
- egg.js路由的优雅改造
引言 在使用express,koa, 或者是egg.js进行node server开发的过程中,我们的路由基本上都是定义在controller层的,框架对于 node 原生路由都会进行一层封装,一版都 ...
- google protobuf 使用示例
定义.proto接口文件 package tutorial; message Person { required ; required int32 id = ; //unique ID number ...
- caffe源码学习之Proto数据格式【1】
前言: 由于业务需要,接触caffe已经有接近半年,一直忙着阅读各种论文,重现大大小小的模型. 期间也总结过一些caffe源码学习笔记,断断续续,这次打算系统的记录一下caffe源码学习笔记,巩固一下 ...
- 如何用vue-cli3脚手架搭建一个基于ts的基础脚手架
目录 准备工作 搭建项目 vue 中 ts 语法 项目代理及 webpack 性能优化 其他 忙里偷闲,整理了一下关于如何借助 vue-cli3 搭建 ts + 装饰器 的脚手架,并如何自定义 web ...
- angular2 学习笔记 (Typescript - Attribute & reflection & decorator)
更新 : 2018-11-27 { date: Date } 之前好像搞错了,这个是可以用 design:type 拿到的 { date: Date | null } 任何类型一但配上了 | 就 de ...
- angular2 学习笔记 (Typescript - Attribute & reflection)
refer : https://www.npmjs.com/package/reflect-metadata refer : https://www.typescriptlang.org/docs/h ...
- 异常:“System.Reflection.Metadata”已拥有为“System.Collections.Immutable”定义的依赖项
参考动态执行T4模板:https://msdn.microsoft.com/zh-cn/library/bb126579.aspx 我项目是.NET Framework 4.5控制台应用程序写的. 执 ...
- TypeScript: Week Reflection
TypeScript: Week Reflection Introduction Type Script already provide decorators to help developers i ...
随机推荐
- BZOJ1861[ZJOI2006]Book书架
Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下 ...
- Docker的安装及简单使用
1. Docker的安装 (这里的“安装docker”其实就是安装Docker Engine) $ sudo apt-get intasll docker.io note: apt-get是ubun ...
- 修改PHP上传大小设置
目前文档文库上传大小是读取服务器的PHP环境的设置,你们的PHP环境上传限制是多少,这里显示的就是多少. 很多用户问我如何修改上传大小,自己可以百度一下方法,也可以根据以下步骤修改: 1.找到服务务器 ...
- 移除Ubuntu“下载额外数据不成功”的提示通知
参考自经过几天的摸索,终于得出安装flashplugin-installer的方法 移除"下载额外数据不成功"的恼人提示通知,方法: $cd /usr/share/package- ...
- 裸眼3D立体显示技术原理详解
众所周知,现实世界是一个三维空间,除去时间这一维度,现实世界是由长度.宽度和高度三个维度组成,我们每天就生活在这个三维世界中,而现有的显示设备大多数都只能显示二维信息,并不能带给人真实的三维感觉.为了 ...
- EMMC 简要介绍
一直想写一篇关于EMMC的文章,但是因为之前弄了很多PPT,所以一直提不起兴趣,索性直接把之前的一个介绍EMMC的PPT贴出来给大家看看,有什么问题可以直接跟帖,我会第一时间进行解答,谢谢
- springboot + devtools(热部署)
技术介绍 devtools:是boot的一个热部署工具,当我们修改了classpath下的文件(包括类文件.属性文件.页面等)时,会重新启动应用(由于其采用的双类加载器机制,这个启动会非常快,如果发现 ...
- [OJ] Insert Interval
LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...
- Lua和Javascript差异对比
Lua模拟器js方案 1.语法级模拟lua与js语言差异 1.1注释 js 为//,lua为--. 1.2变量js利用val来声明全局变量不存在局部变量,lua则不需要直接定位则为全局变量,local ...
- POJ2676 Sudoku(dfs)
题目链接. 题目大意: 就是数独游戏.横竖,每一个9宫方块,必须有1~9,且不重复. 分析: 直接DFS.一开始在原图上搜,会TLE.把要补全的空格,放入数组,这样就不用遍历整个图寻找要填的空格了. ...