no-meaningless-void-operator
Disallow the
void
operator except when used to discard a value.
Extending "plugin:@typescript-eslint/strict-type-checked"
in an ESLint configuration enables this rule.
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
Some problems reported by this rule are manually fixable by editor suggestions.
This rule requires type information to run.
void
in TypeScript refers to a function return that is meant to be ignored.
The void
operator is a useful tool to convey the programmer's intent to discard a value.
For example, it is recommended as one way of suppressing @typescript-eslint/no-floating-promises
instead of adding .catch()
to a promise.
This rule helps an authors catch API changes where previously a value was being discarded at a call site, but the callee changed so it no longer returns a value.
When combined with no-unused-expressions, it also helps readers of the code by ensuring consistency: a statement that looks like void foo();
is always discarding a return value, and a statement that looks like foo();
is never discarding a return value.
This rule reports on any void
operator whose argument is already of type void
or undefined
.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-meaningless-void-operator": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-meaningless-void-operator": "error"
}
};
Try this rule in the playground ↗
Examples
Examples
- ❌ Incorrect
- ✅ Correct
void (() => {})();
function foo() {}
void foo();
Open in Playground(() => {})();
function foo() {}
foo(); // nothing to discard
function bar(x: number) {
void x; // discarding a number
return 2;
}
void bar(1); // discarding a number
Open in PlaygroundOptions
This rule accepts the following options:
type Options = [
{
/** Whether to suggest removing `void` when the argument has type `never`. */
checkNever?: boolean;
},
];
const defaultOptions: Options = [{ checkNever: false }];
checkNever
Whether to suggest removing void
when the argument has type never
. Default: false
.
When Not To Use It
If you don't mind extra void
s in your project, you can avoid this rule.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.