Search code examples
typescripttypescript-eslint

Syntax for TypeScript class methods


In TypeScript I can define methods like a real method (Foo) or a property which gets a method assigned (Baz).

class TestClass {
    private mText: string = "test";
    public Baz = (): void => {
        console.log(this.mText);
    }

    public Foo(): void {
        console.log(this.mText);
    }
}
  1. Are they equivalent?
  2. Are there differences?
  3. What syntax should I use?
  4. Can I use typescript-eslint to forbid one of them?

Solution

  • They are not equivalent. Baz would not be on the prototype of TestClass (Foo would), but it would be a property on each instance.