@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,67 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow iterating over an array with a for-in loop.'
|
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-for-in-array** for documentation.
|
11
|
-
|
12
|
-
A for-in loop (`for (const i in o)`) iterates over the properties of an Object.
|
13
|
-
While it is legal to use for-in loops with array values, it is not common. There are several potential bugs with this:
|
14
|
-
|
15
|
-
1. It iterates over all enumerable properties, including non-index ones and the entire prototype chain. For example, [`RegExp.prototype.exec`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) returns an array with additional properties, and `for-in` will iterate over them. Some libraries or even your own code may add additional methods to `Array.prototype` (either as polyfill or as custom methods), and if not done properly, they may be iterated over as well.
|
16
|
-
2. It skips holes in the array. While sparse arrays are rare and advised against, they are still possible and your code should be able to handle them.
|
17
|
-
3. The "index" is returned as a string, not a number. This can be caught by TypeScript, but can still lead to subtle bugs.
|
18
|
-
|
19
|
-
You may have confused for-in with for-of, which iterates over the elements of the array. If you actually need the index, use a regular `for` loop or the `forEach` method.
|
20
|
-
|
21
|
-
## Examples
|
22
|
-
|
23
|
-
<Tabs>
|
24
|
-
<TabItem value="❌ Incorrect">
|
25
|
-
|
26
|
-
```ts
|
27
|
-
declare const array: string[];
|
28
|
-
|
29
|
-
for (const i in array) {
|
30
|
-
console.log(array[i]);
|
31
|
-
}
|
32
|
-
|
33
|
-
for (const i in array) {
|
34
|
-
console.log(i, array[i]);
|
35
|
-
}
|
36
|
-
```
|
37
|
-
|
38
|
-
</TabItem>
|
39
|
-
<TabItem value="✅ Correct">
|
40
|
-
|
41
|
-
```ts
|
42
|
-
declare const array: string[];
|
43
|
-
|
44
|
-
for (const value of array) {
|
45
|
-
console.log(value);
|
46
|
-
}
|
47
|
-
|
48
|
-
for (let i = 0; i < array.length; i += 1) {
|
49
|
-
console.log(i, array[i]);
|
50
|
-
}
|
51
|
-
|
52
|
-
array.forEach((value, i) => {
|
53
|
-
console.log(i, value);
|
54
|
-
});
|
55
|
-
|
56
|
-
for (const [i, value] of array.entries()) {
|
57
|
-
console.log(i, value);
|
58
|
-
}
|
59
|
-
```
|
60
|
-
|
61
|
-
</TabItem>
|
62
|
-
</Tabs>
|
63
|
-
|
64
|
-
## When Not To Use It
|
65
|
-
|
66
|
-
If your project is a rare one that intentionally loops over string indices of arrays, you can turn off this rule.
|
67
|
-
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,106 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow the use of `eval()`-like functions.'
|
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-implied-eval** for documentation.
|
11
|
-
|
12
|
-
It uses type information to determine which values are `eval()`-like functions.
|
13
|
-
|
14
|
-
It's considered a good practice to avoid using `eval()`. There are security and performance implications involved with doing so, which is why many linters recommend disallowing `eval()`. However, there are some other ways to pass a string and have it interpreted as JavaScript code that have similar concerns.
|
15
|
-
|
16
|
-
The first is using `setTimeout()`, `setInterval()`, `setImmediate` or `execScript()` (Internet Explorer only), all of which can accept a string of code as their first argument
|
17
|
-
|
18
|
-
```ts
|
19
|
-
setTimeout('alert(`Hi!`);', 100);
|
20
|
-
```
|
21
|
-
|
22
|
-
or using `new Function()`
|
23
|
-
|
24
|
-
```ts
|
25
|
-
const fn = new Function('a', 'b', 'return a + b');
|
26
|
-
```
|
27
|
-
|
28
|
-
This is considered an implied `eval()` because a string of code is
|
29
|
-
passed in to be interpreted. The same can be done with `setInterval()`, `setImmediate()` and `execScript()`. All interpret the JavaScript code in the global scope.
|
30
|
-
|
31
|
-
The best practice is to avoid using `new Function()` or `execScript()` and always use a function for the first argument of `setTimeout()`, `setInterval()` and `setImmediate()`.
|
32
|
-
|
33
|
-
## Examples
|
34
|
-
|
35
|
-
This rule aims to eliminate implied `eval()` through the use of `new Function()`, `setTimeout()`, `setInterval()`, `setImmediate()` or `execScript()`.
|
36
|
-
|
37
|
-
<Tabs>
|
38
|
-
<TabItem value="❌ Incorrect">
|
39
|
-
|
40
|
-
```ts
|
41
|
-
setTimeout('alert(`Hi!`);', 100);
|
42
|
-
|
43
|
-
setInterval('alert(`Hi!`);', 100);
|
44
|
-
|
45
|
-
setImmediate('alert(`Hi!`)');
|
46
|
-
|
47
|
-
execScript('alert(`Hi!`)');
|
48
|
-
|
49
|
-
window.setTimeout('count = 5', 10);
|
50
|
-
|
51
|
-
window.setInterval('foo = bar', 10);
|
52
|
-
|
53
|
-
const fn = '() = {}';
|
54
|
-
setTimeout(fn, 100);
|
55
|
-
|
56
|
-
const fn = () => {
|
57
|
-
return 'x = 10';
|
58
|
-
};
|
59
|
-
setTimeout(fn(), 100);
|
60
|
-
|
61
|
-
const fn = new Function('a', 'b', 'return a + b');
|
62
|
-
```
|
63
|
-
|
64
|
-
</TabItem>
|
65
|
-
<TabItem value="✅ Correct">
|
66
|
-
|
67
|
-
```ts
|
68
|
-
setTimeout(function () {
|
69
|
-
alert('Hi!');
|
70
|
-
}, 100);
|
71
|
-
|
72
|
-
setInterval(function () {
|
73
|
-
alert('Hi!');
|
74
|
-
}, 100);
|
75
|
-
|
76
|
-
setImmediate(function () {
|
77
|
-
alert('Hi!');
|
78
|
-
});
|
79
|
-
|
80
|
-
execScript(function () {
|
81
|
-
alert('Hi!');
|
82
|
-
});
|
83
|
-
|
84
|
-
const fn = () => {};
|
85
|
-
setTimeout(fn, 100);
|
86
|
-
|
87
|
-
const foo = {
|
88
|
-
fn: function () {},
|
89
|
-
};
|
90
|
-
setTimeout(foo.fn, 100);
|
91
|
-
setTimeout(foo.fn.bind(this), 100);
|
92
|
-
|
93
|
-
class Foo {
|
94
|
-
static fn = () => {};
|
95
|
-
}
|
96
|
-
|
97
|
-
setTimeout(Foo.fn, 100);
|
98
|
-
```
|
99
|
-
|
100
|
-
</TabItem>
|
101
|
-
</Tabs>
|
102
|
-
|
103
|
-
## When Not To Use It
|
104
|
-
|
105
|
-
If your project is a rare one that needs to allow `new Function()` or `setTimeout()`, `setInterval()`, `setImmediate()` and `execScript()` with string arguments, then you can disable this rule.
|
106
|
-
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,80 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers.'
|
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-import-type-side-effects** for documentation.
|
11
|
-
|
12
|
-
The [`--verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax) compiler option causes TypeScript to do simple and predictable transpilation on import declarations.
|
13
|
-
Namely, it completely removes import declarations with a top-level `type` qualifier, and it removes any import specifiers with an inline `type` qualifier.
|
14
|
-
|
15
|
-
The latter behavior does have one potentially surprising effect in that in certain cases TS can leave behind a "side effect" import at runtime:
|
16
|
-
|
17
|
-
```ts
|
18
|
-
import { type A, type B } from 'mod';
|
19
|
-
|
20
|
-
// is transpiled to
|
21
|
-
|
22
|
-
import {} from 'mod';
|
23
|
-
// which is the same as
|
24
|
-
import 'mod';
|
25
|
-
```
|
26
|
-
|
27
|
-
For the rare case of needing to import for side effects, this may be desirable - but for most cases you will not want to leave behind an unnecessary side effect import.
|
28
|
-
|
29
|
-
## Examples
|
30
|
-
|
31
|
-
This rule enforces that you use a top-level `type` qualifier for imports when it only imports specifiers with an inline `type` qualifier
|
32
|
-
|
33
|
-
<Tabs>
|
34
|
-
<TabItem value="❌ Incorrect">
|
35
|
-
|
36
|
-
```ts
|
37
|
-
import { type A } from 'mod';
|
38
|
-
import { type A as AA } from 'mod';
|
39
|
-
import { type A, type B } from 'mod';
|
40
|
-
import { type A as AA, type B as BB } from 'mod';
|
41
|
-
```
|
42
|
-
|
43
|
-
</TabItem>
|
44
|
-
<TabItem value="✅ Correct">
|
45
|
-
|
46
|
-
```ts
|
47
|
-
import type { A } from 'mod';
|
48
|
-
import type { A as AA } from 'mod';
|
49
|
-
import type { A, B } from 'mod';
|
50
|
-
import type { A as AA, B as BB } from 'mod';
|
51
|
-
|
52
|
-
import T from 'mod';
|
53
|
-
import type T from 'mod';
|
54
|
-
|
55
|
-
import * as T from 'mod';
|
56
|
-
import type * as T from 'mod';
|
57
|
-
|
58
|
-
import { T } from 'mod';
|
59
|
-
import type { T } from 'mod';
|
60
|
-
import { T, U } from 'mod';
|
61
|
-
import type { T, U } from 'mod';
|
62
|
-
import { type T, U } from 'mod';
|
63
|
-
import { T, type U } from 'mod';
|
64
|
-
|
65
|
-
import type T, { U } from 'mod';
|
66
|
-
import T, { type U } from 'mod';
|
67
|
-
```
|
68
|
-
|
69
|
-
</TabItem>
|
70
|
-
</Tabs>
|
71
|
-
|
72
|
-
## When Not To Use It
|
73
|
-
|
74
|
-
If you're not using TypeScript 5.0's `verbatimModuleSyntax` option and your project is built with a bundler that manages import side effects for you, this rule may not be as useful for you.
|
75
|
-
|
76
|
-
## Related To
|
77
|
-
|
78
|
-
- [`consistent-type-imports`](./consistent-type-imports.mdx)
|
79
|
-
- [`import/consistent-type-specifier-style`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.md)
|
80
|
-
- [`import/no-duplicates` with `{"prefer-inline": true}`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-duplicates.md#inline-type-imports)
|
@@ -1,113 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow explicit type declarations for variables or parameters initialized to a number, string, or boolean.'
|
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-inferrable-types** for documentation.
|
11
|
-
|
12
|
-
TypeScript is able to infer the types of parameters, properties, and variables from their default or initial values.
|
13
|
-
There is no need to use an explicit `:` type annotation on one of those constructs initialized to a boolean, number, or string.
|
14
|
-
Doing so adds unnecessary verbosity to code -making it harder to read- and in some cases can prevent TypeScript from inferring a more specific literal type (e.g. `10`) instead of the more general primitive type (e.g. `number`)
|
15
|
-
|
16
|
-
## Examples
|
17
|
-
|
18
|
-
<Tabs>
|
19
|
-
<TabItem value="❌ Incorrect">
|
20
|
-
|
21
|
-
```ts
|
22
|
-
const a: bigint = 10n;
|
23
|
-
const a: bigint = BigInt(10);
|
24
|
-
const a: boolean = !0;
|
25
|
-
const a: boolean = Boolean(null);
|
26
|
-
const a: boolean = true;
|
27
|
-
const a: null = null;
|
28
|
-
const a: number = 10;
|
29
|
-
const a: number = Infinity;
|
30
|
-
const a: number = NaN;
|
31
|
-
const a: number = Number('1');
|
32
|
-
const a: RegExp = /a/;
|
33
|
-
const a: RegExp = new RegExp('a');
|
34
|
-
const a: string = `str`;
|
35
|
-
const a: string = String(1);
|
36
|
-
const a: symbol = Symbol('a');
|
37
|
-
const a: undefined = undefined;
|
38
|
-
const a: undefined = void someValue;
|
39
|
-
|
40
|
-
class Foo {
|
41
|
-
prop: number = 5;
|
42
|
-
}
|
43
|
-
|
44
|
-
function fn(a: number = 5, b: boolean = true) {}
|
45
|
-
```
|
46
|
-
|
47
|
-
</TabItem>
|
48
|
-
<TabItem value="✅ Correct">
|
49
|
-
|
50
|
-
```ts
|
51
|
-
const a = 10n;
|
52
|
-
const a = BigInt(10);
|
53
|
-
const a = !0;
|
54
|
-
const a = Boolean(null);
|
55
|
-
const a = true;
|
56
|
-
const a = null;
|
57
|
-
const a = 10;
|
58
|
-
const a = Infinity;
|
59
|
-
const a = NaN;
|
60
|
-
const a = Number('1');
|
61
|
-
const a = /a/;
|
62
|
-
const a = new RegExp('a');
|
63
|
-
const a = `str`;
|
64
|
-
const a = String(1);
|
65
|
-
const a = Symbol('a');
|
66
|
-
const a = undefined;
|
67
|
-
const a = void someValue;
|
68
|
-
|
69
|
-
class Foo {
|
70
|
-
prop = 5;
|
71
|
-
}
|
72
|
-
|
73
|
-
function fn(a = 5, b = true) {}
|
74
|
-
```
|
75
|
-
|
76
|
-
</TabItem>
|
77
|
-
</Tabs>
|
78
|
-
|
79
|
-
## Options
|
80
|
-
|
81
|
-
### `ignoreParameters`
|
82
|
-
|
83
|
-
{/* insert option description */}
|
84
|
-
|
85
|
-
When set to true, the following pattern is considered valid:
|
86
|
-
|
87
|
-
```ts option='{ "ignoreParameters": true }' showPlaygroundButton
|
88
|
-
function foo(a: number = 5, b: boolean = true) {
|
89
|
-
// ...
|
90
|
-
}
|
91
|
-
```
|
92
|
-
|
93
|
-
### `ignoreProperties`
|
94
|
-
|
95
|
-
{/* insert option description */}
|
96
|
-
|
97
|
-
When set to true, the following pattern is considered valid:
|
98
|
-
|
99
|
-
```ts option='{ "ignoreProperties": true }' showPlaygroundButton
|
100
|
-
class Foo {
|
101
|
-
prop: number = 5;
|
102
|
-
}
|
103
|
-
```
|
104
|
-
|
105
|
-
## When Not To Use It
|
106
|
-
|
107
|
-
If you strongly prefer to have explicit types regardless of whether they can be inferred, this rule may not be for you.
|
108
|
-
|
109
|
-
If you use the `--isolatedDeclarations` compiler option, this rule is incompatible.
|
110
|
-
|
111
|
-
## Further Reading
|
112
|
-
|
113
|
-
- [TypeScript Inference](https://www.typescriptlang.org/docs/handbook/type-inference.html)
|
@@ -1,16 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow `this` keywords outside of classes or class-like objects.'
|
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-invalid-this** for documentation.
|
11
|
-
|
12
|
-
import TypeScriptOverlap from '@site/src/components/TypeScriptOverlap';
|
13
|
-
|
14
|
-
<TypeScriptOverlap strict />
|
15
|
-
|
16
|
-
It adds support for TypeScript's `this` parameters.
|
@@ -1,119 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow `void` type outside of generic or return types.'
|
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-invalid-void-type** for documentation.
|
11
|
-
|
12
|
-
`void` in TypeScript refers to a function return that is meant to be ignored.
|
13
|
-
Attempting to use a `void` type outside of a return type or generic type argument is often a sign of programmer error.
|
14
|
-
`void` can also be misleading for other developers even if used correctly.
|
15
|
-
|
16
|
-
> The `void` type means cannot be mixed with any other types, other than `never`, which accepts all types.
|
17
|
-
> If you think you need this then you probably want the `undefined` type instead.
|
18
|
-
|
19
|
-
## Examples
|
20
|
-
|
21
|
-
<Tabs>
|
22
|
-
<TabItem value="❌ Incorrect">
|
23
|
-
|
24
|
-
```ts
|
25
|
-
type PossibleValues = string | number | void;
|
26
|
-
type MorePossibleValues = string | ((number & any) | (string | void));
|
27
|
-
|
28
|
-
function logSomething(thing: void) {}
|
29
|
-
function printArg<T = void>(arg: T) {}
|
30
|
-
|
31
|
-
logAndReturn<void>(undefined);
|
32
|
-
|
33
|
-
interface Interface {
|
34
|
-
lambda: () => void;
|
35
|
-
prop: void;
|
36
|
-
}
|
37
|
-
|
38
|
-
class MyClass {
|
39
|
-
private readonly propName: void;
|
40
|
-
}
|
41
|
-
```
|
42
|
-
|
43
|
-
</TabItem>
|
44
|
-
<TabItem value="✅ Correct">
|
45
|
-
|
46
|
-
```ts
|
47
|
-
type NoOp = () => void;
|
48
|
-
|
49
|
-
function noop(): void {}
|
50
|
-
|
51
|
-
let trulyUndefined = void 0;
|
52
|
-
|
53
|
-
async function promiseMeSomething(): Promise<void> {}
|
54
|
-
|
55
|
-
type stillVoid = void | never;
|
56
|
-
```
|
57
|
-
|
58
|
-
</TabItem>
|
59
|
-
</Tabs>
|
60
|
-
|
61
|
-
## Options
|
62
|
-
|
63
|
-
### `allowInGenericTypeArguments`
|
64
|
-
|
65
|
-
{/* insert option description */}
|
66
|
-
|
67
|
-
Alternatively, you can provide an array of strings which allowlist which types may accept `void` as a generic type parameter.
|
68
|
-
|
69
|
-
Any types considered valid by this option will be considered valid as part of a union type with `void`.
|
70
|
-
|
71
|
-
This option is `true` by default.
|
72
|
-
|
73
|
-
The following patterns are considered warnings with `{ allowInGenericTypeArguments: false }`:
|
74
|
-
|
75
|
-
```ts option='{ "allowInGenericTypeArguments": false }' showPlaygroundButton
|
76
|
-
logAndReturn<void>(undefined);
|
77
|
-
|
78
|
-
let voidPromise: Promise<void> = new Promise<void>(() => {});
|
79
|
-
let voidMap: Map<string, void> = new Map<string, void>();
|
80
|
-
```
|
81
|
-
|
82
|
-
The following patterns are considered warnings with `{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] }`:
|
83
|
-
|
84
|
-
```ts option='{ "allowInGenericTypeArguments": ["Ex.Mx.Tx"] }' showPlaygroundButton
|
85
|
-
logAndReturn<void>(undefined);
|
86
|
-
|
87
|
-
type NotAllowedVoid1 = Mx.Tx<void>;
|
88
|
-
type NotAllowedVoid2 = Tx<void>;
|
89
|
-
type NotAllowedVoid3 = Promise<void>;
|
90
|
-
```
|
91
|
-
|
92
|
-
The following patterns are not considered warnings with `{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] }`:
|
93
|
-
|
94
|
-
```ts option='{ "allowInGenericTypeArguments": ["Ex.Mx.Tx"] }' showPlaygroundButton
|
95
|
-
type AllowedVoid = Ex.Mx.Tx<void>;
|
96
|
-
type AllowedVoidUnion = void | Ex.Mx.Tx<void>;
|
97
|
-
```
|
98
|
-
|
99
|
-
### `allowAsThisParameter`
|
100
|
-
|
101
|
-
{/* insert option description */}
|
102
|
-
|
103
|
-
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).
|
104
|
-
|
105
|
-
This option is `false` by default.
|
106
|
-
|
107
|
-
The following patterns are considered warnings with `{ allowAsThisParameter: false }` but valid with `{ allowAsThisParameter: true }`:
|
108
|
-
|
109
|
-
```ts option='{ "allowAsThisParameter": false }' showPlaygroundButton
|
110
|
-
function doThing(this: void) {}
|
111
|
-
class Example {
|
112
|
-
static helper(this: void) {}
|
113
|
-
callback(this: void) {}
|
114
|
-
}
|
115
|
-
```
|
116
|
-
|
117
|
-
## When Not To Use It
|
118
|
-
|
119
|
-
If you don't care about if `void` is used with other types, or in invalid places, then you don't need this rule.
|
@@ -1,12 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow function declarations that contain unsafe references inside loop statements.'
|
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-loop-func** for documentation.
|
11
|
-
|
12
|
-
It adds support for TypeScript types.
|
@@ -1,17 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow literal numbers that lose precision.'
|
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-loss-of-precision** for documentation.
|
11
|
-
|
12
|
-
:::danger Deprecated
|
13
|
-
|
14
|
-
This rule has been deprecated because the base [`eslint/no-loss-of-precision`](https://eslint.org/docs/rules/no-loss-of-precision) rule added support for [numeric separators](https://github.com/tc39/proposal-numeric-separator).
|
15
|
-
There is no longer any need to use this extension rule.
|
16
|
-
|
17
|
-
:::
|
@@ -1,131 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow magic numbers.'
|
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-magic-numbers** for documentation.
|
11
|
-
|
12
|
-
It adds support for:
|
13
|
-
|
14
|
-
- numeric literal types (`type T = 1`),
|
15
|
-
- `enum` members (`enum Foo { bar = 1 }`),
|
16
|
-
- `readonly` class properties (`class Foo { readonly bar = 1 }`).
|
17
|
-
|
18
|
-
## Options
|
19
|
-
|
20
|
-
This rule adds the following options:
|
21
|
-
|
22
|
-
```ts
|
23
|
-
interface Options extends BaseNoMagicNumbersOptions {
|
24
|
-
ignoreEnums?: boolean;
|
25
|
-
ignoreNumericLiteralTypes?: boolean;
|
26
|
-
ignoreReadonlyClassProperties?: boolean;
|
27
|
-
ignoreTypeIndexes?: boolean;
|
28
|
-
}
|
29
|
-
|
30
|
-
const defaultOptions: Options = {
|
31
|
-
...baseNoMagicNumbersDefaultOptions,
|
32
|
-
ignoreEnums: false,
|
33
|
-
ignoreNumericLiteralTypes: false,
|
34
|
-
ignoreReadonlyClassProperties: false,
|
35
|
-
ignoreTypeIndexes: false,
|
36
|
-
};
|
37
|
-
```
|
38
|
-
|
39
|
-
### `ignoreEnums`
|
40
|
-
|
41
|
-
{/* insert option description */}
|
42
|
-
|
43
|
-
Whether enums used in TypeScript are considered okay. `false` by default.
|
44
|
-
|
45
|
-
Examples of **incorrect** code for the `{ "ignoreEnums": false }` option:
|
46
|
-
|
47
|
-
```ts option='{ "ignoreEnums": false }' showPlaygroundButton
|
48
|
-
enum foo {
|
49
|
-
SECOND = 1000,
|
50
|
-
}
|
51
|
-
```
|
52
|
-
|
53
|
-
Examples of **correct** code for the `{ "ignoreEnums": true }` option:
|
54
|
-
|
55
|
-
```ts option='{ "ignoreEnums": true }' showPlaygroundButton
|
56
|
-
enum foo {
|
57
|
-
SECOND = 1000,
|
58
|
-
}
|
59
|
-
```
|
60
|
-
|
61
|
-
### `ignoreNumericLiteralTypes`
|
62
|
-
|
63
|
-
{/* insert option description */}
|
64
|
-
|
65
|
-
Whether numbers used in TypeScript numeric literal types are considered okay. `false` by default.
|
66
|
-
|
67
|
-
Examples of **incorrect** code for the `{ "ignoreNumericLiteralTypes": false }` option:
|
68
|
-
|
69
|
-
```ts option='{ "ignoreNumericLiteralTypes": false }' showPlaygroundButton
|
70
|
-
type SmallPrimes = 2 | 3 | 5 | 7 | 11;
|
71
|
-
```
|
72
|
-
|
73
|
-
Examples of **correct** code for the `{ "ignoreNumericLiteralTypes": true }` option:
|
74
|
-
|
75
|
-
```ts option='{ "ignoreNumericLiteralTypes": true }' showPlaygroundButton
|
76
|
-
type SmallPrimes = 2 | 3 | 5 | 7 | 11;
|
77
|
-
```
|
78
|
-
|
79
|
-
### `ignoreReadonlyClassProperties`
|
80
|
-
|
81
|
-
{/* insert option description */}
|
82
|
-
|
83
|
-
Whether `readonly` class properties are considered okay.
|
84
|
-
|
85
|
-
Examples of **incorrect** code for the `{ "ignoreReadonlyClassProperties": false }` option:
|
86
|
-
|
87
|
-
```ts option='{ "ignoreReadonlyClassProperties": false }' showPlaygroundButton
|
88
|
-
class Foo {
|
89
|
-
readonly A = 1;
|
90
|
-
readonly B = 2;
|
91
|
-
public static readonly C = 1;
|
92
|
-
static readonly D = 1;
|
93
|
-
}
|
94
|
-
```
|
95
|
-
|
96
|
-
Examples of **correct** code for the `{ "ignoreReadonlyClassProperties": true }` option:
|
97
|
-
|
98
|
-
```ts option='{ "ignoreReadonlyClassProperties": true }' showPlaygroundButton
|
99
|
-
class Foo {
|
100
|
-
readonly A = 1;
|
101
|
-
readonly B = 2;
|
102
|
-
public static readonly C = 1;
|
103
|
-
static readonly D = 1;
|
104
|
-
}
|
105
|
-
```
|
106
|
-
|
107
|
-
### `ignoreTypeIndexes`
|
108
|
-
|
109
|
-
{/* insert option description */}
|
110
|
-
|
111
|
-
Whether numbers used to index types are okay. `false` by default.
|
112
|
-
|
113
|
-
Examples of **incorrect** code for the `{ "ignoreTypeIndexes": false }` option:
|
114
|
-
|
115
|
-
```ts option='{ "ignoreTypeIndexes": false }' showPlaygroundButton
|
116
|
-
type Foo = Bar[0];
|
117
|
-
type Baz = Parameters<Foo>[2];
|
118
|
-
```
|
119
|
-
|
120
|
-
Examples of **correct** code for the `{ "ignoreTypeIndexes": true }` option:
|
121
|
-
|
122
|
-
```ts option='{ "ignoreTypeIndexes": true }' showPlaygroundButton
|
123
|
-
type Foo = Bar[0];
|
124
|
-
type Baz = Parameters<Foo>[2];
|
125
|
-
```
|
126
|
-
|
127
|
-
## When Not To Use It
|
128
|
-
|
129
|
-
If your project frequently deals with constant numbers and you don't wish to take up extra space to declare them, this rule might not be for you.
|
130
|
-
We recommend at least using descriptive comments and/or names to describe constants.
|
131
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) instead of completely disabling this rule.
|