Aller au contenu

noConfusingVoidType

Ce contenu n’est pas encore disponible dans votre langue.

Diagnostic Category: lint/suspicious/noConfusingVoidType

Since: v1.2.0

Sources:

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 void type means cannot be mixed with any other types, other than never, which accepts all types. If you think you need this then you probably want the undefined type instead.

The code action suggests using undefined instead of void. It is unsafe because a variable with the void type cannot be asigned to a variable with the undefined type.

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) {}