call a protected static property on an instance of a class in typescript

To call a protected static property on an instance of a class in TypeScript, you can use the following syntax:

index.ts
class MyClass {
  protected static myProperty: string = 'Hello, world!'
}

class MyChildClass extends MyClass {
  public logMyProperty() {
    console.log(MyChildClass.myProperty)
  }
}

const instance = new MyChildClass()
instance.logMyProperty() // output: 'Hello, world!'
275 chars
13 lines

In this example, we define a MyClass with a protected static myProperty property. We then create a MyChildClass which extends MyClass.

Inside MyChildClass, we create a public method logMyProperty that logs the myProperty value using the MyChildClass.myProperty syntax, since MyChildClass extends MyClass.

We then create an instance of MyChildClass and call the logMyProperty method on it, which outputs the value of the myProperty property. Note that we do not need to use the new keyword when accessing the static property; instead, we use the class name followed by the property name (MyChildClass.myProperty).

gistlibby LogSnag