Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I override just a getter in a subclass?

If I have a base class that has a getter and setter, and I override the getter in a subclass (but not the setter), I am getting Cannot set property x of #<MyClass> which has only a getter … Do I really have to have such a superfluous method in my subclass like:

set x(val: any) {
  super.x = val;
}

??

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is something really weird, as per ES6 i experienced that lets say design issue, both get and set must be defined on child class. As a work around i ended up defining methods instead of accessors.

class A
{
    protected _val:number;
    getval():number
    {
        return this._val;
    }

}

class B extends A
{

    setval(value:number)
    {
        this._val = value;
    }

}

Theres a large thread here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading