Skip to main content

no-invalid-void-type

禁止在泛型或返回类型之外使用 void 类型.

🔒

ESLint 配置 中扩展"plugin:@typescript-eslint/strict" 可启用此规则。

TypeScript 中的 void 指的是要忽略的函数返回。尝试在返回类型或泛型类型参数之外使用 void 类型通常是程序员错误的标志。即使正确使用,void 也会误导其他开发者。

¥void in TypeScript refers to a function return that is meant to be ignored. Attempting to use a void type outside of a return type or generic type argument is often a sign of programmer error. void can also be misleading for other developers even if used correctly.

void 类型意味着不能与除 never 之外的任何其他类型混合,never 接受所有类型。如果你认为你需要这个,那么你可能想要 undefined 类型。

¥The void type means cannot be mixed with any other types, other than never, which accepts all types. If you think you need this then you probably want the undefined type instead.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-invalid-void-type": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

type PossibleValues = string | number | void;
type MorePossibleValues = string | ((number & any) | (string | void));

function logSomething(thing: void) {}
function printArg<T = void>(arg: T) {}

logAndReturn<void>(undefined);

interface Interface {
lambda: () => void;
prop: void;
}

class MyClass {
private readonly propName: void;
}
Open in Playground

选项

该规则接受以下选项:

type Options = [
{
/** 函数的 `this` 参数是否可以是 `void`。 */
allowAsThisParameter?: boolean;
/** `void` 是否可以用作泛型类型参数的有效值。 */
allowInGenericTypeArguments?: /**
* `void` 是否可以用作泛型类型参数的有效值。
* `void` 是否可以用作所有泛型类型参数的有效值。
*/
| boolean
/** 可以接受 `void` 作为泛型类型参数的类型的允许列表。 */
| [string, ...string[]];
},
];

const defaultOptions: Options = [
{ allowAsThisParameter: false, allowInGenericTypeArguments: true },
];

¥Options

allowInGenericTypeArguments

void 是否可以用作泛型类型参数的有效值。 Default: true.

或者,你可以提供一个字符串数组,其中允许列出哪些类型可以接受 void 作为泛型类型参数。

¥Alternatively, you can provide an array of strings which allowlist which types may accept void as a generic type parameter.

此选项认为有效的任何类型都将被视为与 void 联合类型的一部分。

¥Any types considered valid by this option will be considered valid as part of a union type with void.

默认情况下,此选项为 true

¥This option is true by default.

{ allowInGenericTypeArguments: false } 的情况下,以下模式被视为警告:

¥The following patterns are considered warnings with { allowInGenericTypeArguments: false }:

logAndReturn<void>(undefined);

let voidPromise: Promise<void> = new Promise<void>(() => {});
let voidMap: Map<string, void> = new Map<string, void>();
Open in Playground

{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] } 的情况下,以下模式被视为警告:

¥The following patterns are considered warnings with { allowInGenericTypeArguments: ['Ex.Mx.Tx'] }:

logAndReturn<void>(undefined);

type NotAllowedVoid1 = Mx.Tx<void>;
type NotAllowedVoid2 = Tx<void>;
type NotAllowedVoid3 = Promise<void>;
Open in Playground

{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] } 的情况下,以下模式不被视为警告:

¥The following patterns are not considered warnings with { allowInGenericTypeArguments: ['Ex.Mx.Tx'] }:

type AllowedVoid = Ex.Mx.Tx<void>;
type AllowedVoidUnion = void | Ex.Mx.Tx<void>;
Open in Playground

allowAsThisParameter

函数的 this 参数是否可以是 void。 Default: false.

此模式可用于明确标记不使用 this 参数的函数类型。请参阅 TypeScript 文档以获取更多信息

¥This pattern can be useful to explicitly label function types that do not use a this argument. See the TypeScript docs for more information.

默认情况下,此选项为 false

¥This option is false by default.

{ allowAsThisParameter: false } 的情况下,以下模式被视为警告,但在 { allowAsThisParameter: true } 的情况下有效:

¥The following patterns are considered warnings with { allowAsThisParameter: false } but valid with { allowAsThisParameter: true }:

function doThing(this: void) {}
class Example {
static helper(this: void) {}
callback(this: void) {}
}
Open in Playground

何时不使用它

¥When Not To Use It

如果你不关心 void 是否与其他类型一起使用,或者在无效的地方使用,那么你不需要此规则。

¥If you don't care about if void is used with other types, or in invalid places, then you don't need this rule.

资源