@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.
Files changed (161) hide show
  1. package/package.json +9 -10
  2. package/docs/rules/README.md +0 -57
  3. package/docs/rules/TEMPLATE.md +0 -36
  4. package/docs/rules/adjacent-overload-signatures.mdx +0 -105
  5. package/docs/rules/array-type.mdx +0 -126
  6. package/docs/rules/await-thenable.mdx +0 -184
  7. package/docs/rules/ban-ts-comment.mdx +0 -165
  8. package/docs/rules/ban-tslint-comment.mdx +0 -45
  9. package/docs/rules/ban-types.md +0 -26
  10. package/docs/rules/block-spacing.md +0 -15
  11. package/docs/rules/brace-style.md +0 -15
  12. package/docs/rules/camelcase.md +0 -15
  13. package/docs/rules/class-literal-property-style.mdx +0 -112
  14. package/docs/rules/class-methods-use-this.mdx +0 -135
  15. package/docs/rules/comma-dangle.md +0 -15
  16. package/docs/rules/comma-spacing.md +0 -15
  17. package/docs/rules/consistent-generic-constructors.mdx +0 -87
  18. package/docs/rules/consistent-indexed-object-style.mdx +0 -105
  19. package/docs/rules/consistent-return.mdx +0 -51
  20. package/docs/rules/consistent-type-assertions.mdx +0 -196
  21. package/docs/rules/consistent-type-definitions.mdx +0 -133
  22. package/docs/rules/consistent-type-exports.mdx +0 -97
  23. package/docs/rules/consistent-type-imports.mdx +0 -139
  24. package/docs/rules/default-param-last.mdx +0 -59
  25. package/docs/rules/dot-notation.mdx +0 -94
  26. package/docs/rules/explicit-function-return-type.mdx +0 -359
  27. package/docs/rules/explicit-member-accessibility.mdx +0 -353
  28. package/docs/rules/explicit-module-boundary-types.mdx +0 -287
  29. package/docs/rules/func-call-spacing.md +0 -15
  30. package/docs/rules/indent.md +0 -15
  31. package/docs/rules/init-declarations.mdx +0 -12
  32. package/docs/rules/key-spacing.md +0 -15
  33. package/docs/rules/keyword-spacing.md +0 -15
  34. package/docs/rules/lines-around-comment.md +0 -15
  35. package/docs/rules/lines-between-class-members.md +0 -15
  36. package/docs/rules/max-params.mdx +0 -54
  37. package/docs/rules/member-delimiter-style.md +0 -15
  38. package/docs/rules/member-ordering.mdx +0 -1483
  39. package/docs/rules/method-signature-style.mdx +0 -124
  40. package/docs/rules/naming-convention.mdx +0 -755
  41. package/docs/rules/no-array-constructor.mdx +0 -34
  42. package/docs/rules/no-array-delete.mdx +0 -44
  43. package/docs/rules/no-base-to-string.mdx +0 -115
  44. package/docs/rules/no-confusing-non-null-assertion.mdx +0 -75
  45. package/docs/rules/no-confusing-void-expression.mdx +0 -148
  46. package/docs/rules/no-deprecated.mdx +0 -119
  47. package/docs/rules/no-dupe-class-members.mdx +0 -16
  48. package/docs/rules/no-duplicate-enum-values.mdx +0 -66
  49. package/docs/rules/no-duplicate-imports.mdx +0 -17
  50. package/docs/rules/no-duplicate-type-constituents.mdx +0 -89
  51. package/docs/rules/no-dynamic-delete.mdx +0 -64
  52. package/docs/rules/no-empty-function.mdx +0 -94
  53. package/docs/rules/no-empty-interface.mdx +0 -75
  54. package/docs/rules/no-empty-object-type.mdx +0 -150
  55. package/docs/rules/no-explicit-any.mdx +0 -177
  56. package/docs/rules/no-extra-non-null-assertion.mdx +0 -60
  57. package/docs/rules/no-extra-parens.md +0 -15
  58. package/docs/rules/no-extra-semi.md +0 -15
  59. package/docs/rules/no-extraneous-class.mdx +0 -329
  60. package/docs/rules/no-floating-promises.mdx +0 -282
  61. package/docs/rules/no-for-in-array.mdx +0 -67
  62. package/docs/rules/no-implied-eval.mdx +0 -106
  63. package/docs/rules/no-import-type-side-effects.mdx +0 -80
  64. package/docs/rules/no-inferrable-types.mdx +0 -113
  65. package/docs/rules/no-invalid-this.mdx +0 -16
  66. package/docs/rules/no-invalid-void-type.mdx +0 -119
  67. package/docs/rules/no-loop-func.mdx +0 -12
  68. package/docs/rules/no-loss-of-precision.mdx +0 -17
  69. package/docs/rules/no-magic-numbers.mdx +0 -131
  70. package/docs/rules/no-meaningless-void-operator.mdx +0 -61
  71. package/docs/rules/no-misused-new.mdx +0 -53
  72. package/docs/rules/no-misused-promises.mdx +0 -314
  73. package/docs/rules/no-misused-spread.mdx +0 -132
  74. package/docs/rules/no-mixed-enums.mdx +0 -96
  75. package/docs/rules/no-namespace.mdx +0 -157
  76. package/docs/rules/no-non-null-asserted-nullish-coalescing.mdx +0 -60
  77. package/docs/rules/no-non-null-asserted-optional-chain.mdx +0 -46
  78. package/docs/rules/no-non-null-assertion.mdx +0 -48
  79. package/docs/rules/no-parameter-properties.mdx +0 -16
  80. package/docs/rules/no-redeclare.mdx +0 -79
  81. package/docs/rules/no-redundant-type-constituents.mdx +0 -102
  82. package/docs/rules/no-require-imports.mdx +0 -114
  83. package/docs/rules/no-restricted-imports.mdx +0 -84
  84. package/docs/rules/no-restricted-types.mdx +0 -70
  85. package/docs/rules/no-shadow.mdx +0 -143
  86. package/docs/rules/no-this-alias.mdx +0 -124
  87. package/docs/rules/no-type-alias.mdx +0 -626
  88. package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +0 -165
  89. package/docs/rules/no-unnecessary-condition.mdx +0 -293
  90. package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +0 -42
  91. package/docs/rules/no-unnecessary-qualifier.mdx +0 -57
  92. package/docs/rules/no-unnecessary-template-expression.mdx +0 -108
  93. package/docs/rules/no-unnecessary-type-arguments.mdx +0 -85
  94. package/docs/rules/no-unnecessary-type-assertion.mdx +0 -97
  95. package/docs/rules/no-unnecessary-type-constraint.mdx +0 -61
  96. package/docs/rules/no-unnecessary-type-conversion.mdx +0 -79
  97. package/docs/rules/no-unnecessary-type-parameters.mdx +0 -255
  98. package/docs/rules/no-unsafe-argument.mdx +0 -98
  99. package/docs/rules/no-unsafe-assignment.mdx +0 -101
  100. package/docs/rules/no-unsafe-call.mdx +0 -120
  101. package/docs/rules/no-unsafe-declaration-merging.mdx +0 -65
  102. package/docs/rules/no-unsafe-enum-comparison.mdx +0 -98
  103. package/docs/rules/no-unsafe-function-type.mdx +0 -65
  104. package/docs/rules/no-unsafe-member-access.mdx +0 -81
  105. package/docs/rules/no-unsafe-return.mdx +0 -126
  106. package/docs/rules/no-unsafe-type-assertion.mdx +0 -63
  107. package/docs/rules/no-unsafe-unary-minus.mdx +0 -60
  108. package/docs/rules/no-unused-expressions.mdx +0 -52
  109. package/docs/rules/no-unused-vars.mdx +0 -136
  110. package/docs/rules/no-use-before-define.mdx +0 -98
  111. package/docs/rules/no-useless-constructor.mdx +0 -21
  112. package/docs/rules/no-useless-empty-export.mdx +0 -53
  113. package/docs/rules/no-useless-template-literals.mdx +0 -9
  114. package/docs/rules/no-var-requires.mdx +0 -77
  115. package/docs/rules/no-wrapper-object-types.mdx +0 -75
  116. package/docs/rules/non-nullable-type-assertion-style.mdx +0 -47
  117. package/docs/rules/object-curly-spacing.md +0 -15
  118. package/docs/rules/only-throw-error.mdx +0 -150
  119. package/docs/rules/padding-line-between-statements.md +0 -15
  120. package/docs/rules/parameter-properties.mdx +0 -522
  121. package/docs/rules/prefer-as-const.mdx +0 -51
  122. package/docs/rules/prefer-destructuring.mdx +0 -101
  123. package/docs/rules/prefer-enum-initializers.mdx +0 -68
  124. package/docs/rules/prefer-find.mdx +0 -45
  125. package/docs/rules/prefer-for-of.mdx +0 -50
  126. package/docs/rules/prefer-function-type.mdx +0 -98
  127. package/docs/rules/prefer-includes.mdx +0 -81
  128. package/docs/rules/prefer-literal-enum-member.mdx +0 -111
  129. package/docs/rules/prefer-namespace-keyword.mdx +0 -51
  130. package/docs/rules/prefer-nullish-coalescing.mdx +0 -349
  131. package/docs/rules/prefer-optional-chain.mdx +0 -304
  132. package/docs/rules/prefer-promise-reject-errors.mdx +0 -78
  133. package/docs/rules/prefer-readonly-parameter-types.mdx +0 -408
  134. package/docs/rules/prefer-readonly.mdx +0 -111
  135. package/docs/rules/prefer-reduce-type-parameter.mdx +0 -66
  136. package/docs/rules/prefer-regexp-exec.mdx +0 -52
  137. package/docs/rules/prefer-return-this-type.mdx +0 -93
  138. package/docs/rules/prefer-string-starts-ends-with.mdx +0 -84
  139. package/docs/rules/prefer-ts-expect-error.mdx +0 -86
  140. package/docs/rules/promise-function-async.mdx +0 -143
  141. package/docs/rules/quotes.md +0 -15
  142. package/docs/rules/related-getter-setter-pairs.mdx +0 -61
  143. package/docs/rules/require-array-sort-compare.mdx +0 -89
  144. package/docs/rules/require-await.mdx +0 -53
  145. package/docs/rules/restrict-plus-operands.mdx +0 -245
  146. package/docs/rules/restrict-template-expressions.mdx +0 -167
  147. package/docs/rules/return-await.mdx +0 -339
  148. package/docs/rules/semi.md +0 -15
  149. package/docs/rules/sort-type-constituents.mdx +0 -209
  150. package/docs/rules/sort-type-union-intersection-members.mdx +0 -16
  151. package/docs/rules/space-before-blocks.md +0 -15
  152. package/docs/rules/space-before-function-paren.md +0 -15
  153. package/docs/rules/space-infix-ops.md +0 -15
  154. package/docs/rules/strict-boolean-expressions.mdx +0 -184
  155. package/docs/rules/switch-exhaustiveness-check.mdx +0 -280
  156. package/docs/rules/triple-slash-reference.mdx +0 -129
  157. package/docs/rules/type-annotation-spacing.md +0 -15
  158. package/docs/rules/typedef.mdx +0 -350
  159. package/docs/rules/unbound-method.mdx +0 -114
  160. package/docs/rules/unified-signatures.mdx +0 -132
  161. package/docs/rules/use-unknown-in-catch-callback-variable.mdx +0 -97
