Is there a way to use decorator with setters? #2820
Unanswered
UserAbuser228
asked this question in
Q&A
Replies: 2 comments 18 replies
-
The getter(+setter) fields are transformed to const MyDecorator = <T>(): PropertyDecorator => () => {
const value = observable.box();
return {
configurable: true,
get: () => value.get(),
set: val => value.set(val)
};
}; |
Beta Was this translation helpful? Give feedback.
15 replies
-
Depends on how decorators are transpiled, the semantics of Babel are close
but different to TS
…On Sun, 21 Feb 2021, 10:48 urugator, ***@***.***> wrote:
That's wierd
https://www.typescriptlang.org/docs/handbook/decorators.html#property-decorators
:
*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. The return value is ignored too. As
such, a property decorator can only be used to observe that a property of a
specific name has been declared for a class.*
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2820 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBFH65H4VNYRJR6U2FTTADQGHANCNFSM4X46KJVA>
.
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to use decorator, that would control property's setter. Here's what I tried.
In the case above, both my
@MyDecorator
andmakeAutoObservable
don't work. They do work separatly. WithoutmakeAutoObservable
the value is increasing as it should be. Without@MyDecorator
it's increasing and runs the callback forreaction
. Both of them do not actually change the value, it always equal to 1.I tried several ways, even with older versions and
@observable
decorators, but none of my tries were successfull.So, could you please tell is there a way to create a decorator with setter?
Beta Was this translation helpful? Give feedback.
All reactions