@typescript-eslint/no-confusing-void-expression
Require expressions of type void to stand alone as statements
Also called typescript-eslint/no-confusing-void-expression, typescript/no-confusing-void-expression
A function that returns void has no result to use. Code like const saved = save(user) or return log(error) suggests there is one, and the reader goes looking for a value that doesn't exist. A void call belongs on its own line.
A void call is still fine on the right of &&, || and ??, and in the branches of ? :, if the whole expression is a statement.
--fix adds braces to an arrow function, and takes the call out of a return. A void value that's assigned or passed on is reported but not fixed.
Configure#
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
[lint.rules]
"@typescript-eslint/no-confusing-void-expression" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/no-confusing-void-expression" = ["error", {
ignoreArrowShorthand = false,
ignoreVoidOperator = false,
ignoreVoidReturningFunctions = false,
}]Examples#
declare function log(message: string): void;
const result = log("saved")Placing a void expression inside another expression is forbidden. Move it to its own statement instead.;
const onSave = () => log("saved")Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function.;
function onError(error: Error) {
return log(error.message)Returning a void expression from a function is forbidden. Please remove the return statement.;
}declare function log(message: string): void;
declare const verbose: boolean;
log("saved");
verbose && log("details");
const onSave = () => {
log("saved");
};
function onError(error: Error) {
log(error.message);
}Options#
| Option | Default | What it does |
|---|---|---|
ignoreArrowShorthand | false | Allow () => log(), an arrow function without braces. |
ignoreVoidOperator | false | Allow a void expression anywhere if it's marked with the void operator. --fix then adds void in arrow functions and returns. |
ignoreVoidReturningFunctions | false | Allow returning a void expression from a function that returns void itself. |
{ "ignoreArrowShorthand": true }No problemsdeclare function log(message: string): void;
["a", "b"].forEach(name => log(name));With ignoreVoidOperator:
{ "ignoreVoidOperator": true }No problemsdeclare function log(message: string): void;
const onSave = () => void log("saved");{ "ignoreVoidOperator": true }1 problemdeclare function log(message: string): void;
const onSave = () => log("saved")Void expressions returned from an arrow function shorthand must be marked explicitly with the void operator.;With ignoreVoidReturningFunctions, the return type can be written on the function or come from where the function is used:
{ "ignoreVoidReturningFunctions": true }No problemsdeclare function log(message: string): void;
function onError(error: Error): void {
return log(error.message);
}
["a", "b"].forEach(name => log(name));{ "ignoreVoidReturningFunctions": true }1 problemdeclare function log(message: string): void;
function onError(error: Error) {
return log(error.message)Returning a void expression from a function is forbidden. Please remove the return statement.;
}