Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator support in instance functions #10644

Closed
normalser opened this issue Aug 31, 2016 · 3 comments
Closed

Decorator support in instance functions #10644

normalser opened this issue Aug 31, 2016 · 3 comments
Labels
Domain: Decorators The issue relates to the decorator syntax Revisit An issue worth coming back to

Comments

@normalser
Copy link

class Test {

    // works
    @f()
    test() {

    }

   // does not work
    @f()
    test2 = ()=> {
    }

}

It would be useful if decorator would work in second case

@Thinkscape
Copy link

This is a major pain point. The latter is seen by the compiler as a property, hence property decorator rules apply - the decorator function doesn't receive a PropertyDescriptor. Even though a property decorator can return a descriptor, it can not access the original/intended value (the bound function).

From docs:

NOTE  A Property Descriptor is not provided as an argument to a property decorator due to how property decorators are initialized in TypeScript. This is because there is currently no mechanism to describe an instance property when defining members of a prototype, and no way to observe or modify the initializer for a property. As such, a property decorator can only be used to observe that a property of a specific name has been declared for a class.

I think there should be an easy way to receive the value inside the decorator, so it can be manipulated - otherwise decorators are unusable in above context (bound methods).

@RyanCavanaugh
Copy link
Member

@rbuckton is this a duplicate of something?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2017

The semantics of these two declarations is significantly different.

Methods (e.g. test) are created at class declaration time, and are placed on the prototype. Properties (e.g. test2) are initialized in the constructor for every instance, and live on the object itself.

Decorators do not run for every object, they only run on the declaration of the class. since the initialization expression can have side effects, it can not be evaluated at the declaration time, but rather has to be initialized in every invocation of the constructor.

This change will need some concept of "initializer" function that is part of the PropertyDiscriptor that would be passed in the decorator, but for that we need to wait for both decorators and public fields

@mhegazy mhegazy added Domain: Decorators The issue relates to the decorator syntax Revisit An issue worth coming back to labels Apr 26, 2017
@mhegazy mhegazy closed this as completed Apr 26, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Domain: Decorators The issue relates to the decorator syntax Revisit An issue worth coming back to
Projects
None yet
Development

No branches or pull requests

4 participants