noConfusingVoidType (since v1.2.0)
Diagnostic Category: lint/suspicious/noConfusingVoidType
Source: no-invalid-void-type
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 generic type argument is often a sign of programmer error. void
can also be misleading for other developers even if used correctly.
The
void
type means cannot be mixed with any other types, other thannever
, which accepts all types. If you think you need this then you probably want theundefined
type instead.
Examples
Section titled ExamplesInvalid
Section titled Invalidlet foo: void;
suspicious/noConfusingVoidType.js:1:10 lint/suspicious/noConfusingVoidType ━━━━━━━━━━━━━━━━━━━━━━━━━
✖ void is only valid as a return type or a type argument in generic type
> 1 │ let foo: void;
│ ^^^^
2 │
ℹ Remove void
function logSomething(thing: void) {}
suspicious/noConfusingVoidType.js:1:30 lint/suspicious/noConfusingVoidType ━━━━━━━━━━━━━━━━━━━━━━━━━
✖ void is only valid as a return type or a type argument in generic type
> 1 │ function logSomething(thing: void) {}
│ ^^^^
2 │
ℹ Remove void
interface Interface { prop: void;}
suspicious/noConfusingVoidType.js:2:11 lint/suspicious/noConfusingVoidType ━━━━━━━━━━━━━━━━━━━━━━━━━
✖ void is only valid as a return type or a type argument in generic type
1 │ interface Interface {
> 2 │ prop: void;
│ ^^^^
3 │ }
4 │
ℹ Remove void
type PossibleValues = number | void;
suspicious/noConfusingVoidType.js:1:32 lint/suspicious/noConfusingVoidType ━━━━━━━━━━━━━━━━━━━━━━━━━
✖ void is not valid as a constituent in a union type
> 1 │ type PossibleValues = number | void;
│ ^^^^
2 │
ℹ Remove void
Valid
Section titled Validfunction foo(): void {};
function doSomething(this: void) {}
function printArg<T = void>(arg: T) {}