TSConfig
emitDecoratorMetadata
Enables experimental support for emitting type metadata for decorators which works with the module reflect-metadata
.
For example, here is the TypeScript
tsTry
functionLogMethod (target : any,propertyKey : string | symbol,descriptor :PropertyDescriptor ) {console .log (target );console .log (propertyKey );console .log (descriptor );}classDemo {@LogMethod publicfoo (bar : number) {// do nothing}}constdemo = newDemo ();
With emitDecoratorMetadata
not set to true (default) the emitted JavaScript is:
tsTry
"use strict";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 LogMethod(target, propertyKey, descriptor) {console.log(target);console.log(propertyKey);console.log(descriptor);}class Demo {foo(bar) {// do nothing}}__decorate([LogMethod], Demo.prototype, "foo", null);const demo = new Demo();
With emitDecoratorMetadata
set to true the emitted JavaScript is:
tsTry
"use strict";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;};var __metadata = (this && this.__metadata) || function (k, v) {if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);};function LogMethod(target, propertyKey, descriptor) {console.log(target);console.log(propertyKey);console.log(descriptor);}class Demo {foo(bar) {// do nothing}}__decorate([LogMethod,__metadata("design:type", Function),__metadata("design:paramtypes", [Number]),__metadata("design:returntype", void 0)], Demo.prototype, "foo", null);const demo = new Demo();