Experimental
TypeScript strives to only include features which are confirmed to be added into the JavaScript language.
There have been cases where a feature is compelling enough to be an exception to that rule, and these live as experimental compiler flags. It is possible that a version of these features may be different when/if they are added to the JavaScript language, and thus are considered risky.
Emit Decorator Metadata - emitDecoratorMetadata
Enables experimental support for emitting type metadata for decorators which works with the module reflect-metadata
.
For example, here is the JavaScript
functionLogMethod (target : any,propertyKey : string | symbol,descriptor :PropertyDescriptor ) {console .log (target );console .log (propertyKey );console .log (descriptor );}
classDemo {@LogMethod publicfoo (bar : number) {// do nothing}}Try
constdemo = newDemo ();
With emitDecoratorMetadata
not set to true (default):
Try
"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:
Try
"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();
Related:
Experimental Decorators - experimentalDecorators
Enables experimental support for decorators, which is in stage 2 of the TC39 standardization process.
Decorators are a language feature which hasn’t yet been fully ratified into the JavaScript specification. This means that the implementation version in TypeScript may differ from the implementation in JavaScript when it it decided by TC39.
You can find out more about decorator support in TypeScript in the handbook.
Related: