@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: voidvoid is only valid as a return type or generic type argument.;
function log(message: string | voidvoid is not valid as a constituent in a union type) {}
type Listener = (event: voidvoid is only valid as a return type or generic type argument.) => void;example.tsNo problems
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. |
example.tswith
{ "allowInGenericTypeArguments": false }1 problemasync function save(): Promise<voidvoid is only valid as a return type.> {}With a list, void is allowed as an argument of the types in it:
example.tswith
{ "allowInGenericTypeArguments": ["Promise"] }No problemsasync function save(): Promise<void> {}example.tswith
{ "allowInGenericTypeArguments": ["Promise"] }1 problemconst results: Array<voidArray may not have void as a type argument.> = [];this: void is reported unless you turn on allowAsThisParameter:
example.ts1 problem
function now(this: voidvoid is only valid as a return type or generic type argument.) {
return Date.now();
}example.tswith
{ "allowAsThisParameter": true }No problemsfunction now(this: void) {
return Date.now();
}