noConfusingVoidType
Summary
Section titled “Summary”- Rule available since:
v1.2.0 - Diagnostic Category:
lint/suspicious/noConfusingVoidType - This rule is recommended, which means is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "suspicious": { "noConfusingVoidType": "error" } } }}Description
Section titled “Description”Disallow void type outside of generic or return types.
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 a type parameter is often a sign of programmer error.
void can also be misleading for other developers even if used correctly.
The
voidtype means cannot be mixed with any other types, other thannever, which accepts all types. If you think you need this then you probably want theundefinedtype instead.
The code action suggests using undefined instead of void.
It is unsafe because a variable with the void type cannot be assigned to a variable with the undefined type.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”let foo: void;code-block.ts:1:10 lint/suspicious/noConfusingVoidType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ void is confusing outside a return type or a type parameter.
> 1 │ let foo: void;
│ ^^^^
2 │
ℹ Unsafe fix: Use undefined instead.
1 │ - let·foo:·void;
1 │ + let·foo:·undefined;
2 2 │
function logSomething(thing: void) {}code-block.ts:1:30 lint/suspicious/noConfusingVoidType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ void is confusing outside a return type or a type parameter.
> 1 │ function logSomething(thing: void) {}
│ ^^^^
2 │
ℹ Unsafe fix: Use undefined instead.
1 │ - function·logSomething(thing:·void)·{}
1 │ + function·logSomething(thing:·undefined)·{}
2 2 │
interface Interface { prop: void;}code-block.ts:2:11 lint/suspicious/noConfusingVoidType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ void is confusing outside a return type or a type parameter.
1 │ interface Interface {
> 2 │ prop: void;
│ ^^^^
3 │ }
4 │
ℹ Unsafe fix: Use undefined instead.
1 1 │ interface Interface {
2 │ - ····prop:·void;
2 │ + ····prop:·undefined;
3 3 │ }
4 4 │
type PossibleValues = number | void;code-block.ts:1:32 lint/suspicious/noConfusingVoidType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ void is confusing inside a union type.
> 1 │ type PossibleValues = number | void;
│ ^^^^
2 │
ℹ Unsafe fix: Use undefined instead.
1 │ - type·PossibleValues·=·number·|·void;
1 │ + type·PossibleValues·=·number·|·undefined;
2 2 │
function foo(): void {};function doSomething(this: void) {}function printArg<T = void>(arg: T) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.