@typescript-eslint/no-empty-function
Disallow functions with an empty body
has suggestions
Also called typescript-eslint/no-empty-function, typescript/no-empty-function
An empty function is either unfinished or does nothing on purpose, and the reader can't tell which. If it's on purpose, say so in a comment in the body. A function with a comment in it isn't reported.
A constructor that declares properties in its parameters, such as constructor(private name: string) {}, isn't reported either.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-empty-function" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-empty-function" = ["error", {
allow = [], # list of strings
}]Examples#
example.ts2 problems
function onClose() {}Unexpected empty function onClose.
class Logger {
flush() {}Unexpected empty method flush.
}example.tsNo problems
function onClose() {
// Nothing to clean up.
}
class Logger {
constructor(private prefix: string) {}
flush() {
console.log(this.prefix);
}
}Options#
| Option | Default | What it does |
|---|---|---|
allow | [] | The kinds of functions that may be empty. |
In allow | What may be empty |
|---|---|
"functions" | function save() {} and function () {} |
"arrowFunctions" | () => {} and async () => {} |
"generatorFunctions" | function* items() {} |
"asyncFunctions" | async function load() {} |
"methods" | flush() {} in a class or an object |
"generatorMethods" | *items() {} |
"asyncMethods" | async load() {} |
"getters" | get size() {} |
"setters" | set size(value) {} |
"constructors" | Every constructor |
"private-constructors" | private constructor() {} |
"protected-constructors" | protected constructor() {} |
"decoratedFunctions" | Methods that have a decorator |
"overrideMethods" | Methods marked override |
An empty arrow function is a common way to write "do nothing":
example.tswith
{ "allow": ["arrowFunctions"] }No problemsconst noop = () => {};
const wait = async () => {};A private constructor stops other code from calling new:
example.tswith
{ "allow": ["private-constructors"] }No problemsclass Registry {
static shared = new Registry();
private constructor() {}
}An override with an empty body turns off what the base class does:
example.tswith
{ "allow": ["overrideMethods"] }No problemsclass Widget {
render() {
console.log("widget");
}
}
class Spacer extends Widget {
override render() {}
}