add a private method to a class in typescript

To add a private method to a class in TypeScript, simply define the method using the private keyword. Here's an example:

index.ts
class MyClass {
  private myPrivateMethod(): void {
    // do something here
  }
}
83 chars
6 lines

In this example, we have defined a class MyClass with a private method myPrivateMethod(). The private keyword ensures that this method is only accessible within the class and not outside of it.

You can also define private methods with parameters:

index.ts
class MyClass {
  private myPrivateMethod(param1: number, param2: string): void {
    // do something here
  }
}
113 chars
6 lines

Just like with public methods, private methods can have any number of parameters and can return any data type.

gistlibby LogSnag