- Notifications
You must be signed in to change notification settings - Fork13.2k
Closed
Description
ES7 proposal
The ES7 proposal for decorators can be found here:https://github.com/wycats/javascript-decorators
The ES7 proposal serves as the base of this proposal. Below are notes about how the type system
Decorator targets:
Class constructor
@F("color")@GclassFoo{}
desugars to:
varFoo=(function(){functionFoo(){}Foo=__decorate([F("color"),G],Foo);returnFoo;})();
Methods
classFoo{ @F(color) @Gbar(){}}
desugars to:
varFoo=(function(){functionFoo(){}Foo.prototype.bar=function(){};Object.defineProperty(Foo.prototype,"bar",__decorate([F(color),G],Foo.prototype,"bar",Object.getOwnPropertyDescriptor(Foo.prototype,"bar")));returnFoo;})();
Static method
classFoo{ @F("color") @GstaticsMethod(){}}
desugars to:
varFoo=(function(){functionFoo(){}Foo.sMethod=function(){};Object.defineProperty(Foo,"sMethod",__decorate([F("color"),G],Foo,"sMethod",Object.getOwnPropertyDescriptor(Foo,"sMethod")));returnFoo;})();
Properties
classFoo{ @F("color") @Gprop:number;}
desugars to:
varFoo=(function(){functionFoo(){}__decorate([F("color"),G],Foo.prototype,"prop");returnFoo;})();
Method/Accessor formal parameter
classFoo{method(@Ga, @F("color")b){}}
desugars to:
varFoo=(function(){functionFoo(){}Foo.prototype.method=function(a,b){};__decorate([G],Foo.prototype,"method",0);__decorate([F("color")],Foo.prototype,"method",1);returnFoo;})();
Where the __decorate is defined as:
var__decorate=this.__decorate||function(decorators,target,key,value){varkind=typeof(arguments.length==2 ?value=target :value);for(vari=decorators.length-1;i>=0;--i){vardecorator=decorators[i];switch(kind){case"function":value=decorator(value)||value;break;case"number":decorator(target,key,value);break;case"undefined":decorator(target,key);break;case"object":value=decorator(target,key,value)||value;break;}}returnvalue;};
Decorator signatures:
A valid decorator should be:
- Assignable to one of the Decorator types (ClassDecorator | PropertyDecorator | MethodDecorator | ParameterDecorator) as described below.
- Return a value (in the case of class decorators and method decorator) that is assignable to the decorated value.
declaretypeClassDecorator=<TFunctionextendsFunction>(target:TFunction)=>TFunction|void;declaretypePropertyDecorator=(target:Object,propertyKey:string|symbol)=>void;declaretypeMethodDecorator=<T>(target:Object,propertyKey:string|symbol,descriptor:TypedPropertyDescriptor<T>)=>TypedPropertyDescriptor<T>|void;declaretypeParameterDecorator=(target:Function,propertyKey:string|symbol,parameterIndex:number)=>void;
Notes:
- Decorating a function declaration is not allowed as it will block hoisting the function to the top of the scope, which is a significant change in semantics.
- Decorating function expressions and arrow functions are not supported. The same effect can be achived by applying the decorator function as
var x = dec(function () { }); - Decorating function formal parameters is currently not part of the ES7 proposal.
- Decorators are not allowed when targeting ES3