call a method on an instance of a class in typescript

In TypeScript, you can call a method on an instance of a class using the dot notation. First, you need to create an instance of the class using the new keyword. Once you have an instance, you can call any method defined in the class using the dot notation. Here's an example:

index.ts
class MyClass {
  myMethod(): void {
    console.log("Hello world!");
  }
}

const myInstance = new MyClass();
myInstance.myMethod(); // Output: Hello world!
158 chars
9 lines

In this example, we defined a class MyClass that has a method myMethod. We then created an instance of this class called myInstance. Finally, we called the myMethod method on the myInstance instance using the dot notation.

In summary, to call a method on an instance of a class in TypeScript:

  1. Create an instance of the class using the new keyword.
  2. Call the method on the instance using the dot notation.

gistlibby LogSnag