@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,63 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow type assertions that narrow a 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-type-assertion** for documentation.
|
11
|
-
|
12
|
-
Type assertions are a way to tell TypeScript what the type of a value is. This can be useful but also unsafe if you use type assertions to narrow down a type.
|
13
|
-
|
14
|
-
This rule forbids using type assertions to narrow a type, as this bypasses TypeScript's type-checking. Type assertions that broaden a type are safe because TypeScript essentially knows _less_ about a type.
|
15
|
-
|
16
|
-
Instead of using type assertions to narrow a type, it's better to rely on type guards, which help avoid potential runtime errors caused by unsafe type assertions.
|
17
|
-
|
18
|
-
## Examples
|
19
|
-
|
20
|
-
<Tabs>
|
21
|
-
<TabItem value="❌ Incorrect">
|
22
|
-
|
23
|
-
```ts
|
24
|
-
function f() {
|
25
|
-
return Math.random() < 0.5 ? 42 : 'oops';
|
26
|
-
}
|
27
|
-
|
28
|
-
const z = f() as number;
|
29
|
-
|
30
|
-
const items = [1, '2', 3, '4'];
|
31
|
-
|
32
|
-
const number = items[0] as number;
|
33
|
-
```
|
34
|
-
|
35
|
-
</TabItem>
|
36
|
-
<TabItem value="✅ Correct">
|
37
|
-
|
38
|
-
```ts
|
39
|
-
function f() {
|
40
|
-
return Math.random() < 0.5 ? 42 : 'oops';
|
41
|
-
}
|
42
|
-
|
43
|
-
const z = f() as number | string | boolean;
|
44
|
-
|
45
|
-
const items = [1, '2', 3, '4'];
|
46
|
-
|
47
|
-
const number = items[0] as number | string | undefined;
|
48
|
-
```
|
49
|
-
|
50
|
-
</TabItem>
|
51
|
-
</Tabs>
|
52
|
-
|
53
|
-
## When Not To Use It
|
54
|
-
|
55
|
-
If your codebase has many unsafe type assertions, then it may be difficult to enable this rule.
|
56
|
-
It may be easier to skip the `no-unsafe-*` rules pending increasing type safety in unsafe areas of your project.
|
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
|
-
If your project frequently stubs objects in test files, the rule may trigger a lot of reports. Consider disabling the rule for such files to reduce frequent warnings.
|
60
|
-
|
61
|
-
## Further Reading
|
62
|
-
|
63
|
-
- More on TypeScript's [type assertions](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions)
|
@@ -1,60 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Require unary negation to take a number.'
|
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-unary-minus** for documentation.
|
11
|
-
|
12
|
-
TypeScript does not prevent you from putting a minus sign before things other than numbers:
|
13
|
-
|
14
|
-
```ts
|
15
|
-
const s = 'hello';
|
16
|
-
const x = -s; // x is NaN
|
17
|
-
```
|
18
|
-
|
19
|
-
This rule restricts the unary `-` operator to `number | bigint`.
|
20
|
-
|
21
|
-
## Examples
|
22
|
-
|
23
|
-
<Tabs>
|
24
|
-
<TabItem value="❌ Incorrect">
|
25
|
-
|
26
|
-
```ts
|
27
|
-
declare const a: string;
|
28
|
-
-a;
|
29
|
-
|
30
|
-
declare const b: {};
|
31
|
-
-b;
|
32
|
-
```
|
33
|
-
|
34
|
-
</TabItem>
|
35
|
-
<TabItem value="✅ Correct">
|
36
|
-
|
37
|
-
```ts
|
38
|
-
-42;
|
39
|
-
-42n;
|
40
|
-
|
41
|
-
declare const a: number;
|
42
|
-
-a;
|
43
|
-
|
44
|
-
declare const b: number;
|
45
|
-
-b;
|
46
|
-
|
47
|
-
declare const c: number | bigint;
|
48
|
-
-c;
|
49
|
-
|
50
|
-
declare const d: any;
|
51
|
-
-d;
|
52
|
-
|
53
|
-
declare const e: 1 | 2;
|
54
|
-
-e;
|
55
|
-
```
|
56
|
-
|
57
|
-
</TabItem>
|
58
|
-
</Tabs>
|
59
|
-
|
60
|
-
{/* Intentionally Omitted: When Not To Use It */}
|
@@ -1,52 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unused expressions.'
|
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-unused-expressions** for documentation.
|
11
|
-
|
12
|
-
It supports TypeScript-specific expressions:
|
13
|
-
|
14
|
-
- Marks directives in modules declarations (`"use strict"`, etc.) as not unused
|
15
|
-
- Marks the following expressions as unused if their wrapped value expressions are unused:
|
16
|
-
- Assertion expressions: `x as number;`, `x!;`, `<number>x;`
|
17
|
-
- Instantiation expressions: `Set<number>;`
|
18
|
-
|
19
|
-
Although the type expressions never have runtime side effects (that is, `x!;` is the same as `x;`), they can be used to assert types for testing purposes.
|
20
|
-
|
21
|
-
## Examples
|
22
|
-
|
23
|
-
<Tabs>
|
24
|
-
<TabItem value="❌ Incorrect">
|
25
|
-
|
26
|
-
```ts
|
27
|
-
Set<number>;
|
28
|
-
1 as number;
|
29
|
-
window!;
|
30
|
-
```
|
31
|
-
|
32
|
-
</TabItem>
|
33
|
-
<TabItem value="✅ Correct">
|
34
|
-
|
35
|
-
```ts
|
36
|
-
function getSet() {
|
37
|
-
return Set;
|
38
|
-
}
|
39
|
-
|
40
|
-
// Funtion calls are allowed, so type expressions that wrap function calls are allowed
|
41
|
-
getSet()<number>;
|
42
|
-
getSet() as Set<unknown>;
|
43
|
-
getSet()!;
|
44
|
-
|
45
|
-
// Namespaces can have directives
|
46
|
-
namespace A {
|
47
|
-
'use strict';
|
48
|
-
}
|
49
|
-
```
|
50
|
-
|
51
|
-
</TabItem>
|
52
|
-
</Tabs>
|
@@ -1,136 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unused variables.'
|
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-unused-vars** for documentation.
|
11
|
-
|
12
|
-
It adds support for TypeScript features, such as types.
|
13
|
-
|
14
|
-
## Options
|
15
|
-
|
16
|
-
## FAQs
|
17
|
-
|
18
|
-
### What benefits does this rule have over TypeScript?
|
19
|
-
|
20
|
-
TypeScript provides [`noUnusedLocals`](https://www.typescriptlang.org/tsconfig#noUnusedLocals) and [`noUnusedParameters`](https://www.typescriptlang.org/tsconfig#noUnusedParameters) compiler options that can report errors on unused local variables or parameters, respectively.
|
21
|
-
Those compiler options can be convenient to use if you don't want to set up ESLint and typescript-eslint.
|
22
|
-
However:
|
23
|
-
|
24
|
-
- These lint rules are more configurable than TypeScript's compiler options.
|
25
|
-
- For example, the [`varsIgnorePattern` option](https://eslint.org/docs/latest/rules/no-unused-vars#varsignorepattern) can customize what names are always allowed to be exempted. TypeScript hardcodes its exemptions to names starting with `_`.
|
26
|
-
If you would like to emulate the TypeScript style of exempting names starting with `_`, you can use this configuration (this includes errors as well):
|
27
|
-
```json
|
28
|
-
{
|
29
|
-
"rules": {
|
30
|
-
"@typescript-eslint/no-unused-vars": [
|
31
|
-
"error",
|
32
|
-
{
|
33
|
-
"args": "all",
|
34
|
-
"argsIgnorePattern": "^_",
|
35
|
-
"caughtErrors": "all",
|
36
|
-
"caughtErrorsIgnorePattern": "^_",
|
37
|
-
"destructuredArrayIgnorePattern": "^_",
|
38
|
-
"varsIgnorePattern": "^_",
|
39
|
-
"ignoreRestSiblings": true
|
40
|
-
}
|
41
|
-
]
|
42
|
-
}
|
43
|
-
}
|
44
|
-
```
|
45
|
-
- [ESLint can be configured](https://eslint.org/docs/latest/use/configure/rules) within lines, files, and folders. TypeScript compiler options are linked to their TSConfig file.
|
46
|
-
- Many projects configure TypeScript's reported errors to block builds more aggressively than ESLint complaints. Blocking builds on unused variables can be inconvenient.
|
47
|
-
|
48
|
-
We generally recommend using `@typescript-eslint/no-unused-vars` to flag unused locals and parameters instead of TypeScript.
|
49
|
-
|
50
|
-
:::tip
|
51
|
-
Editors such as VS Code will still generally "grey out" unused variables even if `noUnusedLocals` and `noUnusedParameters` are not enabled in a project.
|
52
|
-
:::
|
53
|
-
|
54
|
-
Also see similar rules provided by ESLint:
|
55
|
-
|
56
|
-
- [`no-unused-private-class-members`](https://eslint.org/docs/latest/rules/no-unused-private-class-members)
|
57
|
-
- [`no-unused-labels`](https://eslint.org/docs/latest/rules/no-unused-labels)
|
58
|
-
|
59
|
-
### Why does this rule report variables used only for types?
|
60
|
-
|
61
|
-
This rule does not count type-only uses when determining whether a variable is used.
|
62
|
-
Declaring variables only to use them for types adds code and runtime complexity.
|
63
|
-
The variables are never actually used at runtime.
|
64
|
-
They can be misleading to readers of the code.
|
65
|
-
|
66
|
-
<Tabs>
|
67
|
-
|
68
|
-
<TabItem value="typeof Variables">
|
69
|
-
|
70
|
-
For example, if a variable is only used for `typeof`, this rule will report:
|
71
|
-
|
72
|
-
```ts
|
73
|
-
const box = {
|
74
|
-
// ~~~
|
75
|
-
// 'box' is assigned a value but only used as a type.
|
76
|
-
value: 123,
|
77
|
-
};
|
78
|
-
|
79
|
-
export type Box = typeof box;
|
80
|
-
```
|
81
|
-
|
82
|
-
Instead, it's often cleaner and less code to write out the types directly:
|
83
|
-
|
84
|
-
```ts
|
85
|
-
export interface Box {
|
86
|
-
value: number;
|
87
|
-
}
|
88
|
-
```
|
89
|
-
|
90
|
-
</TabItem>
|
91
|
-
|
92
|
-
<TabItem value="Zod Schemas">
|
93
|
-
|
94
|
-
For example, if a Zod schema variable is only used for `typeof`, this rule will report:
|
95
|
-
|
96
|
-
```ts
|
97
|
-
import { z } from 'zod';
|
98
|
-
|
99
|
-
const schema = z.object({
|
100
|
-
// ~~~~~~
|
101
|
-
// 'schema' is assigned a value but only used as a type.
|
102
|
-
value: z.number(),
|
103
|
-
});
|
104
|
-
|
105
|
-
export type Box = z.infer<typeof schema>;
|
106
|
-
```
|
107
|
-
|
108
|
-
Instead, it's often cleaner and less code to write out the types directly:
|
109
|
-
|
110
|
-
```ts
|
111
|
-
export interface Box {
|
112
|
-
value: number;
|
113
|
-
}
|
114
|
-
```
|
115
|
-
|
116
|
-
</TabItem>
|
117
|
-
|
118
|
-
</Tabs>
|
119
|
-
|
120
|
-
If you find yourself writing runtime values only for types, consider refactoring your code to declare types directly.
|
121
|
-
|
122
|
-
### Why are variables reported as unused despite being referenced by @link in JSDoc?
|
123
|
-
|
124
|
-
JSDoc references are not supported by typescript-eslint.
|
125
|
-
You can use a rule such as [`jsdoc/no-undefined-types`](https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/no-undefined-types.md) to resolve variables as used in JSDoc comments.
|
126
|
-
|
127
|
-
```ts
|
128
|
-
import type { Box } from './Box';
|
129
|
-
// ~~~
|
130
|
-
// 'Box' is defined but never used.
|
131
|
-
|
132
|
-
/**
|
133
|
-
* @see {@link Box}
|
134
|
-
*/
|
135
|
-
export function getBox() {}
|
136
|
-
```
|
@@ -1,98 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow the use of variables before they are defined.'
|
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-use-before-define** for documentation.
|
11
|
-
|
12
|
-
It adds support for `type`, `interface` and `enum` declarations.
|
13
|
-
|
14
|
-
## Options
|
15
|
-
|
16
|
-
This rule adds the following options:
|
17
|
-
|
18
|
-
```ts
|
19
|
-
interface Options extends BaseNoUseBeforeDefineOptions {
|
20
|
-
enums?: boolean;
|
21
|
-
typedefs?: boolean;
|
22
|
-
ignoreTypeReferences?: boolean;
|
23
|
-
}
|
24
|
-
|
25
|
-
const defaultOptions: Options = {
|
26
|
-
...baseNoUseBeforeDefineDefaultOptions,
|
27
|
-
enums: true,
|
28
|
-
typedefs: true,
|
29
|
-
ignoreTypeReferences: true,
|
30
|
-
};
|
31
|
-
```
|
32
|
-
|
33
|
-
### `enums`
|
34
|
-
|
35
|
-
{/* insert option description */}
|
36
|
-
|
37
|
-
If this is `true`, this rule warns every reference to a enum before the enum declaration.
|
38
|
-
If this is `false`, this rule will ignore references to enums, when the reference is in a child scope.
|
39
|
-
|
40
|
-
Examples of code for the `{ "enums": true }` option:
|
41
|
-
|
42
|
-
<Tabs>
|
43
|
-
<TabItem value="❌ Incorrect">
|
44
|
-
|
45
|
-
```ts option='{ "enums": true }'
|
46
|
-
const x = Foo.FOO;
|
47
|
-
|
48
|
-
enum Foo {
|
49
|
-
FOO,
|
50
|
-
}
|
51
|
-
```
|
52
|
-
|
53
|
-
</TabItem>
|
54
|
-
<TabItem value="✅ Correct">
|
55
|
-
|
56
|
-
```ts option='{ "enums": false }'
|
57
|
-
function foo() {
|
58
|
-
return Foo.FOO;
|
59
|
-
}
|
60
|
-
|
61
|
-
enum Foo {
|
62
|
-
FOO,
|
63
|
-
}
|
64
|
-
```
|
65
|
-
|
66
|
-
</TabItem>
|
67
|
-
</Tabs>
|
68
|
-
|
69
|
-
### `typedefs`
|
70
|
-
|
71
|
-
{/* insert option description */}
|
72
|
-
|
73
|
-
If this is `true`, this rule warns every reference to a type before the type declaration.
|
74
|
-
If this is `false`, this rule will ignore references to types.
|
75
|
-
|
76
|
-
Examples of **correct** code for the `{ "typedefs": false }` option:
|
77
|
-
|
78
|
-
```ts option='{ "typedefs": false }' showPlaygroundButton
|
79
|
-
let myVar: StringOrNumber;
|
80
|
-
type StringOrNumber = string | number;
|
81
|
-
```
|
82
|
-
|
83
|
-
### `ignoreTypeReferences`
|
84
|
-
|
85
|
-
{/* insert option description */}
|
86
|
-
|
87
|
-
If this is `true`, this rule ignores all type references.
|
88
|
-
If this is `false`, this will check all type references.
|
89
|
-
|
90
|
-
Examples of **correct** code for the `{ "ignoreTypeReferences": true }` option:
|
91
|
-
|
92
|
-
```ts option='{ "ignoreTypeReferences": true }' showPlaygroundButton
|
93
|
-
let var1: StringOrNumber;
|
94
|
-
type StringOrNumber = string | number;
|
95
|
-
|
96
|
-
let var2: Enum;
|
97
|
-
enum Enum {}
|
98
|
-
```
|
@@ -1,21 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unnecessary constructors.'
|
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-useless-constructor** for documentation.
|
11
|
-
|
12
|
-
It adds support for:
|
13
|
-
|
14
|
-
- constructors marked as `protected` / `private` (i.e. marking a constructor as non-public),
|
15
|
-
- `public` constructors when there is no superclass,
|
16
|
-
- constructors with only parameter properties.
|
17
|
-
|
18
|
-
### Caveat
|
19
|
-
|
20
|
-
This lint rule will report on constructors whose sole purpose is to change visibility of a parent constructor.
|
21
|
-
See [discussion on this rule's lack of type information](https://github.com/typescript-eslint/typescript-eslint/issues/3820#issuecomment-917821240) for context.
|
@@ -1,53 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: "Disallow empty exports that don't change anything in a module file."
|
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-useless-empty-export** for documentation.
|
11
|
-
|
12
|
-
An empty `export {}` statement is sometimes useful in TypeScript code to turn a file that would otherwise be a script file into a module file.
|
13
|
-
Per the [TypeScript Handbook Modules page](https://www.typescriptlang.org/docs/handbook/modules.html):
|
14
|
-
|
15
|
-
> In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.
|
16
|
-
> Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
|
17
|
-
|
18
|
-
However, an `export {}` statement does nothing if there are any other top-level import or export statements in a file.
|
19
|
-
|
20
|
-
This rule reports an `export {}` that doesn't do anything in a file already using ES modules.
|
21
|
-
|
22
|
-
## Examples
|
23
|
-
|
24
|
-
<Tabs>
|
25
|
-
<TabItem value="❌ Incorrect">
|
26
|
-
|
27
|
-
```ts
|
28
|
-
export const value = 'Hello, world!';
|
29
|
-
export {};
|
30
|
-
```
|
31
|
-
|
32
|
-
```ts
|
33
|
-
import 'some-other-module';
|
34
|
-
export {};
|
35
|
-
```
|
36
|
-
|
37
|
-
</TabItem>
|
38
|
-
<TabItem value="✅ Correct">
|
39
|
-
|
40
|
-
```ts
|
41
|
-
export const value = 'Hello, world!';
|
42
|
-
```
|
43
|
-
|
44
|
-
```ts
|
45
|
-
import 'some-other-module';
|
46
|
-
```
|
47
|
-
|
48
|
-
</TabItem>
|
49
|
-
</Tabs>
|
50
|
-
|
51
|
-
## When Not To Use It
|
52
|
-
|
53
|
-
If you don't mind an empty `export {}` at the bottom of files, you likely don't need this rule.
|
@@ -1,9 +0,0 @@
|
|
1
|
-
---
|
2
|
-
displayed_sidebar: rulesSidebar
|
3
|
-
---
|
4
|
-
|
5
|
-
:::danger Deprecated
|
6
|
-
|
7
|
-
This rule has been renamed to [`no-unnecessary-template-expression`](./no-unnecessary-template-expression.mdx). See [#8544](https://github.com/typescript-eslint/typescript-eslint/issues/8544) for more information.
|
8
|
-
|
9
|
-
:::
|
@@ -1,77 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow `require` statements except in import 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-var-requires** for documentation.
|
11
|
-
|
12
|
-
:::danger Deprecated
|
13
|
-
|
14
|
-
This rule has been deprecated in favour of the [`@typescript-eslint/no-require-imports`](./no-require-imports.mdx) rule.
|
15
|
-
|
16
|
-
:::
|
17
|
-
|
18
|
-
In other words, the use of forms such as `var foo = require("foo")` are banned. Instead use ES6 style imports or `import foo = require("foo")` imports.
|
19
|
-
|
20
|
-
## Examples
|
21
|
-
|
22
|
-
<Tabs>
|
23
|
-
<TabItem value="❌ Incorrect">
|
24
|
-
|
25
|
-
```ts
|
26
|
-
var foo = require('foo');
|
27
|
-
const foo = require('foo');
|
28
|
-
let foo = require('foo');
|
29
|
-
```
|
30
|
-
|
31
|
-
</TabItem>
|
32
|
-
<TabItem value="✅ Correct">
|
33
|
-
|
34
|
-
```ts
|
35
|
-
import foo = require('foo');
|
36
|
-
require('foo');
|
37
|
-
import foo from 'foo';
|
38
|
-
```
|
39
|
-
|
40
|
-
</TabItem>
|
41
|
-
</Tabs>
|
42
|
-
|
43
|
-
## Options
|
44
|
-
|
45
|
-
### `allow`
|
46
|
-
|
47
|
-
{/* insert option description */}
|
48
|
-
|
49
|
-
A array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
|
50
|
-
|
51
|
-
With `{allow: ['/package\\.json$']}`:
|
52
|
-
|
53
|
-
<Tabs>
|
54
|
-
<TabItem value="❌ Incorrect">
|
55
|
-
|
56
|
-
```ts option='{ "allow": ["/package.json$"] }'
|
57
|
-
const foo = require('../data.json');
|
58
|
-
```
|
59
|
-
|
60
|
-
</TabItem>
|
61
|
-
<TabItem value="✅ Correct">
|
62
|
-
|
63
|
-
```ts option='{ "allow": ["/package.json$"] }'
|
64
|
-
const foo = require('../package.json');
|
65
|
-
```
|
66
|
-
|
67
|
-
</TabItem>
|
68
|
-
</Tabs>
|
69
|
-
|
70
|
-
## When Not To Use It
|
71
|
-
|
72
|
-
If your project frequently uses older CommonJS `require`s, then this rule might not be applicable to you.
|
73
|
-
If only a subset of your project uses `require`s then 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.
|
74
|
-
|
75
|
-
## Related To
|
76
|
-
|
77
|
-
- [`no-require-imports`](./no-require-imports.mdx)
|
@@ -1,75 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow using confusing built-in primitive class wrappers.'
|
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-wrapper-object-types** for documentation.
|
11
|
-
|
12
|
-
TypeScript defines several confusing pairs of types that look very similar to each other, but actually mean different things: `boolean`/`Boolean`, `number`/`Number`, `string`/`String`, `bigint`/`BigInt`, `symbol`/`Symbol`, `object`/`Object`.
|
13
|
-
In general, only the lowercase variant is appropriate to use.
|
14
|
-
Therefore, this rule enforces that you only use the lowercase variant.
|
15
|
-
|
16
|
-
JavaScript has [8 data types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures) at runtime, and these are described in TypeScript by the lowercase types `undefined`, `null`, `boolean`, `number`, `string`, `bigint`, `symbol`, and `object`.
|
17
|
-
|
18
|
-
As for the uppercase types, these are _structural types_ which describe JavaScript "wrapper" objects for each of the data types, such as [`Boolean`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) and [`Number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number).
|
19
|
-
Additionally, due to the quirks of structural typing, the corresponding primitives are _also_ assignable to these uppercase types, since they have the same "shape".
|
20
|
-
|
21
|
-
It is a universal best practice to work directly with the built-in primitives, like `0`, rather than objects that "look like" the corresponding primitive, like `new Number(0)`.
|
22
|
-
|
23
|
-
- Primitives have the expected value semantics with `==` and `===` equality checks, whereas their object counterparts are compared by reference.
|
24
|
-
That is to say, `"str" === "str"` but `new String("str") !== new String("str")`.
|
25
|
-
- Primitives have well-known behavior around truthiness/falsiness which is common to rely on, whereas all objects are truthy, regardless of the wrapped value (e.g. `new Boolean(false)` is truthy).
|
26
|
-
- TypeScript only allows arithmetic operations (e.g. `x - y`) to be performed on numeric primitives, not objects.
|
27
|
-
|
28
|
-
As a result, using the lowercase type names like `number` in TypeScript types instead of the uppercase names like `Number` is a better practice that describes code more accurately.
|
29
|
-
|
30
|
-
Examples of code for this rule:
|
31
|
-
|
32
|
-
<Tabs>
|
33
|
-
<TabItem value="❌ Incorrect">
|
34
|
-
|
35
|
-
```ts
|
36
|
-
let myBigInt: BigInt;
|
37
|
-
let myBoolean: Boolean;
|
38
|
-
let myNumber: Number;
|
39
|
-
let myString: String;
|
40
|
-
let mySymbol: Symbol;
|
41
|
-
|
42
|
-
let myObject: Object = 'allowed by TypeScript';
|
43
|
-
```
|
44
|
-
|
45
|
-
</TabItem>
|
46
|
-
<TabItem value="✅ Correct">
|
47
|
-
|
48
|
-
```ts
|
49
|
-
let myBigint: bigint;
|
50
|
-
let myBoolean: boolean;
|
51
|
-
let myNumber: number;
|
52
|
-
let myString: string;
|
53
|
-
let mySymbol: symbol;
|
54
|
-
|
55
|
-
let myObject: object = "Type 'string' is not assignable to type 'object'.";
|
56
|
-
```
|
57
|
-
|
58
|
-
</TabItem>
|
59
|
-
</Tabs>
|
60
|
-
|
61
|
-
## When Not To Use It
|
62
|
-
|
63
|
-
If your project is a rare one that intentionally deals with the class equivalents of primitives, it might not be worthwhile to use this rule.
|
64
|
-
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.
|
65
|
-
|
66
|
-
## Further Reading
|
67
|
-
|
68
|
-
- [MDN documentation on primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive)
|
69
|
-
- [MDN documentation on `string` primitives and `String` objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_primitives_and_string_objects)
|
70
|
-
|
71
|
-
## Related To
|
72
|
-
|
73
|
-
- [`no-empty-object-type`](./no-empty-object-type.mdx)
|
74
|
-
- [`no-restricted-types`](./no-restricted-types.mdx)
|
75
|
-
- [`no-unsafe-function-type`](./no-unsafe-function-type.mdx)
|