跳转到内容

noVoidTypeReturn

Diagnostic Category: lint/correctness/noVoidTypeReturn

Since: v1.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.

class A {
f(): void {
return undefined;
}
}
code-block.ts:3:9 lint/correctness/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;
}
}
code-block.ts:3:9 lint/correctness/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;
}
code-block.ts:2:5 lint/correctness/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;
}
code-block.ts:2:5 lint/correctness/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;
};
code-block.ts:2:5 lint/correctness/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;
};
code-block.ts:2:5 lint/correctness/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.

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