add a static property to a class in typescript

To add a static property to a class in TypeScript, use the static keyword followed by the property name and its value.

index.ts
class MyClass {
  static myStaticProperty: string = "Hello World";

  // rest of the class properties and methods
}
116 chars
6 lines

In the example above, we add a static property called myStaticProperty to the MyClass class. The value of this property is "Hello World".

You can access this static property without creating an instance of the MyClass class, like so:

index.ts
console.log(MyClass.myStaticProperty); // Output: "Hello World"
64 chars
2 lines

Note that static properties are shared among all instances of the class and can also be accessed within static methods.

gistlibby LogSnag