typescript/no-invalid-void-type Restriction
作用
禁止在返回类型和已配置的泛型上下文之外使用 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
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/no-invalid-void-type": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/no-invalid-void-type": "error",
},
});bash
oxlint --deny typescript/no-invalid-void-type版本
此规则已在 v1.47.0 中添加。
