user1354934 user1354934 - 1 month ago 5
TypeScript Question

In TypeScript, how can I fix an error "property ____ is protected and only accessible within class ____"?

I am working on an angular 2 project and I am using a library called

ng2-bootstrap
.

I am using modals from this library, and there is a property called
_isShown
that I am checking in a function inside my class:

proceed() {
if (this.childModal._isShown) this.hideChildModal(); // error message here: "[ts] Property '_isShown' is protected and only accessible within class 'ModalDirective' and its subclasses."
this.router.navigate(['signup-step-two']);
}


It seems to work even though I get that error, but how can I fix that error correctly?

Thanks

Answer

Because _isShown is protected in ModalDirective source code. Check it out here.

You can access the public value using isShown() function.

It is declared as public in the source code.

  public get isShown(): boolean {
    return this._isShown;
  }