@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,105 +0,0 @@
1
- ---
2
- description: 'Require or disallow the `Record` 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/consistent-indexed-object-style** for documentation.
11
-
12
- TypeScript supports defining arbitrary object keys using an index signature or mapped type.
13
- TypeScript also has a builtin type named `Record` to create an empty object defining only an index signature.
14
- For example, the following types are equal:
15
-
16
- ```ts
17
- interface IndexSignatureInterface {
18
- [key: string]: unknown;
19
- }
20
-
21
- type IndexSignatureType = {
22
- [key: string]: unknown;
23
- };
24
-
25
- type MappedType = {
26
- [key in string]: unknown;
27
- };
28
-
29
- type RecordType = Record<string, unknown>;
30
- ```
31
-
32
- Using one declaration form consistently improves code readability.
33
-
34
- ## Options
35
-
36
- - `'record'` _(default)_: only allow the `Record` type.
37
- - `'index-signature'`: only allow index signatures.
38
-
39
- ### `'record'`
40
-
41
- {/* insert option description */}
42
-
43
- <Tabs>
44
- <TabItem value="❌ Incorrect">
45
-
46
- ```ts option='"record"'
47
- interface IndexSignatureInterface {
48
- [key: string]: unknown;
49
- }
50
-
51
- type IndexSignatureType = {
52
- [key: string]: unknown;
53
- };
54
-
55
- type MappedType = {
56
- [key in string]: unknown;
57
- };
58
- ```
59
-
60
- </TabItem>
61
- <TabItem value="✅ Correct">
62
-
63
- ```ts option='"record"'
64
- type RecordType = Record<string, unknown>;
65
- ```
66
-
67
- </TabItem>
68
- </Tabs>
69
-
70
- ### `'index-signature'`
71
-
72
- <Tabs>
73
- <TabItem value="❌ Incorrect">
74
-
75
- ```ts option='"index-signature"'
76
- type RecordType = Record<string, unknown>;
77
- ```
78
-
79
- </TabItem>
80
- <TabItem value="✅ Correct">
81
-
82
- ```ts option='"index-signature"'
83
- interface IndexSignatureInterface {
84
- [key: string]: unknown;
85
- }
86
-
87
- type IndexSignatureType = {
88
- [key: string]: unknown;
89
- };
90
-
91
- type MappedType = {
92
- [key in string]: unknown;
93
- };
94
- ```
95
-
96
- </TabItem>
97
- </Tabs>
98
-
99
- ## When Not To Use It
100
-
101
- This rule is purely a stylistic rule for maintaining consistency in your project.
102
- You can turn it off if you don't want to keep a consistent style for indexed object types.
103
-
104
- However, keep in mind that inconsistent style can harm readability in a project.
105
- We recommend picking a single option for this rule that works best for your project.
@@ -1,51 +0,0 @@
1
- ---
2
- description: 'Require `return` statements to either always or never specify values.'
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-return** for documentation.
11
-
12
- It adds support for functions that return `void` or `Promise<void>`.
13
-
14
- :::danger warning
15
- If possible, it is recommended to use tsconfig's [`noImplicitReturns`](https://www.typescriptlang.org/tsconfig/#noImplicitReturns) option rather than this rule. `noImplicitReturns` is powered by TS's type information and control-flow analysis so it has better coverage than this rule.
16
- :::
17
-
18
- <Tabs>
19
- <TabItem value="❌ Incorrect">
20
-
21
- ```ts
22
- function foo(): undefined {}
23
- function bar(flag: boolean): undefined {
24
- if (flag) return foo();
25
- return;
26
- }
27
-
28
- async function baz(flag: boolean): Promise<undefined> {
29
- if (flag) return;
30
- return foo();
31
- }
32
- ```
33
-
34
- </TabItem>
35
- <TabItem value="✅ Correct">
36
-
37
- ```ts
38
- function foo(): void {}
39
- function bar(flag: boolean): void {
40
- if (flag) return foo();
41
- return;
42
- }
43
-
44
- async function baz(flag: boolean): Promise<void | number> {
45
- if (flag) return 42;
46
- return;
47
- }
48
- ```
49
-
50
- </TabItem>
51
- </Tabs>
@@ -1,196 +0,0 @@
1
- ---
2
- description: 'Enforce consistent usage of type assertions.'
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-assertions** for documentation.
11
-
12
- TypeScript provides two syntaxes for "type assertions":
13
-
14
- - Angle brackets: `<Type>value`
15
- - As: `value as Type`
16
-
17
- This rule aims to standardize the use of type assertion style across the codebase.
18
- Keeping to one syntax consistently helps with code readability.
19
-
20
- :::note
21
- Type assertions are also commonly referred as "type casting" in TypeScript.
22
- However, that term is technically slightly different to what is understood by type casting in other languages.
23
- Type assertions are a way to say to the TypeScript compiler, _"I know better than you, it's actually this different type!"_.
24
- :::
25
-
26
- [`const` assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions) are always allowed by this rule.
27
- Examples of them include `let x = "hello" as const;` and `let x = <const>"hello";`.
28
-
29
- ## Options
30
-
31
- ### `assertionStyle`
32
-
33
- {/* insert option description */}
34
-
35
- Valid values for `assertionStyle` are:
36
-
37
- - `as` will enforce that you always use `... as foo`.
38
- - `angle-bracket` will enforce that you always use `<foo>...`
39
- - `never` will enforce that you do not do any type assertions.
40
-
41
- Most codebases will want to enforce not using `angle-bracket` style because it conflicts with JSX syntax, and is confusing when paired with generic syntax.
42
-
43
- Some codebases like to go for an extra level of type safety, and ban assertions altogether via the `never` option.
44
-
45
- ### `objectLiteralTypeAssertions`
46
-
47
- {/* insert option description */}
48
-
49
- For example, this would prefer `const x: T = { ... };` to `const x = { ... } as T;` (or similar with angle brackets).
50
- The type assertion in the latter case is either unnecessary or will probably hide an error.
51
-
52
- The compiler will warn for excess properties with this syntax, but not missing _required_ fields. For example: `const x: { foo: number } = {};` will fail to compile, but `const x = {} as { foo: number }` will succeed.
53
-
54
- The const assertion `const x = { foo: 1 } as const`, introduced in TypeScript 3.4, is considered beneficial and is ignored by this option.
55
-
56
- Assertions to `any` are also ignored by this option.
57
-
58
- Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'never' }`:
59
-
60
- <Tabs>
61
- <TabItem value="❌ Incorrect">
62
-
63
- ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }'
64
- const x = { foo: 1 } as T;
65
-
66
- function bar() {
67
- return { foo: 1 } as T;
68
- }
69
- ```
70
-
71
- </TabItem>
72
- <TabItem value="✅ Correct">
73
-
74
- ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }'
75
- const x: T = { foo: 1 };
76
- const y = { foo: 1 } as any;
77
- const z = { foo: 1 } as unknown;
78
-
79
- function bar(): T {
80
- return { foo: 1 };
81
- }
82
- ```
83
-
84
- </TabItem>
85
- </Tabs>
86
-
87
- Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' }`:
88
-
89
- <Tabs>
90
- <TabItem value="❌ Incorrect">
91
-
92
- ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }'
93
- const x = { foo: 1 } as T;
94
-
95
- function bar() {
96
- return { foo: 1 } as T;
97
- }
98
- ```
99
-
100
- </TabItem>
101
- <TabItem value="✅ Correct">
102
-
103
- ```tsx option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }'
104
- const x: T = { foo: 1 };
105
- const y = { foo: 1 } as any;
106
- const z = { foo: 1 } as unknown;
107
- bar({ foo: 1 } as T);
108
- new Clazz({ foo: 1 } as T);
109
- function bar() {
110
- throw { foo: 1 } as Foo;
111
- }
112
- const foo = <Foo props={{ bar: 1 } as Bar} />;
113
- ```
114
-
115
- </TabItem>
116
- </Tabs>
117
-
118
- ### `arrayLiteralTypeAssertions`
119
-
120
- {/* insert option description */}
121
-
122
- For example, this would prefer `const x: T[] = [ ... ];` to `const x = [ ... ] as T[];` (or similar with angle brackets).
123
-
124
- The compiler will warn for excess properties of elements with this syntax, but not missing _required_ fields of those objects.
125
- For example: `const x: {foo: number}[] = [{}];` will fail to compile, but `const x = [{}] as [{ foo: number }]` will succeed.
126
-
127
- The const assertion `const x = [1, 2, 3] as const`, introduced in TypeScript 3.4, is considered beneficial and is ignored by this option.
128
-
129
- Assertions to `any` are also ignored by this option.
130
-
131
- Examples of code for `{ assertionStyle: 'as', arrayLiteralTypeAssertions: 'never' }`:
132
-
133
- <Tabs>
134
- <TabItem value="❌ Incorrect">
135
-
136
- ```ts option='{ "assertionStyle": "as", "arrayLiteralTypeAssertions": "never" }'
137
- const x = ['foo'] as T;
138
-
139
- function bar() {
140
- return ['foo'] as T;
141
- }
142
- ```
143
-
144
- </TabItem>
145
- <TabItem value="✅ Correct">
146
-
147
- ```ts option='{ "assertionStyle": "as", "arrayLiteralTypeAssertions": "never" }'
148
- const x: T = ['foo'];
149
- const y = ['foo'] as any;
150
- const z = ['foo'] as unknown;
151
-
152
- function bar(): T {
153
- return ['foo'];
154
- }
155
- ```
156
-
157
- </TabItem>
158
- </Tabs>
159
-
160
- Examples of code for `{ assertionStyle: 'as', arrayLiteralTypeAssertions: 'allow-as-parameter' }`:
161
-
162
- <Tabs>
163
- <TabItem value="❌ Incorrect">
164
-
165
- ```ts option='{ "assertionStyle": "as", "arrayLiteralTypeAssertions": "allow-as-parameter" }'
166
- const x = ['foo'] as T;
167
-
168
- function bar() {
169
- return ['foo'] as T;
170
- }
171
- ```
172
-
173
- </TabItem>
174
- <TabItem value="✅ Correct">
175
-
176
- ```tsx option='{ "assertionStyle": "as", "arrayLiteralTypeAssertions": "allow-as-parameter" }'
177
- const x: T = ['foo'];
178
- const y = ['foo'] as any;
179
- const z = ['foo'] as unknown;
180
- bar(['foo'] as T);
181
- new Clazz(['foo'] as T);
182
- function bar() {
183
- throw ['foo'] as Foo;
184
- }
185
- const foo = <Foo props={['foo'] as Bar} />;
186
- ```
187
-
188
- </TabItem>
189
- </Tabs>
190
-
191
- ## When Not To Use It
192
-
193
- If you do not want to enforce consistent type assertions.
194
-
195
- However, keep in mind that inconsistent style can harm readability in a project.
196
- We recommend picking a single option for this rule that works best for your project.
@@ -1,133 +0,0 @@
1
- ---
2
- description: 'Enforce type definitions to consistently use either `interface` or `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/consistent-type-definitions** for documentation.
11
-
12
- TypeScript provides two common ways to define an object type: `interface` and `type`.
13
-
14
- ```ts
15
- // type alias
16
- type T1 = {
17
- a: string;
18
- b: number;
19
- };
20
-
21
- // interface keyword
22
- interface T2 {
23
- a: string;
24
- b: number;
25
- }
26
- ```
27
-
28
- The two are generally very similar, and can often be used interchangeably.
29
- Using the same type declaration style consistently helps with code readability.
30
-
31
- ## Options
32
-
33
- - `'interface'` _(default)_: enforce using `interface`s for object type definitions.
34
- - `'type'`: enforce using `type`s for object type definitions.
35
-
36
- ### `'interface'`
37
-
38
- {/* insert option description */}
39
-
40
- <Tabs>
41
- <TabItem value="❌ Incorrect">
42
-
43
- ```ts option='"interface"'
44
- type T = { x: number };
45
- ```
46
-
47
- </TabItem>
48
- <TabItem value="✅ Correct">
49
-
50
- ```ts option='"interface"'
51
- type T = string;
52
- type Foo = string | {};
53
-
54
- interface T {
55
- x: number;
56
- }
57
- ```
58
-
59
- </TabItem>
60
- </Tabs>
61
-
62
- ### `'type'`
63
-
64
- {/* insert option description */}
65
-
66
- <Tabs>
67
- <TabItem value="❌ Incorrect">
68
-
69
- ```ts option='"type"'
70
- interface T {
71
- x: number;
72
- }
73
- ```
74
-
75
- </TabItem>
76
- <TabItem value="✅ Correct">
77
-
78
- ```ts option='"type"'
79
- type T = { x: number };
80
- ```
81
-
82
- </TabItem>
83
- </Tabs>
84
-
85
- ## FAQs
86
-
87
- ### What are the differences between `interface` and `type`?
88
-
89
- There are very few differences between interfaces and object types in TypeScript.
90
- Other than type aliases being used to represent union types, it is rare that you will need to choose one over the other.
91
-
92
- | Feature | Interfaces | Object Types | Explanation |
93
- | --------------------- | ---------- | ------------ | ------------------------------------------------------------------------------------------------------ |
94
- | Object shapes | ✅ | ✅ | Both can be used to represent general object shapes. |
95
- | General performance | ✅ | ✅ | Both are optimized for performance in TypeScript's type checker. |
96
- | Edge case performance | ✅ | | Large, complex logical types can be optimized better with interfaces by TypeScript's type checker. |
97
- | Traditional semantics | ✅ | | Interfaces are typically the default in much -though not all- of the TypeScript community. |
98
- | Non-object shapes | | ✅ | Object types may describe literals, primitives, unions, and intersections. |
99
- | Logical types | | ✅ | Object types may include conditional and mapped types. |
100
- | Merging | Allowed | Not allowed | Interfaces of the same name are treated as one interface ("merged"); type aliases may not share names. |
101
-
102
- We recommend choosing one definition style, using it when possible, and falling back to the other style when needed.
103
- The benefits of remaining consistent within a codebase almost always outweigh the benefits of either definition style.
104
-
105
- ### When do the performance differences between `interface` and `type` matter?
106
-
107
- Almost never.
108
- Most TypeScript projects do not -and should not- utilize types that exercise the performance differences between the two kinds of definitions.
109
-
110
- If you are having problems with type checking performance, see the [TypeScript Wiki's Performance page](https://github.com/microsoft/TypeScript/wiki/Performance).
111
-
112
- ### Why is the default `interface`?
113
-
114
- Interfaces are the prevailing, most common style in the TypeScript.
115
- `interface` has traditionally been TypeScript's intended ("semantic") way to convey _"an object with these fields"_.
116
-
117
- We generally recommend staying with the default, `'interface'`, to be stylistically consistent with the majority of TypeScript projects.
118
- If you strongly prefer `'type'`, that's fine too.
119
-
120
- ## When Not To Use It
121
-
122
- If you specifically want to manually choose whether to use an interface or type literal for stylistic reasons each time you define a type, you can avoid this rule.
123
-
124
- However, keep in mind that inconsistent style can harm readability in a project.
125
- We recommend picking a single option for this rule that works best for your project.
126
-
127
- You might occasionally need to a different definition type in specific cases, such as if your project is a dependency or dependent of another project that relies on a specific type definition style.
128
- 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.
129
-
130
- ## Further Reading
131
-
132
- - [TypeScript Handbook > Everyday Types > Differences Between Type Aliases and Interfaces](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces)
133
- - [StackOverflow: Interfaces vs Types in TypeScript](https://stackoverflow.com/questions/37233735/interfaces-vs-types-in-typescript)
@@ -1,97 +0,0 @@
1
- ---
2
- description: 'Enforce consistent usage of type exports.'
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-exports** for documentation.
11
-
12
- TypeScript allows specifying a `type` keyword on exports to indicate that the export exists only in the type system, not at runtime.
13
- This allows transpilers to drop exports 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
- ## Examples
18
-
19
- <Tabs>
20
- <TabItem value="❌ Incorrect">
21
-
22
- ```ts
23
- interface ButtonProps {
24
- onClick: () => void;
25
- }
26
-
27
- class Button implements ButtonProps {
28
- onClick = () => console.log('button!');
29
- }
30
-
31
- export { Button, ButtonProps };
32
- ```
33
-
34
- </TabItem>
35
- <TabItem value="✅ Correct">
36
-
37
- ```ts
38
- interface ButtonProps {
39
- onClick: () => void;
40
- }
41
-
42
- class Button implements ButtonProps {
43
- onClick = () => console.log('button!');
44
- }
45
-
46
- export { Button };
47
- export type { ButtonProps };
48
- ```
49
-
50
- </TabItem>
51
- </Tabs>
52
-
53
- ## Options
54
-
55
- ### `fixMixedExportsWithInlineTypeSpecifier`
56
-
57
- {/* insert option description */}
58
-
59
- If you are using a TypeScript version less than 4.5, then you will not be able to use this option.
60
-
61
- For example the following code:
62
-
63
- ```ts
64
- const x = 1;
65
- type T = number;
66
-
67
- export { x, T };
68
- ```
69
-
70
- With `{fixMixedExportsWithInlineTypeSpecifier: true}` will be fixed to:
71
-
72
- ```ts
73
- const x = 1;
74
- type T = number;
75
-
76
- export { x, type T };
77
- ```
78
-
79
- With `{fixMixedExportsWithInlineTypeSpecifier: false}` will be fixed to:
80
-
81
- ```ts
82
- const x = 1;
83
- type T = number;
84
-
85
- export type { T };
86
- export { x };
87
- ```
88
-
89
- ## When Not To Use It
90
-
91
- If you use `--isolatedModules` the compiler would error if a type is not re-exported using `export type`.
92
- This rule may be less useful in those cases.
93
-
94
- If you specifically want to use both export kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.
95
-
96
- However, keep in mind that inconsistent style can harm readability in a project.
97
- We recommend picking a single option for this rule that works best for your project.