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

To call a static property on an instance of a class in TypeScript, you would typically use the class name to access the static property, rather than the instance itself. Here's an example:

index.ts
class MyClass {
  static myStaticProp = 'Hello, static property!';

  myInstanceProp = 'Hello, instance property!';
}

const myInstance = new MyClass();
console.log(myInstance.myInstanceProp); // 'Hello, instance property!'
console.log(MyClass.myStaticProp); // 'Hello, static property!'
288 chars
10 lines

In this example, we define a class, MyClass, with both a static property, myStaticProp, and an instance property, myInstanceProp. We then create an instance of MyClass, myInstance, and access both properties using the instance (myInstance.myInstanceProp) and class name (MyClass.myStaticProp).

Note that attempting to access a static property with an instance variable will result in an error in TypeScript.

gistlibby LogSnag