---
url: /docs/guide/usage/linter/rules/typescript/no-invalid-void-type.md
---

### 作用

禁止在返回类型和已配置的泛型上下文之外使用 `void` 类型。

### 为什么这不好？

在 TypeScript 中，`void` 主要在返回位置上才有意义。在其他类型位置中使用 `void`
（参数、属性、别名以及大多数联合类型）通常会造成混淆，
并且往往表明类型设计有误。

### 示例

以下是此规则的**错误**代码示例：

```ts
function takeVoid(arg: void) {}
type Alias = void;
type Union = string | void;
```

以下是此规则的**正确**代码示例：

```ts
function f(): void {}
type P = Promise<void>;
type U = void | never;
```

## 配置

此规则接受一个包含以下属性的配置对象：

### allowAsThisParameter

type: `boolean`

default: `false`

是否允许函数的 `this` 参数为 `void`。

### allowInGenericTypeArguments

type: `array | boolean`

#### allowInGenericTypeArguments\[n]

type: `string`

## 如何使用

## 版本

此规则已在 v1.47.0 中添加。

## 参考
