Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Decorators #2249

Closed
Closed
Assignees
rbuckton
Labels
CommittedThe team has roadmapped this issueES NextNew featurers for ECMAScript (a.k.a. ESNext)FixedA PR has been merged for this issueSuggestionAn idea for TypeScript
@rbuckton

Description

@rbuckton

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:

  1. Assignable to one of the Decorator types (ClassDecorator | PropertyDecorator | MethodDecorator | ParameterDecorator) as described below.
  2. 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 asvar x = dec(function () { });
  • Decorating function formal parameters is currently not part of the ES7 proposal.
  • Decorators are not allowed when targeting ES3

Metadata

Metadata

Assignees

Labels

CommittedThe team has roadmapped this issueES NextNew featurers for ECMAScript (a.k.a. ESNext)FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp