noConfusingVoidType
このコンテンツはまだ日本語訳がありません。
Diagnostic Category: lint/suspicious/noConfusingVoidType
Since: v1.2.0
Sources:
Description
Section titled DescriptionDisallow 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 asigned to a variable with the undefined type.
Examples
Section titled ExamplesInvalid
Section titled Invalidlet 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 │
Valid
Section titled Validfunction foo(): void {};function doSomething(this: void) {}function printArg<T = void>(arg: T) {}How to configure
Section titled How to configure{ "linter": { "rules": { "suspicious": { "noConfusingVoidType": "error" } } }}