[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 ...
随机推荐
- html 各个标签初始化
html,body,div,ul,li,ol,h1,h2,h3,h4,h5,h6,span,input{ margin:0;padding:0;}body{font:12px/1.5em " ...
- HTML5之本地文件系统API - File System API
HTML5之本地文件系统API - File System API 新的HTML5标准给我们带来了大量的新特性和惊喜,例如,画图的画布Canvas,多媒体的audio和video等等.除了上面我们提到 ...
- C语言程序设计做题笔记之C语言基础知识(下)
C 语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行 事.并且C是相当灵活的,用于执行计算机程序能完成的 ...
- [topcoder]CorrectMultiplicationTwo
http://community.topcoder.com/stat?c=problem_statement&pm=11609&rd=14547 http://apps.topcode ...
- Android Adapter的getViewTypeCount和getItemViewType
Adapter的getViewTypeCount和getItemViewType 不同的项目布局(item layout) 我们再举一个稍微复杂的例子,在上例的list中加入一些分隔线 你需要做这些: ...
- 最全的JAVA源码整合下载
http://www.360doc.com/content/14/0602/00/11407612_382890953.shtml http://www.360doc.com/content/14/0 ...
- eclipse导入已有源码
http://blog.csdn.net/scruffybear/article/details/1917301 如有转载,请注明出处,并保持文章的完整性,谢谢! 最近工作之余在研究国外经典书籍< ...
- 你们都是怎么阅读android系统源码的,用什么工具?
如果是eclipse,可以建立java或者c++工程,把framework源码导入.packages目录下的可以建立android工程.sourceinsight的话,建立工程把相应模块的代码添加进去 ...
- java线程(3)-多线程死锁
产生死锁的条件: 1.有至少一个资源不能共享2.至少有一个任务必须持有一个资源并且等待获取另一个被别的任务持有的资源3.资源不能任务抢占4.必须有循环等待 package com.thread; /* ...
- ASP.NET(C#) GridView (编辑、删除、更新、取消)
转自:http://my.oschina.net/dldlhrmc/blog/93458 前台代码 view source print? 01 <%@ Page Language=" ...