@typescript-eslint/eslint-plugin 8.33.1-alpha.5 → 8.33.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -10
- package/docs/rules/README.md +0 -57
- package/docs/rules/TEMPLATE.md +0 -36
- package/docs/rules/adjacent-overload-signatures.mdx +0 -105
- package/docs/rules/array-type.mdx +0 -126
- package/docs/rules/await-thenable.mdx +0 -184
- package/docs/rules/ban-ts-comment.mdx +0 -165
- package/docs/rules/ban-tslint-comment.mdx +0 -45
- package/docs/rules/ban-types.md +0 -26
- package/docs/rules/block-spacing.md +0 -15
- package/docs/rules/brace-style.md +0 -15
- package/docs/rules/camelcase.md +0 -15
- package/docs/rules/class-literal-property-style.mdx +0 -112
- package/docs/rules/class-methods-use-this.mdx +0 -135
- package/docs/rules/comma-dangle.md +0 -15
- package/docs/rules/comma-spacing.md +0 -15
- package/docs/rules/consistent-generic-constructors.mdx +0 -87
- package/docs/rules/consistent-indexed-object-style.mdx +0 -105
- package/docs/rules/consistent-return.mdx +0 -51
- package/docs/rules/consistent-type-assertions.mdx +0 -196
- package/docs/rules/consistent-type-definitions.mdx +0 -133
- package/docs/rules/consistent-type-exports.mdx +0 -97
- package/docs/rules/consistent-type-imports.mdx +0 -139
- package/docs/rules/default-param-last.mdx +0 -59
- package/docs/rules/dot-notation.mdx +0 -94
- package/docs/rules/explicit-function-return-type.mdx +0 -359
- package/docs/rules/explicit-member-accessibility.mdx +0 -353
- package/docs/rules/explicit-module-boundary-types.mdx +0 -287
- package/docs/rules/func-call-spacing.md +0 -15
- package/docs/rules/indent.md +0 -15
- package/docs/rules/init-declarations.mdx +0 -12
- package/docs/rules/key-spacing.md +0 -15
- package/docs/rules/keyword-spacing.md +0 -15
- package/docs/rules/lines-around-comment.md +0 -15
- package/docs/rules/lines-between-class-members.md +0 -15
- package/docs/rules/max-params.mdx +0 -54
- package/docs/rules/member-delimiter-style.md +0 -15
- package/docs/rules/member-ordering.mdx +0 -1483
- package/docs/rules/method-signature-style.mdx +0 -124
- package/docs/rules/naming-convention.mdx +0 -755
- package/docs/rules/no-array-constructor.mdx +0 -34
- package/docs/rules/no-array-delete.mdx +0 -44
- package/docs/rules/no-base-to-string.mdx +0 -115
- package/docs/rules/no-confusing-non-null-assertion.mdx +0 -75
- package/docs/rules/no-confusing-void-expression.mdx +0 -148
- package/docs/rules/no-deprecated.mdx +0 -119
- package/docs/rules/no-dupe-class-members.mdx +0 -16
- package/docs/rules/no-duplicate-enum-values.mdx +0 -66
- package/docs/rules/no-duplicate-imports.mdx +0 -17
- package/docs/rules/no-duplicate-type-constituents.mdx +0 -89
- package/docs/rules/no-dynamic-delete.mdx +0 -64
- package/docs/rules/no-empty-function.mdx +0 -94
- package/docs/rules/no-empty-interface.mdx +0 -75
- package/docs/rules/no-empty-object-type.mdx +0 -150
- package/docs/rules/no-explicit-any.mdx +0 -177
- package/docs/rules/no-extra-non-null-assertion.mdx +0 -60
- package/docs/rules/no-extra-parens.md +0 -15
- package/docs/rules/no-extra-semi.md +0 -15
- package/docs/rules/no-extraneous-class.mdx +0 -329
- package/docs/rules/no-floating-promises.mdx +0 -282
- package/docs/rules/no-for-in-array.mdx +0 -67
- package/docs/rules/no-implied-eval.mdx +0 -106
- package/docs/rules/no-import-type-side-effects.mdx +0 -80
- package/docs/rules/no-inferrable-types.mdx +0 -113
- package/docs/rules/no-invalid-this.mdx +0 -16
- package/docs/rules/no-invalid-void-type.mdx +0 -119
- package/docs/rules/no-loop-func.mdx +0 -12
- package/docs/rules/no-loss-of-precision.mdx +0 -17
- package/docs/rules/no-magic-numbers.mdx +0 -131
- package/docs/rules/no-meaningless-void-operator.mdx +0 -61
- package/docs/rules/no-misused-new.mdx +0 -53
- package/docs/rules/no-misused-promises.mdx +0 -314
- package/docs/rules/no-misused-spread.mdx +0 -132
- package/docs/rules/no-mixed-enums.mdx +0 -96
- package/docs/rules/no-namespace.mdx +0 -157
- package/docs/rules/no-non-null-asserted-nullish-coalescing.mdx +0 -60
- package/docs/rules/no-non-null-asserted-optional-chain.mdx +0 -46
- package/docs/rules/no-non-null-assertion.mdx +0 -48
- package/docs/rules/no-parameter-properties.mdx +0 -16
- package/docs/rules/no-redeclare.mdx +0 -79
- package/docs/rules/no-redundant-type-constituents.mdx +0 -102
- package/docs/rules/no-require-imports.mdx +0 -114
- package/docs/rules/no-restricted-imports.mdx +0 -84
- package/docs/rules/no-restricted-types.mdx +0 -70
- package/docs/rules/no-shadow.mdx +0 -143
- package/docs/rules/no-this-alias.mdx +0 -124
- package/docs/rules/no-type-alias.mdx +0 -626
- package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +0 -165
- package/docs/rules/no-unnecessary-condition.mdx +0 -293
- package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +0 -42
- package/docs/rules/no-unnecessary-qualifier.mdx +0 -57
- package/docs/rules/no-unnecessary-template-expression.mdx +0 -108
- package/docs/rules/no-unnecessary-type-arguments.mdx +0 -85
- package/docs/rules/no-unnecessary-type-assertion.mdx +0 -97
- package/docs/rules/no-unnecessary-type-constraint.mdx +0 -61
- package/docs/rules/no-unnecessary-type-conversion.mdx +0 -79
- package/docs/rules/no-unnecessary-type-parameters.mdx +0 -255
- package/docs/rules/no-unsafe-argument.mdx +0 -98
- package/docs/rules/no-unsafe-assignment.mdx +0 -101
- package/docs/rules/no-unsafe-call.mdx +0 -120
- package/docs/rules/no-unsafe-declaration-merging.mdx +0 -65
- package/docs/rules/no-unsafe-enum-comparison.mdx +0 -98
- package/docs/rules/no-unsafe-function-type.mdx +0 -65
- package/docs/rules/no-unsafe-member-access.mdx +0 -81
- package/docs/rules/no-unsafe-return.mdx +0 -126
- package/docs/rules/no-unsafe-type-assertion.mdx +0 -63
- package/docs/rules/no-unsafe-unary-minus.mdx +0 -60
- package/docs/rules/no-unused-expressions.mdx +0 -52
- package/docs/rules/no-unused-vars.mdx +0 -136
- package/docs/rules/no-use-before-define.mdx +0 -98
- package/docs/rules/no-useless-constructor.mdx +0 -21
- package/docs/rules/no-useless-empty-export.mdx +0 -53
- package/docs/rules/no-useless-template-literals.mdx +0 -9
- package/docs/rules/no-var-requires.mdx +0 -77
- package/docs/rules/no-wrapper-object-types.mdx +0 -75
- package/docs/rules/non-nullable-type-assertion-style.mdx +0 -47
- package/docs/rules/object-curly-spacing.md +0 -15
- package/docs/rules/only-throw-error.mdx +0 -150
- package/docs/rules/padding-line-between-statements.md +0 -15
- package/docs/rules/parameter-properties.mdx +0 -522
- package/docs/rules/prefer-as-const.mdx +0 -51
- package/docs/rules/prefer-destructuring.mdx +0 -101
- package/docs/rules/prefer-enum-initializers.mdx +0 -68
- package/docs/rules/prefer-find.mdx +0 -45
- package/docs/rules/prefer-for-of.mdx +0 -50
- package/docs/rules/prefer-function-type.mdx +0 -98
- package/docs/rules/prefer-includes.mdx +0 -81
- package/docs/rules/prefer-literal-enum-member.mdx +0 -111
- package/docs/rules/prefer-namespace-keyword.mdx +0 -51
- package/docs/rules/prefer-nullish-coalescing.mdx +0 -349
- package/docs/rules/prefer-optional-chain.mdx +0 -304
- package/docs/rules/prefer-promise-reject-errors.mdx +0 -78
- package/docs/rules/prefer-readonly-parameter-types.mdx +0 -408
- package/docs/rules/prefer-readonly.mdx +0 -111
- package/docs/rules/prefer-reduce-type-parameter.mdx +0 -66
- package/docs/rules/prefer-regexp-exec.mdx +0 -52
- package/docs/rules/prefer-return-this-type.mdx +0 -93
- package/docs/rules/prefer-string-starts-ends-with.mdx +0 -84
- package/docs/rules/prefer-ts-expect-error.mdx +0 -86
- package/docs/rules/promise-function-async.mdx +0 -143
- package/docs/rules/quotes.md +0 -15
- package/docs/rules/related-getter-setter-pairs.mdx +0 -61
- package/docs/rules/require-array-sort-compare.mdx +0 -89
- package/docs/rules/require-await.mdx +0 -53
- package/docs/rules/restrict-plus-operands.mdx +0 -245
- package/docs/rules/restrict-template-expressions.mdx +0 -167
- package/docs/rules/return-await.mdx +0 -339
- package/docs/rules/semi.md +0 -15
- package/docs/rules/sort-type-constituents.mdx +0 -209
- package/docs/rules/sort-type-union-intersection-members.mdx +0 -16
- package/docs/rules/space-before-blocks.md +0 -15
- package/docs/rules/space-before-function-paren.md +0 -15
- package/docs/rules/space-infix-ops.md +0 -15
- package/docs/rules/strict-boolean-expressions.mdx +0 -184
- package/docs/rules/switch-exhaustiveness-check.mdx +0 -280
- package/docs/rules/triple-slash-reference.mdx +0 -129
- package/docs/rules/type-annotation-spacing.md +0 -15
- package/docs/rules/typedef.mdx +0 -350
- package/docs/rules/unbound-method.mdx +0 -114
- package/docs/rules/unified-signatures.mdx +0 -132
- package/docs/rules/use-unknown-in-catch-callback-variable.mdx +0 -97
@@ -1,101 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow assigning a value with type `any` to variables and properties.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unsafe-assignment** for documentation.
|
11
|
-
|
12
|
-
The `any` type in TypeScript is a dangerous "escape hatch" from the type system.
|
13
|
-
Using `any` disables many type checking rules and is generally best used only as a last resort or when prototyping code.
|
14
|
-
|
15
|
-
Despite your best intentions, the `any` type can sometimes leak into your codebase.
|
16
|
-
Assigning an `any` typed value to a variable can be hard to pick up on, particularly if it leaks in from an external library.
|
17
|
-
|
18
|
-
This rule disallows assigning `any` to a variable, and assigning `any[]` to an array destructuring.
|
19
|
-
|
20
|
-
This rule also compares generic type argument types to ensure you don't pass an unsafe `any` in a generic position to a receiver that's expecting a specific type.
|
21
|
-
For example, it will error if you assign `Set<any>` to a variable declared as `Set<string>`.
|
22
|
-
|
23
|
-
## Examples
|
24
|
-
|
25
|
-
<Tabs>
|
26
|
-
<TabItem value="❌ Incorrect">
|
27
|
-
|
28
|
-
```ts
|
29
|
-
const x = 1 as any,
|
30
|
-
y = 1 as any;
|
31
|
-
const [x] = 1 as any;
|
32
|
-
const [x] = [] as any[];
|
33
|
-
const [x] = [1 as any];
|
34
|
-
[x] = [1] as [any];
|
35
|
-
|
36
|
-
function foo(a = 1 as any) {}
|
37
|
-
class Foo {
|
38
|
-
constructor(private a = 1 as any) {}
|
39
|
-
}
|
40
|
-
class Foo {
|
41
|
-
private a = 1 as any;
|
42
|
-
}
|
43
|
-
|
44
|
-
// generic position examples
|
45
|
-
const x: Set<string> = new Set<any>();
|
46
|
-
const x: Map<string, string> = new Map<string, any>();
|
47
|
-
const x: Set<string[]> = new Set<any[]>();
|
48
|
-
const x: Set<Set<Set<string>>> = new Set<Set<Set<any>>>();
|
49
|
-
```
|
50
|
-
|
51
|
-
</TabItem>
|
52
|
-
<TabItem value="✅ Correct">
|
53
|
-
|
54
|
-
```ts
|
55
|
-
const x = 1,
|
56
|
-
y = 1;
|
57
|
-
const [x] = [1];
|
58
|
-
[x] = [1] as [number];
|
59
|
-
|
60
|
-
function foo(a = 1) {}
|
61
|
-
class Foo {
|
62
|
-
constructor(private a = 1) {}
|
63
|
-
}
|
64
|
-
class Foo {
|
65
|
-
private a = 1;
|
66
|
-
}
|
67
|
-
|
68
|
-
// generic position examples
|
69
|
-
const x: Set<string> = new Set<string>();
|
70
|
-
const x: Map<string, string> = new Map<string, string>();
|
71
|
-
const x: Set<string[]> = new Set<string[]>();
|
72
|
-
const x: Set<Set<Set<string>>> = new Set<Set<Set<string>>>();
|
73
|
-
```
|
74
|
-
|
75
|
-
</TabItem>
|
76
|
-
</Tabs>
|
77
|
-
|
78
|
-
There are cases where the rule allows assignment of `any` to `unknown`.
|
79
|
-
|
80
|
-
Example of `any` to `unknown` assignment that are allowed:
|
81
|
-
|
82
|
-
```ts showPlaygroundButton
|
83
|
-
const x: unknown = y as any;
|
84
|
-
const x: unknown[] = y as any[];
|
85
|
-
const x: Set<unknown> = y as Set<any>;
|
86
|
-
```
|
87
|
-
|
88
|
-
## When Not To Use It
|
89
|
-
|
90
|
-
If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.
|
91
|
-
It may be easier to skip the `no-unsafe-*` rules pending increasing type safety in unsafe areas of your project.
|
92
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
93
|
-
|
94
|
-
## Related To
|
95
|
-
|
96
|
-
- [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
|
97
|
-
- [`no-explicit-any`](./no-explicit-any.mdx)
|
98
|
-
- [`no-unsafe-argument`](./no-unsafe-argument.mdx)
|
99
|
-
- [`no-unsafe-call`](./no-unsafe-call.mdx)
|
100
|
-
- [`no-unsafe-member-access`](./no-unsafe-member-access.mdx)
|
101
|
-
- [`no-unsafe-return`](./no-unsafe-return.mdx)
|
@@ -1,120 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow calling a value with type `any`.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unsafe-call** for documentation.
|
11
|
-
|
12
|
-
The `any` type in TypeScript is a dangerous "escape hatch" from the type system.
|
13
|
-
Using `any` disables many type checking rules and is generally best used only as a last resort or when prototyping code.
|
14
|
-
|
15
|
-
Despite your best intentions, the `any` type can sometimes leak into your codebase.
|
16
|
-
Calling an `any`-typed value as a function creates a potential type safety hole and source of bugs in your codebase.
|
17
|
-
|
18
|
-
This rule disallows calling any value that is typed as `any`.
|
19
|
-
|
20
|
-
## Examples
|
21
|
-
|
22
|
-
<Tabs>
|
23
|
-
<TabItem value="❌ Incorrect">
|
24
|
-
|
25
|
-
```ts
|
26
|
-
declare const anyVar: any;
|
27
|
-
declare const nestedAny: { prop: any };
|
28
|
-
|
29
|
-
anyVar();
|
30
|
-
anyVar.a.b();
|
31
|
-
|
32
|
-
nestedAny.prop();
|
33
|
-
nestedAny.prop['a']();
|
34
|
-
|
35
|
-
new anyVar();
|
36
|
-
new nestedAny.prop();
|
37
|
-
|
38
|
-
anyVar`foo`;
|
39
|
-
nestedAny.prop`foo`;
|
40
|
-
```
|
41
|
-
|
42
|
-
</TabItem>
|
43
|
-
<TabItem value="✅ Correct">
|
44
|
-
|
45
|
-
```ts
|
46
|
-
declare const typedVar: () => void;
|
47
|
-
declare const typedNested: { prop: { a: () => void } };
|
48
|
-
|
49
|
-
typedVar();
|
50
|
-
typedNested.prop.a();
|
51
|
-
|
52
|
-
(() => {})();
|
53
|
-
|
54
|
-
new Map();
|
55
|
-
|
56
|
-
String.raw`foo`;
|
57
|
-
```
|
58
|
-
|
59
|
-
</TabItem>
|
60
|
-
</Tabs>
|
61
|
-
|
62
|
-
## The Unsafe `Function` Type
|
63
|
-
|
64
|
-
The `Function` type is behaves almost identically to `any` when called, so this rule also disallows calling values of type `Function`.
|
65
|
-
|
66
|
-
<Tabs>
|
67
|
-
<TabItem value="❌ Incorrect">
|
68
|
-
|
69
|
-
```ts
|
70
|
-
const f: Function = () => {};
|
71
|
-
f();
|
72
|
-
```
|
73
|
-
|
74
|
-
</TabItem>
|
75
|
-
</Tabs>
|
76
|
-
|
77
|
-
Note that whereas [no-unsafe-function-type](./no-unsafe-function-type.mdx) helps prevent the _creation_ of `Function` types, this rule helps prevent the unsafe _use_ of `Function` types, which may creep into your codebase without explicitly referencing the `Function` type at all.
|
78
|
-
See, for example, the following code:
|
79
|
-
|
80
|
-
```ts
|
81
|
-
function callUnsafe(maybeFunction: unknown): string {
|
82
|
-
if (typeof maybeFunction === 'function') {
|
83
|
-
// TypeScript allows this, but it's completely unsound.
|
84
|
-
return maybeFunction('call', 'with', 'any', 'args');
|
85
|
-
}
|
86
|
-
// etc
|
87
|
-
}
|
88
|
-
```
|
89
|
-
|
90
|
-
In this sort of situation, beware that there is no way to guarantee with runtime checks that a value is safe to call.
|
91
|
-
If you _really_ want to call a value whose type you don't know, your best best is to use a `try`/`catch` and suppress any TypeScript or linter errors that get in your way.
|
92
|
-
|
93
|
-
```ts
|
94
|
-
function callSafe(maybeFunction: unknown): void {
|
95
|
-
try {
|
96
|
-
// intentionally unsound type assertion
|
97
|
-
(maybeFunction as () => unknown)();
|
98
|
-
} catch (e) {
|
99
|
-
console.error(
|
100
|
-
'Function either could not be called or threw an error when called: ',
|
101
|
-
e,
|
102
|
-
);
|
103
|
-
}
|
104
|
-
}
|
105
|
-
```
|
106
|
-
|
107
|
-
## When Not To Use It
|
108
|
-
|
109
|
-
If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.
|
110
|
-
It may be easier to skip the `no-unsafe-*` rules pending increasing type safety in unsafe areas of your project.
|
111
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
112
|
-
|
113
|
-
## Related To
|
114
|
-
|
115
|
-
- [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
|
116
|
-
- [`no-explicit-any`](./no-explicit-any.mdx)
|
117
|
-
- [`no-unsafe-argument`](./no-unsafe-argument.mdx)
|
118
|
-
- [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
|
119
|
-
- [`no-unsafe-member-access`](./no-unsafe-member-access.mdx)
|
120
|
-
- [`no-unsafe-return`](./no-unsafe-return.mdx)
|
@@ -1,65 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unsafe declaration merging.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unsafe-declaration-merging** for documentation.
|
11
|
-
|
12
|
-
TypeScript's "declaration merging" supports merging separate declarations with the same name.
|
13
|
-
|
14
|
-
Declaration merging between classes and interfaces is unsafe.
|
15
|
-
The TypeScript compiler doesn't check whether properties are initialized, which can lead to TypeScript not detecting code that will cause runtime errors.
|
16
|
-
|
17
|
-
```ts
|
18
|
-
interface Foo {
|
19
|
-
nums: number[];
|
20
|
-
}
|
21
|
-
|
22
|
-
class Foo {}
|
23
|
-
|
24
|
-
const foo = new Foo();
|
25
|
-
|
26
|
-
foo.nums.push(1); // Runtime Error: Cannot read properties of undefined.
|
27
|
-
```
|
28
|
-
|
29
|
-
## Examples
|
30
|
-
|
31
|
-
<Tabs>
|
32
|
-
<TabItem value="❌ Incorrect">
|
33
|
-
|
34
|
-
```ts
|
35
|
-
interface Foo {}
|
36
|
-
|
37
|
-
class Foo {}
|
38
|
-
```
|
39
|
-
|
40
|
-
</TabItem>
|
41
|
-
<TabItem value="✅ Correct">
|
42
|
-
|
43
|
-
```ts
|
44
|
-
interface Foo {}
|
45
|
-
class Bar implements Foo {}
|
46
|
-
|
47
|
-
namespace Baz {}
|
48
|
-
namespace Baz {}
|
49
|
-
enum Baz {}
|
50
|
-
|
51
|
-
namespace Qux {}
|
52
|
-
function Qux() {}
|
53
|
-
```
|
54
|
-
|
55
|
-
</TabItem>
|
56
|
-
</Tabs>
|
57
|
-
|
58
|
-
## When Not To Use It
|
59
|
-
|
60
|
-
If your project intentionally defines classes and interfaces with unsafe declaration merging patterns, this rule might not be for you.
|
61
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
62
|
-
|
63
|
-
## Further Reading
|
64
|
-
|
65
|
-
- [Declaration Merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)
|
@@ -1,98 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow comparing an enum value with a non-enum value.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unsafe-enum-comparison** for documentation.
|
11
|
-
|
12
|
-
The TypeScript compiler can be surprisingly lenient when working with enums.
|
13
|
-
While overt safety problems with enums were [resolved in TypeScript 5.0](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#all-enums-are-union-enums), some logical pitfalls remain permitted.
|
14
|
-
For example, it is allowed to compare enum values against non-enum values:
|
15
|
-
|
16
|
-
```ts
|
17
|
-
enum Vegetable {
|
18
|
-
Asparagus = 'asparagus',
|
19
|
-
}
|
20
|
-
|
21
|
-
declare const vegetable: Vegetable;
|
22
|
-
|
23
|
-
vegetable === 'asparagus'; // No error
|
24
|
-
```
|
25
|
-
|
26
|
-
The above code snippet should instead be written as `vegetable === Vegetable.Asparagus`.
|
27
|
-
Allowing non-enums in comparisons subverts the point of using enums in the first place.
|
28
|
-
By enforcing comparisons with properly typed enums:
|
29
|
-
|
30
|
-
- It makes a codebase more resilient to enum members changing values.
|
31
|
-
- It allows for code IDEs to use the "Rename Symbol" feature to quickly rename an enum.
|
32
|
-
- It aligns code to the proper enum semantics of referring to them by name and treating their values as implementation details.
|
33
|
-
|
34
|
-
## Examples
|
35
|
-
|
36
|
-
<Tabs>
|
37
|
-
<TabItem value="❌ Incorrect">
|
38
|
-
|
39
|
-
```ts
|
40
|
-
enum Fruit {
|
41
|
-
Apple,
|
42
|
-
}
|
43
|
-
|
44
|
-
declare let fruit: Fruit;
|
45
|
-
|
46
|
-
// bad - comparison between enum and explicit value instead of named enum member
|
47
|
-
fruit === 0;
|
48
|
-
|
49
|
-
enum Vegetable {
|
50
|
-
Asparagus = 'asparagus',
|
51
|
-
}
|
52
|
-
|
53
|
-
declare let vegetable: Vegetable;
|
54
|
-
|
55
|
-
// bad - comparison between enum and explicit value instead of named enum member
|
56
|
-
vegetable === 'asparagus';
|
57
|
-
|
58
|
-
declare let anyString: string;
|
59
|
-
|
60
|
-
// bad - comparison between enum and non-enum value
|
61
|
-
anyString === Vegetable.Asparagus;
|
62
|
-
```
|
63
|
-
|
64
|
-
</TabItem>
|
65
|
-
<TabItem value="✅ Correct">
|
66
|
-
|
67
|
-
```ts
|
68
|
-
enum Fruit {
|
69
|
-
Apple,
|
70
|
-
}
|
71
|
-
|
72
|
-
declare let fruit: Fruit;
|
73
|
-
|
74
|
-
fruit === Fruit.Apple;
|
75
|
-
|
76
|
-
enum Vegetable {
|
77
|
-
Asparagus = 'asparagus',
|
78
|
-
}
|
79
|
-
|
80
|
-
declare let vegetable: Vegetable;
|
81
|
-
|
82
|
-
vegetable === Vegetable.Asparagus;
|
83
|
-
```
|
84
|
-
|
85
|
-
</TabItem>
|
86
|
-
</Tabs>
|
87
|
-
|
88
|
-
## When Not To Use It
|
89
|
-
|
90
|
-
If you don't mind enums being treated as a namespaced bag of values, rather than opaque identifiers, you likely don't need this rule.
|
91
|
-
|
92
|
-
Sometimes, you may want to ingest a value from an API or user input, then use it as an enum throughout your application.
|
93
|
-
While validating the input, it may be appropriate to disable the rule.
|
94
|
-
Alternately, you might consider making use of a validation library like [Zod](https://zod.dev/?id=native-enums).
|
95
|
-
See further discussion of this topic in [#8557](https://github.com/typescript-eslint/typescript-eslint/issues/8557).
|
96
|
-
|
97
|
-
Finally, in the rare case of relying on an third party enums that are only imported as `type`s, it may be difficult to adhere to this rule.
|
98
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
@@ -1,65 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow using the unsafe built-in Function type.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unsafe-function-type** for documentation.
|
11
|
-
|
12
|
-
TypeScript's built-in `Function` type allows being called with any number of arguments and returns type `any`.
|
13
|
-
`Function` also allows classes or plain objects that happen to possess all properties of the `Function` class.
|
14
|
-
It's generally better to specify function parameters and return types with the function type syntax.
|
15
|
-
|
16
|
-
"Catch-all" function types include:
|
17
|
-
|
18
|
-
- `() => void`: a function that has no parameters and whose return is ignored
|
19
|
-
- `(...args: never) => unknown`: a "top type" for functions that can be assigned any function type, but can't be called
|
20
|
-
|
21
|
-
Examples of code for this rule:
|
22
|
-
|
23
|
-
<Tabs>
|
24
|
-
<TabItem value="❌ Incorrect">
|
25
|
-
|
26
|
-
```ts
|
27
|
-
let noParametersOrReturn: Function;
|
28
|
-
noParametersOrReturn = () => {};
|
29
|
-
|
30
|
-
let stringToNumber: Function;
|
31
|
-
stringToNumber = (text: string) => text.length;
|
32
|
-
|
33
|
-
let identity: Function;
|
34
|
-
identity = value => value;
|
35
|
-
```
|
36
|
-
|
37
|
-
</TabItem>
|
38
|
-
<TabItem value="✅ Correct">
|
39
|
-
|
40
|
-
```ts
|
41
|
-
let noParametersOrReturn: () => void;
|
42
|
-
noParametersOrReturn = () => {};
|
43
|
-
|
44
|
-
let stringToNumber: (text: string) => number;
|
45
|
-
stringToNumber = text => text.length;
|
46
|
-
|
47
|
-
let identity: <T>(value: T) => T;
|
48
|
-
identity = value => value;
|
49
|
-
```
|
50
|
-
|
51
|
-
</TabItem>
|
52
|
-
</Tabs>
|
53
|
-
|
54
|
-
## When Not To Use It
|
55
|
-
|
56
|
-
If your project is still onboarding to TypeScript, it might be difficult to fully replace all unsafe `Function` types with more precise function types.
|
57
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
58
|
-
|
59
|
-
## Related To
|
60
|
-
|
61
|
-
- [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
|
62
|
-
- [`no-empty-object-type`](./no-empty-object-type.mdx)
|
63
|
-
- [`no-restricted-types`](./no-restricted-types.mdx)
|
64
|
-
- [`no-unsafe-call`](./no-unsafe-call.mdx)
|
65
|
-
- [`no-wrapper-object-types`](./no-wrapper-object-types.mdx)
|
@@ -1,81 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow member access on a value with type `any`.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unsafe-member-access** for documentation.
|
11
|
-
|
12
|
-
The `any` type in TypeScript is a dangerous "escape hatch" from the type system.
|
13
|
-
Using `any` disables many type checking rules and is generally best used only as a last resort or when prototyping code.
|
14
|
-
|
15
|
-
Despite your best intentions, the `any` type can sometimes leak into your codebase.
|
16
|
-
Accessing a member of an `any`-typed value creates a potential type safety hole and source of bugs in your codebase.
|
17
|
-
|
18
|
-
This rule disallows member access on any variable that is typed as `any`.
|
19
|
-
|
20
|
-
## Examples
|
21
|
-
|
22
|
-
<Tabs>
|
23
|
-
<TabItem value="❌ Incorrect">
|
24
|
-
|
25
|
-
```ts
|
26
|
-
declare const anyVar: any;
|
27
|
-
declare const nestedAny: { prop: any };
|
28
|
-
|
29
|
-
anyVar.a;
|
30
|
-
anyVar.a.b;
|
31
|
-
anyVar['a'];
|
32
|
-
anyVar['a']['b'];
|
33
|
-
|
34
|
-
nestedAny.prop.a;
|
35
|
-
nestedAny.prop['a'];
|
36
|
-
|
37
|
-
const key = 'a';
|
38
|
-
nestedAny.prop[key];
|
39
|
-
|
40
|
-
// Using an any to access a member is unsafe
|
41
|
-
const arr = [1, 2, 3];
|
42
|
-
arr[anyVar];
|
43
|
-
nestedAny[anyVar];
|
44
|
-
```
|
45
|
-
|
46
|
-
</TabItem>
|
47
|
-
<TabItem value="✅ Correct">
|
48
|
-
|
49
|
-
```ts
|
50
|
-
declare const properlyTyped: { prop: { a: string } };
|
51
|
-
|
52
|
-
properlyTyped.prop.a;
|
53
|
-
properlyTyped.prop['a'];
|
54
|
-
|
55
|
-
const key = 'a';
|
56
|
-
properlyTyped.prop[key];
|
57
|
-
|
58
|
-
const arr = [1, 2, 3];
|
59
|
-
arr[1];
|
60
|
-
let idx = 1;
|
61
|
-
arr[idx];
|
62
|
-
arr[idx++];
|
63
|
-
```
|
64
|
-
|
65
|
-
</TabItem>
|
66
|
-
</Tabs>
|
67
|
-
|
68
|
-
## When Not To Use It
|
69
|
-
|
70
|
-
If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.
|
71
|
-
It may be easier to skip the `no-unsafe-*` rules pending increasing type safety in unsafe areas of your project.
|
72
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
73
|
-
|
74
|
-
## Related To
|
75
|
-
|
76
|
-
- [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
|
77
|
-
- [`no-explicit-any`](./no-explicit-any.mdx)
|
78
|
-
- [`no-unsafe-argument`](./no-unsafe-argument.mdx)
|
79
|
-
- [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
|
80
|
-
- [`no-unsafe-call`](./no-unsafe-call.mdx)
|
81
|
-
- [`no-unsafe-return`](./no-unsafe-return.mdx)
|
@@ -1,126 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow returning a value with type `any` from a function.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unsafe-return** for documentation.
|
11
|
-
|
12
|
-
The `any` type in TypeScript is a dangerous "escape hatch" from the type system.
|
13
|
-
Using `any` disables many type checking rules and is generally best used only as a last resort or when prototyping code.
|
14
|
-
|
15
|
-
Despite your best intentions, the `any` type can sometimes leak into your codebase.
|
16
|
-
Returning an `any`-typed value from a function creates a potential type safety hole and source of bugs in your codebase.
|
17
|
-
|
18
|
-
This rule disallows returning `any` or `any[]` from a function and returning `Promise<any>` from an async function.
|
19
|
-
|
20
|
-
This rule also compares generic type argument types to ensure you don't return an unsafe `any` in a generic position to a function that's expecting a specific type.
|
21
|
-
For example, it will error if you return `Set<any>` from a function declared as returning `Set<string>`.
|
22
|
-
|
23
|
-
## Examples
|
24
|
-
|
25
|
-
<Tabs>
|
26
|
-
<TabItem value="❌ Incorrect">
|
27
|
-
|
28
|
-
```ts
|
29
|
-
function foo1() {
|
30
|
-
return 1 as any;
|
31
|
-
}
|
32
|
-
function foo2() {
|
33
|
-
return Object.create(null);
|
34
|
-
}
|
35
|
-
const foo3 = () => {
|
36
|
-
return 1 as any;
|
37
|
-
};
|
38
|
-
const foo4 = () => Object.create(null);
|
39
|
-
|
40
|
-
function foo5() {
|
41
|
-
return [] as any[];
|
42
|
-
}
|
43
|
-
function foo6() {
|
44
|
-
return [] as Array<any>;
|
45
|
-
}
|
46
|
-
function foo7() {
|
47
|
-
return [] as readonly any[];
|
48
|
-
}
|
49
|
-
function foo8() {
|
50
|
-
return [] as Readonly<any[]>;
|
51
|
-
}
|
52
|
-
const foo9 = () => {
|
53
|
-
return [] as any[];
|
54
|
-
};
|
55
|
-
const foo10 = () => [] as any[];
|
56
|
-
|
57
|
-
const foo11 = (): string[] => [1, 2, 3] as any[];
|
58
|
-
|
59
|
-
async function foo13() {
|
60
|
-
return Promise.resolve({} as any);
|
61
|
-
}
|
62
|
-
|
63
|
-
// generic position examples
|
64
|
-
function assignability1(): Set<string> {
|
65
|
-
return new Set<any>([1]);
|
66
|
-
}
|
67
|
-
type TAssign = () => Set<string>;
|
68
|
-
const assignability2: TAssign = () => new Set<any>([true]);
|
69
|
-
```
|
70
|
-
|
71
|
-
</TabItem>
|
72
|
-
<TabItem value="✅ Correct">
|
73
|
-
|
74
|
-
```ts
|
75
|
-
function foo1() {
|
76
|
-
return 1;
|
77
|
-
}
|
78
|
-
function foo2() {
|
79
|
-
return Object.create(null) as Record<string, unknown>;
|
80
|
-
}
|
81
|
-
|
82
|
-
const foo3 = () => [];
|
83
|
-
const foo4 = () => ['a'];
|
84
|
-
|
85
|
-
async function foo5() {
|
86
|
-
return Promise.resolve(1);
|
87
|
-
}
|
88
|
-
|
89
|
-
function assignability1(): Set<string> {
|
90
|
-
return new Set<string>(['foo']);
|
91
|
-
}
|
92
|
-
type TAssign = () => Set<string>;
|
93
|
-
const assignability2: TAssign = () => new Set(['foo']);
|
94
|
-
```
|
95
|
-
|
96
|
-
</TabItem>
|
97
|
-
</Tabs>
|
98
|
-
|
99
|
-
There are cases where the rule allows to return `any` to `unknown`.
|
100
|
-
|
101
|
-
Examples of `any` to `unknown` return that are allowed:
|
102
|
-
|
103
|
-
```ts showPlaygroundButton
|
104
|
-
function foo1(): unknown {
|
105
|
-
return JSON.parse(singleObjString); // Return type for JSON.parse is any.
|
106
|
-
}
|
107
|
-
|
108
|
-
function foo2(): unknown[] {
|
109
|
-
return [] as any[];
|
110
|
-
}
|
111
|
-
```
|
112
|
-
|
113
|
-
## When Not To Use It
|
114
|
-
|
115
|
-
If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.
|
116
|
-
It may be easier to skip the `no-unsafe-*` rules pending increasing type safety in unsafe areas of your project.
|
117
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
118
|
-
|
119
|
-
## Related To
|
120
|
-
|
121
|
-
- [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
|
122
|
-
- [`no-explicit-any`](./no-explicit-any.mdx)
|
123
|
-
- [`no-unsafe-argument`](./no-unsafe-argument.mdx)
|
124
|
-
- [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
|
125
|
-
- [`no-unsafe-call`](./no-unsafe-call.mdx)
|
126
|
-
- [`no-unsafe-member-access`](./no-unsafe-member-access.mdx)
|