Skip to content

noVoidTypeReturn

诊断类别:lint/correctness/noVoidTypeReturn

¥Diagnostic Category: lint/correctness/noVoidTypeReturn

自从:v1.0.0

¥Since: v1.0.0

禁止从返回类型为 ‘void’ 的函数返回值

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

‘void’ 表示没有值。调用者可能会忽略返回值。因此,当函数的返回类型为 ‘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;
}
}
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.

¥Valid

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

¥Related links