Search code examples
javascriptooptypescriptgetter-setter

auto getter and setter in TypeScript


I've created this (sort of auto getter and setter) for javaScript but I don't know how to implement this in typeScript

I want to make an Object Oriented version of that if that's possible.


Solution

  • Currently, there is no good way to do this. I think you will just need to go the boilerplate-y way:

    class Foo {
        private _bar: number;
        get bar() { return this._bar }
        set bar(bar: number) { this._bar = bar}
        // ...
    }
    

    If you wanted, you could use an editor snippet to make this a bit less of a pain.