consistent-return
Require
return
statements 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>
.
If possible, it is recommended to use tsconfig's noImplicitReturns
option rather than this rule. noImplicitReturns
is powered by TS's type information and control-flow analysis so it has better coverage than this rule.
- ❌ 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
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"consistent-return": "off",
"@typescript-eslint/consistent-return": "error"
}
};
Try this rule in the playground ↗
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.