'Explicitly apply decorator after defineProperty

I have typescript code with "experimentalDecorators": true which allows me to apply decorators.

Also I'm using mobx witch has decorators like observable, computed and so on.

Some properties in my code are generated dynamically and I want to apply decorators to them too.

In following code

class Smth {
  @observable first = 0;
  declare second: number;

  constructor(
    getSecond: () => number,
  ) {
    Object.defineProperty(this, "second", { get: getSecond, configurable: true });

    makeObservable(this);
  }
}

I want to do something near this call

Object.defineProperty(this, "second", { get: getSecond, configurable: true });

so that it would behave like

@computed get second() { /* ... */ }

How can I apply computed decorator after I used defineProperty?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source