add a private static property to a class in typescript

To add a private static property to a class in TypeScript, you can use the private and static keywords along with the property name and its type. Here is an example:

index.ts
class MyClass {
  private static myPrivateStaticProperty: string;
}
68 chars
4 lines

In this example, myPrivateStaticProperty is a private static property of MyClass with the type string. This property can only be accessed within the class and cannot be accessed from outside the class or its instances.

You can also initialize the private static property with a value:

index.ts
class MyClass {
  private static myPrivateStaticProperty: string = 'default value';
}
86 chars
4 lines

In this case, myPrivateStaticProperty is initialized with the value 'default value'.

To access the private static property within the class, you can use the MyClass.myPrivateStaticProperty syntax:

index.ts
class MyClass {
  private static myPrivateStaticProperty: string = 'default value';

  myMethod() {
    console.log(MyClass.myPrivateStaticProperty);
  }
}
156 chars
8 lines

In this example, myMethod logs the value of myPrivateStaticProperty to the console.

gistlibby LogSnag