@typescript-eslint/no-invalid-void-type

Disallow void outside of return types and generic type arguments

Also called typescript-eslint/no-invalid-void-type, typescript/no-invalid-void-type

void says that the caller should ignore what a function returns. Anywhere else it's confusing, because a variable or a parameter of type void can only hold undefined. Write undefined there, or make the parameter optional.

void is fine as a return type and as a type argument, such as Promise<void>. A union is fine if every member is void, never or a generic type with void as an argument.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-invalid-void-type" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-invalid-void-type" = ["error", {
  allowInGenericTypeArguments = true,
  allowAsThisParameter = false,
}]

Examples#

example.ts3 problems
let result: void;

function log(message: string | void) {}

type Listener = (event: void) => void;
example.tsNo problems
let result: undefined;

function log(message?: string): void {}

async function save(): Promise<void> {}

type MaybeAsync = void | Promise<void>;

Options#

OptionDefaultWhat it does
allowInGenericTypeArgumentstruefalse reports void as a type argument. A list of names allows it only for those types.
allowAsThisParameterfalseAllow this: void, which says that a function doesn't use this.
example.tswith { "allowInGenericTypeArguments": false }1 problem
async function save(): Promise<void> {}

With a list, void is allowed as an argument of the types in it:

example.tswith { "allowInGenericTypeArguments": ["Promise"] }No problems
async function save(): Promise<void> {}
example.tswith { "allowInGenericTypeArguments": ["Promise"] }1 problem
const results: Array<void> = [];

this: void is reported unless you turn on allowAsThisParameter:

example.ts1 problem
function now(this: void) {
  return Date.now();
}
example.tswith { "allowAsThisParameter": true }No problems
function now(this: void) {
  return Date.now();
}