add a protected method to a class in typescript

To add a protected method to a class in TypeScript, you will need to use the protected keyword as an access modifier for the method.

index.ts
class MyClass {
  protected myProtectedMethod() {
    // Code goes here
  }
}
78 chars
6 lines

The protected keyword allows the method to be accessed within the class as well as within any subclasses of the class.

To call the protected method from within the class or subclass, use the this keyword to reference the instance of the class, like this:

index.ts
class MySubclass extends MyClass {
  myMethod() {
    this.myProtectedMethod();
  }
}
86 chars
6 lines

gistlibby LogSnag