consistent-return
Require
returnstatements to either always or never specify values.
💭
This rule requires type information to run.
This rule extends the base eslint/consistent-return rule.
This version adds support for functions that return void or Promise<void>.
- ❌ Incorrect
- ✅ Correct
function foo(): undefined {}
function bar(flag: boolean): undefined {
if (flag) return foo();
return;
}
async function baz(flag: boolean): Promise<undefined> {
if (flag) return;
return foo();
}
Open in Playgroundfunction foo(): void {}
function bar(flag: boolean): void {
if (flag) return foo();
return;
}
async function baz(flag: boolean): Promise<void | number> {
if (flag) return 42;
return;
}
Open in PlaygroundOptions
See eslint/consistent-return options.
How to Use
.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"consistent-return": "off",
"@typescript-eslint/consistent-return": "error"
}
};
When Not To Use It
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting. See Performance Troubleshooting if you experience performance degredations after enabling type checked rules.
Resources
Taken with ❤️ from ESLint core