Urara-Blog/node_modules/.pnpm-store/v3/files/2f/7370a46e8989bf5771de78ad9749a9bd8109df113fc16a962c97a6240e29555bb5a36304e26d60c4c607a5944b29ffad48dc164151a52726d9ead9209617aa
2022-08-14 01:14:53 +08:00

132 lines
3.5 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: 'Disallow `void` type outside of generic or return types.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-invalid-void-type** for documentation.
Disallows usage of `void` type outside of return types or generic type arguments.
If `void` is used as return type, it shouldnt be a part of intersection/union type with most other types.
## Rationale
The `void` type means “nothing” or that a function does not return any value,
in contrast with implicit `undefined` type which means that a function returns a value `undefined`.
So “nothing” cannot be mixed with any other types, other than `never`, which accepts all types.
If you need this - use the `undefined` type instead.
## Rule Details
This rule aims to ensure that the `void` type is only used in valid places.
Examples of code for this rule:
<!--tabs-->
### ❌ Incorrect
```ts
type PossibleValues = string | number | void;
type MorePossibleValues = string | ((number & any) | (string | void));
function logSomething(thing: void) {}
function printArg<T = void>(arg: T) {}
logAndReturn<void>(undefined);
interface Interface {
lambda: () => void;
prop: void;
}
class MyClass {
private readonly propName: void;
}
```
### ✅ Correct
```ts
type NoOp = () => void;
function noop(): void {}
let trulyUndefined = void 0;
async function promiseMeSomething(): Promise<void> {}
type stillVoid = void | never;
```
## Options
```ts
interface Options {
allowInGenericTypeArguments?: boolean | string[];
allowAsThisParameter?: boolean;
}
const defaultOptions: Options = {
allowInGenericTypeArguments: true,
allowAsThisParameter: false,
};
```
### `allowInGenericTypeArguments`
This option lets you control if `void` can be used as a valid value for generic type parameters.
Alternatively, you can provide an array of strings which whitelist which types may accept `void` as a generic type parameter.
Any types considered valid by this option will be considered valid as part of a union type with `void`.
This option is `true` by default.
The following patterns are considered warnings with `{ allowInGenericTypeArguments: false }`:
```ts
logAndReturn<void>(undefined);
let voidPromise: Promise<void> = new Promise<void>(() => {});
let voidMap: Map<string, void> = new Map<string, void>();
```
The following patterns are considered warnings with `{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] }`:
```ts
logAndReturn<void>(undefined);
type NotAllowedVoid1 = Mx.Tx<void>;
type NotAllowedVoid2 = Tx<void>;
type NotAllowedVoid3 = Promise<void>;
```
The following patterns are not considered warnings with `{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] }`:
```ts
type AllowedVoid = Ex.Mx.Tx<void>;
type AllowedVoidUnion = void | Ex.Mx.Tx<void>;
```
### `allowAsThisParameter`
This option allows specifying a `this` parameter of a function to be `void` when set to `true`.
This pattern can be useful to explicitly label function types that do not use a `this` argument. [See the TypeScript docs for more information](https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters-in-callbacks).
This option is `false` by default.
The following patterns are considered warnings with `{ allowAsThisParameter: false }` but valid with `{ allowAsThisParameter: true }`:
```ts
function doThing(this: void) {}
class Example {
static helper(this: void) {}
callback(this: void) {}
}
```
## When Not To Use It
If you don't care about if `void` is used with other types,
or in invalid places, then you don't need this rule.