Search code examples
angulargetter-setterangular-reactive-formslombok

Angular 6 lombok for reactive forms, any way to implement that behaviour?


Normally I create a class for my reactive Forms and I create methods to access the properties forms, instead to do:

myForm.get('property').value

I create a getter-setter:

get property(){ return this.get('property').value;}
set property(value: type){ this.get('property').setValue(value);}

This way I'm able to do myForm.property any place I want.

Is there a way to place a decorator on the class? I want to magically create all that getter-setters as lombok for Java does.


Solution

  • Even if you add the decorator and make it work at runtime, it's not going to work as you expect with TypeScript: you're adding new props (get/set accessors) to the class and TS would not be able to conclude that. So you'd have to write out the type definition by hand anyway -- at this point, it's not really worth it.

    So I guess the answer to your question is, unfortunatelly, "no".

    However, you can utilize you IDE and create a shortcut for generating all this for you. For example, WebStorm offers "live templates", and you can make it somthing like this:

    get $PROP(){ return this.get('$PROP').value;}
    set $PROP(value: type){ this.get('$PROP').setValue(value);}
    

    It will highlight the first $PROP and as yo type inside it, it willre-type the same thing across the other intances of $PROP.