Skip to main content

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>.

warning

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.

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 Playground

Options

See eslint/consistent-return's 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"
}
};

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 Troubleshooting > Linting with Type Information > Performance if you experience performance degredations after enabling type checked rules.

Resources

Taken with ❤️ from ESLint core.