angulartypescriptgetter-setterextending-classes

Cannot set/get the property of extended class in typescript


I have an issue related to setting or getting the value of the extended class in Typescript and angular 2.

I have a class A with these properties:

export class A {
    protected name: string;

    set setName(name: string) {
        this.name= name;
    }

    get getName() {
        return this.name;
    }
}

I am extending the class A from B as :

export class B extends A implements OnInit {
    ngOnInit() {
        this.setName('User');
    }
}

This returns the error as :

this.setName is not a function

I suppose it is telling that there is no any function called setName in class B.

How do i set the value to be used in the function that's extending the Class A? What is the best way as the constructor initialization didn't worked for me.

I also cannot create the new instance of the class B and set the value. If i am wrong any where could anyone correct me?


Solution

  • If it is a setter, it should be

    this.setName = 'User';