Skip to main content


Disallow void type outside of generic or return types.


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 类型。

module.exports = {
"rules": {
"@typescript-eslint/no-invalid-void-type": "error"
在线运行试试这个规则 ↗


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

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


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

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



此选项允许你控制 void 是否可以用作泛型类型参数的有效值。

英:This option lets you control if void can be used as a valid value for generic type parameters.

或者,你可以提供一个字符串数组,将哪些类型列入白名单,可以接受 void 作为泛型类型参数。

英:Alternatively, you can provide an array of strings which whitelist 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 }:


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'] }:


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


当设置为 true 时,此选项允许将函数的 this 参数指定为 void。 此模式对于显式标记不使用 this 参数的函数类型很有用。 请参阅 TypeScript 文档以获取更多信息

英:This option allows specifying a this parameter of a function to be void when set to true. 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


如果你不关心 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.



type Options = [
allowAsThisParameter?: boolean;
allowInGenericTypeArguments?: [string, ...string[]] | boolean;

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