Lint Rules

noVoidTypeReturn (since v11.0.0)

Disallow returning a value from a function with the return type ‘void’

‘void’ signals the absence of value. The returned value is likely to be ignored by the caller. Thus, returning a value when the return type of function is ‘void’, is undoubtedly an error.

Only returning without a value is allowed, as it’s a control flow statement.

Examples

Invalid

class A {
    f(): void {
        return undefined;
    }
}
nursery/noVoidTypeReturn.js:3:9 lint/nursery/noVoidTypeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The function should not return a value because its return type is void.
  
    1 │ class A {
    2 │     f(): void {
  > 3 │         return undefined;
           ^^^^^^^^^^^^^^^^^
    4 │     }
    5 │ }
  
   The function is here:
  
    1 │ class A {
  > 2 │     f(): void {
       ^^^^^^^^^^^
  > 3 │         return undefined;
  > 4 │     }
       ^
    5 │ }
    6 │ 
  
   'void' signals the absence of value. The returned value is likely to be ignored by the caller.
  
const a = {
    f(): void {
        return undefined;
    }
}
nursery/noVoidTypeReturn.js:3:9 lint/nursery/noVoidTypeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The function should not return a value because its return type is void.
  
    1 │ const a = {
    2 │     f(): void {
  > 3 │         return undefined;
           ^^^^^^^^^^^^^^^^^
    4 │     }
    5 │ }
  
   The function is here:
  
    1 │ const a = {
  > 2 │     f(): void {
       ^^^^^^^^^^^
  > 3 │         return undefined;
  > 4 │     }
       ^
    5 │ }
    6 │ 
  
   'void' signals the absence of value. The returned value is likely to be ignored by the caller.
  
function f(): void {
    return undefined;
}
nursery/noVoidTypeReturn.js:2:5 lint/nursery/noVoidTypeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The function should not return a value because its return type is void.
  
    1 │ function f(): void {
  > 2 │     return undefined;
       ^^^^^^^^^^^^^^^^^
    3 │ }
    4 │ 
  
   The function is here:
  
  > 1 │ function f(): void {
   ^^^^^^^^^^^^^^^^^^^^
  > 2 │     return undefined;
  > 3 │ }
   ^
    4 │ 
  
   'void' signals the absence of value. The returned value is likely to be ignored by the caller.
  
export default function(): void {
    return undefined;
}
nursery/noVoidTypeReturn.js:2:5 lint/nursery/noVoidTypeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The function should not return a value because its return type is void.
  
    1 │ export default function(): void {
  > 2 │     return undefined;
       ^^^^^^^^^^^^^^^^^
    3 │ }
    4 │ 
  
   The function is here:
  
  > 1 │ export default function(): void {
                  ^^^^^^^^^^^^^^^^^^
  > 2 │     return undefined;
  > 3 │ }
   ^
    4 │ 
  
   'void' signals the absence of value. The returned value is likely to be ignored by the caller.
  
const g = (): void => {
    return undefined;
};
nursery/noVoidTypeReturn.js:2:5 lint/nursery/noVoidTypeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The function should not return a value because its return type is void.
  
    1 │ const g = (): void => {
  > 2 │     return undefined;
       ^^^^^^^^^^^^^^^^^
    3 │ };
    4 │ 
  
   The function is here:
  
  > 1 │ const g = (): void => {
             ^^^^^^^^^^^^^
  > 2 │     return undefined;
  > 3 │ };
   ^
    4 │ 
  
   'void' signals the absence of value. The returned value is likely to be ignored by the caller.
  
const h = function(): void {
    return undefined;
};
nursery/noVoidTypeReturn.js:2:5 lint/nursery/noVoidTypeReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The function should not return a value because its return type is void.
  
    1 │ const h = function(): void {
  > 2 │     return undefined;
       ^^^^^^^^^^^^^^^^^
    3 │ };
    4 │ 
  
   The function is here:
  
  > 1 │ const h = function(): void {
             ^^^^^^^^^^^^^^^^^^
  > 2 │     return undefined;
  > 3 │ };
   ^
    4 │ 
  
   'void' signals the absence of value. The returned value is likely to be ignored by the caller.
  

Valid

class A {
    f() {
        return undefined;
    }
}
class B {
    f(): void {}
}
function f(): void {
    return;
}