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
Reported:
let result: void;
function log(message: string | void) {}
type Listener = (event: void) => void;
Not reported:
let result: undefined;
function log(message?: string): void {}
async function save(): Promise<void> {}
type MaybeAsync = void | Promise<void>;
Options
| Option | Default | What it does |
|---|---|---|
allowInGenericTypeArguments | true | false reports void as a type argument. A list of names allows it only for those types. |
allowAsThisParameter | false | Allow this: void, which says that a function doesn't use this. |
async function save(): Promise<void> {}
With a list, void is allowed as an argument of the types in it:
async function save(): Promise<void> {}
const results: Array<void> = [];
this: void is reported unless you turn on allowAsThisParameter:
function now(this: void) {
return Date.now();
}
function now(this: void) {
return Date.now();
}