@@ -1,139 +0,0 @@
1
- ---
2
- description: 'Enforce consistent usage of type imports.'
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/consistent-type-imports** for documentation.
11
-
12
- TypeScript allows specifying a `type` keyword on imports to indicate that the export exists only in the type system, not at runtime.
13
- This allows transpilers to drop imports without knowing the types of the dependencies.
14
-
15
- > See [Blog > Consistent Type Exports and Imports: Why and How](/blog/consistent-type-imports-and-exports-why-and-how) for more details.
16
-
17
- ## Options
18
-
19
- ### `prefer`
20
-
21
- {/* insert option description */}
22
-
23
- Valid values for `prefer` are:
24
-
25
- - `type-imports` will enforce that you always use `import type Foo from '...'` except referenced by metadata of decorators. It is the default.
26
- - `no-type-imports` will enforce that you always use `import Foo from '...'`.
27
-
28
- Examples of **correct** code with `{prefer: 'type-imports'}`, and **incorrect** code with `{prefer: 'no-type-imports'}`.
29
-
30
- ```ts option='{ "prefer": "type-imports" }' showPlaygroundButton
31
- import type { Foo } from 'Foo';
32
- import type Bar from 'Bar';
33
- type T = Foo;
34
- const x: Bar = 1;
35
- ```
36
-
37
- Examples of **incorrect** code with `{prefer: 'type-imports'}`, and **correct** code with `{prefer: 'no-type-imports'}`.
38
-
39
- ```ts option='{ "prefer": "type-imports" }' showPlaygroundButton
40
- import { Foo } from 'Foo';
41
- import Bar from 'Bar';
42
- type T = Foo;
43
- const x: Bar = 1;
44
- ```
45
-
46
- ### `fixStyle`
47
-
48
- {/* insert option description */}
49
-
50
- Valid values for `fixStyle` are:
51
-
52
- - `separate-type-imports` will add the type keyword after the import keyword `import type { A } from '...'`. It is the default.
53
- - `inline-type-imports` will inline the type keyword `import { type A } from '...'` and is only available in TypeScript 4.5 and onwards. See [documentation here](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#type-modifiers-on-import-names 'TypeScript 4.5 documentation on type modifiers and import names').
54
-
55
- <Tabs>
56
- <TabItem value="❌ Incorrect">
57
-
58
- ```ts
59
- import { Foo } from 'Foo';
60
- import Bar from 'Bar';
61
- type T = Foo;
62
- const x: Bar = 1;
63
- ```
64
-
65
- </TabItem>
66
- <TabItem value="✅ With `separate-type-imports`">
67
-
68
- ```ts option='{ "fixStyle": "separate-type-imports" }'
69
- import type { Foo } from 'Foo';
70
- import type Bar from 'Bar';
71
- type T = Foo;
72
- const x: Bar = 1;
73
- ```
74
-
75
- </TabItem>
76
- <TabItem value="✅ With `inline-type-imports`">
77
-
78
- ```ts option='{ "fixStyle": "inline-type-imports" }'
79
- import { type Foo } from 'Foo';
80
- import type Bar from 'Bar';
81
- type T = Foo;
82
- const x: Bar = 1;
83
- ```
84
-
85
- </TabItem>
86
- </Tabs>
87
-
88
- ### `disallowTypeAnnotations`
89
-
90
- {/* insert option description */}
91
-
92
- Examples of **incorrect** code with `{disallowTypeAnnotations: true}`:
93
-
94
- ```ts option='{ "disallowTypeAnnotations": true }' showPlaygroundButton
95
- type T = import('Foo').Foo;
96
- const x: import('Bar') = 1;
97
- ```
98
-
99
- ## Caveat: `@decorators` + `experimentalDecorators: true` + `emitDecoratorMetadata: true`
100
-
101
- :::note
102
- If you are using `experimentalDecorators: false` (eg [TypeScript v5.0's stable decorators](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#decorators)) then the rule will always report errors as expected.
103
- This caveat **only** applies to `experimentalDecorators: true`
104
- :::
105
-
106
- The rule will **_not_** report any errors in files _that contain decorators_ when **both** `experimentalDecorators` and `emitDecoratorMetadata` are turned on.
107
-
108
- > See [Blog > Changes to consistent-type-imports when used with legacy decorators and decorator metadata](/blog/changes-to-consistent-type-imports-with-decorators) for more details.
109
-
110
- If you are using [type-aware linting](/getting-started/typed-linting) then we will automatically infer your setup from your tsconfig and you should not need to configure anything.
111
- Otherwise you can explicitly tell our tooling to analyze your code as if the compiler option was turned on by setting both [`parserOptions.emitDecoratorMetadata = true`](/packages/parser/#emitdecoratormetadata) and [`parserOptions.experimentalDecorators = true`](/packages/parser/#experimentaldecorators).
112
-
113
- ## Comparison with `importsNotUsedAsValues` / `verbatimModuleSyntax`
114
-
115
- [`verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax) was introduced in TypeScript v5.0 (as a replacement for `importsNotUsedAsValues`).
116
- This rule and `verbatimModuleSyntax` _mostly_ behave in the same way.
117
- There are a few behavior differences:
118
- | Situation | `consistent-type-imports` (ESLint) | `verbatimModuleSyntax` (TypeScript) |
119
- | -------------------------------------------------------------- | --------------------------------------------------------- | ----------------------------------------------------------- |
120
- | Unused imports | Ignored (consider using [`@typescript-eslint/no-unused-vars`](/rules/no-unused-vars)) | Type error |
121
- | Usage with `emitDecoratorMetadata` & `experimentalDecorations` | Ignores files that contain decorators | Reports on files that contain decorators |
122
- | Failures detected | Does not fail `tsc` build; can be auto-fixed with `--fix` | Fails `tsc` build; cannot be auto-fixed on the command-line |
123
- | `import { type T } from 'T';` | TypeScript will emit nothing (it "elides" the import) | TypeScript emits `import {} from 'T'` |
124
-
125
- Because there are some differences, using both this rule and `verbatimModuleSyntax` at the same time can lead to conflicting errors.
126
- As such we recommend that you only ever use one _or_ the other -- never both.
127
-
128
- ## When Not To Use It
129
-
130
- If you specifically want to use both import kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.
131
-
132
- However, keep in mind that inconsistent style can harm readability in a project.
133
- We recommend picking a single option for this rule that works best for your project.
134
-
135
- ## Related To
136
-
137
- - [`no-import-type-side-effects`](./no-import-type-side-effects.mdx)
138
- - [`import/consistent-type-specifier-style`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.md)
139
- - [`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,59 +0,0 @@
1
- ---
2
- description: 'Enforce default parameters to be last.'
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/default-param-last** for documentation.
11
-
12
- It adds support for optional parameters.
13
-
14
- <Tabs>
15
- <TabItem value="❌ Incorrect">
16
-
17
- ```ts
18
- function f(a = 0, b: number) {}
19
- function f(a: number, b = 0, c: number) {}
20
- function f(a: number, b?: number, c: number) {}
21
- class Foo {
22
- constructor(
23
- public a = 10,
24
- private b: number,
25
- ) {}
26
- }
27
- class Foo {
28
- constructor(
29
- public a?: number,
30
- private b: number,
31
- ) {}
32
- }
33
- ```
34
-
35
- </TabItem>
36
- <TabItem value="✅ Correct">
37
-
38
- ```ts
39
- function f(a = 0) {}
40
- function f(a: number, b = 0) {}
41
- function f(a: number, b?: number) {}
42
- function f(a: number, b?: number, c = 0) {}
43
- function f(a: number, b = 0, c?: number) {}
44
- class Foo {
45
- constructor(
46
- public a,
47
- private b = 0,
48
- ) {}
49
- }
50
- class Foo {
51
- constructor(
52
- public a,
53
- private b?: number,
54
- ) {}
55
- }
56
- ```
57
-
58
- </TabItem>
59
- </Tabs>
@@ -1,94 +0,0 @@
1
- ---
2
- description: 'Enforce dot notation whenever possible.'
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/dot-notation** for documentation.
11
-
12
- It adds:
13
-
14
- - Support for optionally ignoring computed `private` and/or `protected` member access.
15
- - Compatibility with TypeScript's `noPropertyAccessFromIndexSignature` option.
16
-
17
- ## Options
18
-
19
- This rule adds the following options:
20
-
21
- ```ts
22
- interface Options extends BaseDotNotationOptions {
23
- allowPrivateClassPropertyAccess?: boolean;
24
- allowProtectedClassPropertyAccess?: boolean;
25
- allowIndexSignaturePropertyAccess?: boolean;
26
- }
27
-
28
- const defaultOptions: Options = {
29
- ...baseDotNotationDefaultOptions,
30
- allowPrivateClassPropertyAccess: false,
31
- allowProtectedClassPropertyAccess: false,
32
- allowIndexSignaturePropertyAccess: false,
33
- };
34
- ```
35
-
36
- If the TypeScript compiler option `noPropertyAccessFromIndexSignature` is set to `true`, then this rule always allows the use of square bracket notation to access properties of types that have a `string` index signature, even if `allowIndexSignaturePropertyAccess` is `false`.
37
-
38
- ### `allowPrivateClassPropertyAccess`
39
-
40
- {/* insert option description */}
41
-
42
- This can be useful because TypeScript will report a type error on dot notation but not array notation.
43
-
44
- Example of a correct code when `allowPrivateClassPropertyAccess` is set to `true`:
45
-
46
- ```ts option='{ "allowPrivateClassPropertyAccess": true }' showPlaygroundButton
47
- class X {
48
- private priv_prop = 123;
49
- }
50
-
51
- const x = new X();
52
- x['priv_prop'] = 123;
53
- ```
54
-
55
- ### `allowProtectedClassPropertyAccess`
56
-
57
- {/* insert option description */}
58
-
59
- This can be useful because TypeScript will report a type error on dot notation but not array notation.
60
-
61
- Example of a correct code when `allowProtectedClassPropertyAccess` is set to `true`:
62
-
63
- ```ts option='{ "allowProtectedClassPropertyAccess": true }' showPlaygroundButton
64
- class X {
65
- protected protected_prop = 123;
66
- }
67
-
68
- const x = new X();
69
- x['protected_prop'] = 123;
70
- ```
71
-
72
- ### `allowIndexSignaturePropertyAccess`
73
-
74
- {/* insert option description */}
75
-
76
- Example of correct code when `allowIndexSignaturePropertyAccess` is set to `true`:
77
-
78
- ```ts option='{ "allowIndexSignaturePropertyAccess": true }' showPlaygroundButton
79
- class X {
80
- [key: string]: number;
81
- }
82
-
83
- const x = new X();
84
- x['hello'] = 123;
85
- ```
86
-
87
- If the TypeScript compiler option `noPropertyAccessFromIndexSignature` is set to `true`, then the above code is always allowed, even if `allowIndexSignaturePropertyAccess` is `false`.
88
-
89
- ## When Not To Use It
90
-
91
- If you specifically want to use both member access kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.
92
-
93
- However, keep in mind that inconsistent style can harm readability in a project.
94
- We recommend picking a single option for this rule that works best for your project.
@@ -1,359 +0,0 @@
1
- ---
2
- description: 'Require explicit return types on functions and class methods.'
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/explicit-function-return-type** for documentation.
11
-
12
- Functions in TypeScript often don't need to be given an explicit return type annotation.
13
- Leaving off the return type is less code to read or write and allows the compiler to infer it from the contents of the function.
14
-
15
- However, explicit return types do make it visually more clear what type is returned by a function.
16
- They can also speed up TypeScript type checking performance in large codebases with many large functions.
17
-
18
- This rule enforces that functions do have an explicit return type annotation.
19
-
20
- ## Examples
21
-
22
- <Tabs>
23
- <TabItem value="❌ Incorrect">
24
-
25
- ```ts
26
- // Should indicate that no value is returned (void)
27
- function test() {
28
- return;
29
- }
30
-
31
- // Should indicate that a number is returned
32
- var fn = function () {
33
- return 1;
34
- };
35
-
36
- // Should indicate that a string is returned
37
- var arrowFn = () => 'test';
38
-
39
- class Test {
40
- // Should indicate that no value is returned (void)
41
- method() {
42
- return;
43
- }
44
- }
45
- ```
46
-
47
- </TabItem>
48
- <TabItem value="✅ Correct">
49
-
50
- ```ts
51
- // No return value should be expected (void)
52
- function test(): void {
53
- return;
54
- }
55
-
56
- // A return value of type number
57
- var fn = function (): number {
58
- return 1;
59
- };
60
-
61
- // A return value of type string
62
- var arrowFn = (): string => 'test';
63
-
64
- class Test {
65
- // No return value should be expected (void)
66
- method(): void {
67
- return;
68
- }
69
- }
70
- ```
71
-
72
- </TabItem>
73
- </Tabs>
74
-
75
- ## Options
76
-
77
- ### Configuring in a mixed JS/TS codebase
78
-
79
- If you are working on a codebase within which you lint non-TypeScript code (i.e. `.js`/`.mjs`/`.cjs`/`.jsx`), you should ensure that you should use [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files) to only enable the rule on `.ts`/`.mts`/`.cts`/`.tsx` files. If you don't, then you will get unfixable lint errors reported within `.js`/`.mjs`/`.cjs`/`.jsx` files.
80
-
81
- ```jsonc
82
- {
83
- "rules": {
84
- // disable the rule for all files
85
- "@typescript-eslint/explicit-function-return-type": "off",
86
- },
87
- "overrides": [
88
- {
89
- // enable the rule specifically for TypeScript files
90
- "files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
91
- "rules": {
92
- "@typescript-eslint/explicit-function-return-type": "error",
93
- },
94
- },
95
- ],
96
- }
97
- ```
98
-
99
- ### `allowExpressions`
100
-
101
- {/* insert option description */}
102
-
103
- Examples of code for this rule with `{ allowExpressions: true }`:
104
-
105
- <Tabs>
106
- <TabItem value="❌ Incorrect">
107
-
108
- ```ts option='{ "allowExpressions": true }'
109
- function test() {}
110
-
111
- const fn = () => {};
112
-
113
- export default () => {};
114
- ```
115
-
116
- </TabItem>
117
- <TabItem value="✅ Correct">
118
-
119
- ```ts option='{ "allowExpressions": true }'
120
- node.addEventListener('click', () => {});
121
-
122
- node.addEventListener('click', function () {});
123
-
124
- const foo = arr.map(i => i * i);
125
- ```
126
-
127
- </TabItem>
128
- </Tabs>
129
-
130
- ### `allowTypedFunctionExpressions`
131
-
132
- {/* insert option description */}
133
-
134
- Examples of code for this rule with `{ allowTypedFunctionExpressions: true }`:
135
-
136
- <Tabs>
137
- <TabItem value="❌ Incorrect">
138
-
139
- ```ts option='{ "allowTypedFunctionExpressions": true }'
140
- let arrowFn = () => 'test';
141
-
142
- let funcExpr = function () {
143
- return 'test';
144
- };
145
-
146
- let objectProp = {
147
- foo: () => 1,
148
- };
149
- ```
150
-
151
- </TabItem>
152
- <TabItem value="✅ Correct">
153
-
154
- ```ts option='{ "allowTypedFunctionExpressions": true }'
155
- type FuncType = () => string;
156
-
157
- let arrowFn: FuncType = () => 'test';
158
-
159
- let funcExpr: FuncType = function () {
160
- return 'test';
161
- };
162
-
163
- let asTyped = (() => '') as () => string;
164
-
165
- interface ObjectType {
166
- foo(): number;
167
- }
168
- let objectProp: ObjectType = {
169
- foo: () => 1,
170
- };
171
- let objectPropAs = {
172
- foo: () => 1,
173
- } as ObjectType;
174
-
175
- declare function functionWithArg(arg: () => number);
176
- functionWithArg(() => 1);
177
-
178
- declare function functionWithObjectArg(arg: { method: () => number });
179
- functionWithObjectArg({
180
- method() {
181
- return 1;
182
- },
183
- });
184
- ```
185
-
186
- </TabItem>
187
- </Tabs>
188
-
189
- ### `allowHigherOrderFunctions`
190
-
191
- {/* insert option description */}
192
-
193
- Examples of code for this rule with `{ allowHigherOrderFunctions: true }`:
194
-
195
- <Tabs>
196
- <TabItem value="❌ Incorrect">
197
-
198
- ```ts option='{ "allowHigherOrderFunctions": true }'
199
- var arrowFn = () => () => {};
200
-
201
- function fn() {
202
- return function () {};
203
- }
204
- ```
205
-
206
- </TabItem>
207
- <TabItem value="✅ Correct">
208
-
209
- ```ts option='{ "allowHigherOrderFunctions": true }'
210
- var arrowFn = () => (): void => {};
211
-
212
- function fn() {
213
- return function (): void {};
214
- }
215
- ```
216
-
217
- </TabItem>
218
- </Tabs>
219
-
220
- ### `allowDirectConstAssertionInArrowFunctions`
221
-
222
- {/* insert option description */}
223
-
224
- Examples of code for this rule with `{ allowDirectConstAssertionInArrowFunctions: true }`:
225
-
226
- <Tabs>
227
- <TabItem value="❌ Incorrect">
228
-
229
- ```ts option='{ "allowDirectConstAssertionInArrowFunctions": true }'
230
- const func = (value: number) => ({ type: 'X', value }) as any;
231
- const func = (value: number) => ({ type: 'X', value }) as Action;
232
- ```
233
-
234
- </TabItem>
235
- <TabItem value="✅ Correct">
236
-
237
- ```ts option='{ "allowDirectConstAssertionInArrowFunctions": true }'
238
- const func = (value: number) => ({ foo: 'bar', value }) as const;
239
- const func = () => x as const;
240
- ```
241
-
242
- </TabItem>
243
- </Tabs>
244
-
245
- ### `allowConciseArrowFunctionExpressionsStartingWithVoid`
246
-
247
- {/* insert option description */}
248
-
249
- Examples of code for this rule with `{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }`:
250
-
251
- <Tabs>
252
- <TabItem value="❌ Incorrect">
253
-
254
- ```ts option='{ "allowConciseArrowFunctionExpressionsStartingWithVoid": true }'
255
- var join = (a: string, b: string) => `${a}${b}`;
256
-
257
- const log = (message: string) => {
258
- console.log(message);
259
- };
260
- ```
261
-
262
- </TabItem>
263
- <TabItem value="✅ Correct">
264
-
265
- ```ts option='{ "allowConciseArrowFunctionExpressionsStartingWithVoid": true }'
266
- var log = (message: string) => void console.log(message);
267
- ```
268
-
269
- </TabItem>
270
- </Tabs>
271
-
272
- ### `allowFunctionsWithoutTypeParameters`
273
-
274
- {/* insert option description */}
275
-
276
- Examples of code for this rule with `{ allowFunctionsWithoutTypeParameters: true }`:
277
-
278
- <Tabs>
279
- <TabItem value="❌ Incorrect">
280
-
281
- ```ts option='{ "allowFunctionsWithoutTypeParameters": true }'
282
- function foo<T>(t: T) {
283
- return t;
284
- }
285
-
286
- const bar = <T>(t: T) => t;
287
- ```
288
-
289
- </TabItem>
290
- <TabItem value="✅ Correct">
291
-
292
- ```ts option='{ "allowFunctionsWithoutTypeParameters": true }'
293
- function foo<T>(t: T): T {
294
- return t;
295
- }
296
-
297
- const bar = <T>(t: T): T => t;
298
-
299
- function allowedFunction(x: string) {
300
- return x;
301
- }
302
-
303
- const allowedArrow = (x: string) => x;
304
- ```
305
-
306
- </TabItem>
307
- </Tabs>
308
-
309
- ### `allowedNames`
310
-
311
- {/* insert option description */}
312
-
313
- You may pass function/method names you would like this rule to ignore, like so:
314
-
315
- ```json
316
- {
317
- "@typescript-eslint/explicit-function-return-type": [
318
- "error",
319
- {
320
- "allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
321
- }
322
- ]
323
- }
324
- ```
325
-
326
- ### `allowIIFEs`
327
-
328
- {/* insert option description */}
329
-
330
- Examples of code for this rule with `{ allowIIFEs: true }`:
331
-
332
- <Tabs>
333
- <TabItem value="❌ Incorrect">
334
-
335
- ```ts option='{ "allowIIFEs": true }'
336
- var func = () => 'foo';
337
- ```
338
-
339
- </TabItem>
340
- <TabItem value="✅ Correct">
341
-
342
- ```ts option='{ "allowIIFEs": true }'
343
- var foo = (() => 'foo')();
344
-
345
- var bar = (function () {
346
- return 'bar';
347
- })();
348
- ```
349
-
350
- </TabItem>
351
- </Tabs>
352
-
353
- ## When Not To Use It
354
-
355
- If you don't find the added cost of explicitly writing function return types to be worth the visual clarity, or your project is not large enough for it to be a factor in type checking performance, then you will not need this rule.
356
-
357
- ## Further Reading
358
-
359
- - TypeScript [Functions](https://www.typescriptlang.org/docs/handbook/functions.html#function-types)