Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 1.85 KB

no-empty-function.md

File metadata and controls

47 lines (33 loc) · 1.85 KB

Disallow Empty Functions (@typescript-eslint/no-empty-function)

Empty functions can reduce readability because readers need to guess whether it’s intentional or not. So writing a clear comment for empty functions is a good practice.

Rule Details

The @typescript-eslint/no-empty-function rule extends the no-empty-function rule from ESLint core, and adds support for handling Typescript specific code that would otherwise trigger the rule.

One example of valid Typescript specific code that would otherwise trigger the no-empty-function rule is the use of parameter properties in constructor functions:

class Person {
  constructor(private firstName: string, private surname: string) {}
}

The above code is functionally equivalent to:

class Person {
  private firstName: string;
  private surname: string;

  constructor(firstName: string, surname: string) {
    this.firstName = firstName;
    this.surname = surname;
  }
}

Parameter properties enable both the declaration and initialization of member properties in a single location, avoiding the boilerplate & duplication that is common when initializing member properties from parameter values in a constructor function.

In these cases, although the constructor has an empty function body, it is technically valid and should not trigger an error.

See the ESLint documentation for more details on the no-empty-function rule.

Rule Changes

{
    // note you must disable the base rule as it can report incorrect errors
    "no-empty-function": "off",
    "@typescript-eslint/no-empty-function": "error"
}

Taken with ❤️ from ESLint core