call a public method on an instance of a class in typescript

To call a public method on an instance of a class in TypeScript, you need to follow these steps:

  1. Create a new instance of the class by calling the class constructor function.

  2. Store the created instance in a variable.

  3. Call the public method on the stored instance by using the dot notation.

Here's an example:

index.ts
class MyClass {
  public myPublicMethod(): void {
    console.log('This is a public method.');
  }
}

const myInstance = new MyClass();
myInstance.myPublicMethod(); // Output: "This is a public method."
203 chars
9 lines

In this example, we create a new instance of the MyClass class by calling new MyClass(). We store the created instance in a variable called myInstance. Finally, we call the myPublicMethod method on the myInstance variable using the dot notation. The output of the method is logged to the console.

gistlibby LogSnag