For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/typescript-eslint/consistent-return.md.
close

consistent-return

Added in v0.1.14

Configuration

rslint.config.ts
import { defineConfig, ts } from '@rslint/core';

export default defineConfig([
  ts.configs.recommended,
  {
    rules: {
      '@typescript-eslint/consistent-return': 'error',
    },
  },
]);

Rule Details

Require return statements to either always or never specify values. This is the TypeScript-enhanced version of the ESLint consistent-return rule. It uses type information to ignore bare return statements when a synchronous function has a return type containing void, or an async function returns a thenable whose resolved type contains void. This includes number | void and Promise<number | void>. A return type of undefined alone does not grant this exemption.

The first checked return determines whether later returns must specify a value. The rule reports each conflicting return statement and reachable function endings that omit a required value. With treatUndefinedAsUnspecified: true, undefined identifiers and void expressions count as unspecified values, as do expressions whose type is exactly undefined. A union type containing undefined does not by itself make an expression unspecified.

Examples of incorrect code for this rule:

function foo(flag: boolean): string | undefined {
  if (flag) {
    return 'hello';
  }
  return;
}

function bar(x: number) {
  if (x > 0) {
    return x;
  }
  return;
}

Examples of correct code for this rule:

function foo(flag: boolean): string | undefined {
  if (flag) {
    return 'hello';
  }
  return undefined;
}

function bar(): void {
  if (Math.random() > 0.5) {
    return;
  }
  console.log('done');
}

Original Documentation