Skip to content

noVoidTypeReturn

¥Summary

  • 规则生效日期:v1.0.0

    ¥Rule available since: v1.0.0

  • 诊断类别:lint/correctness/noVoidTypeReturn

    ¥Diagnostic Category: lint/correctness/noVoidTypeReturn

  • 此规则为推荐规则,默认启用。

    ¥This rule is recommended, which means is enabled by default.

  • 此规则没有修复方案。

    ¥This rule doesn’t have a fix.

  • 此规则的默认严重级别为 error。

    ¥The default severity of this rule is error.

¥How to configure

biome.json
{
"linter": {
"rules": {
"correctness": {
"noVoidTypeReturn": "error"
}
}
}
}

¥Description

禁止从返回类型为 ‘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