You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* chore(website): generate rule docs options automatically
* Apply suggestions from code review
Co-authored-by: Joshua Chen <[email protected]>
* First round of changes
* Simplify Prettier types
* Progress on using defs
* Progress, through the letter 'd' for rules
* Through explicit-module-boundary-types
* rip, ban-types defaults
* Cleaner separation for adding headers
* ...I meant headings
* Finished specifically base rule extensions
* Fix unit test, and enable debug cypress logging
* Fix deprecation notices, and ci.yml env indent
* Fix up lang and <sup>
* No, not sup
* Removed bunch more options docs cruft
* naming-convention, I give up
* Aha, a missing lint rule
* member-ordering, I give up on you too
* Several more docs automations
* Fixed options index
* Also above Related To
* Aha, jsx working
* Thanks lint - correct no-inferrable-types headings
* Update packages/eslint-plugin/src/rules/no-implicit-any-catch.ts
Co-authored-by: Brad Zacher <[email protected]>
* Fix explicit-function-return-type
Co-authored-by: Joshua Chen <[email protected]>
Co-authored-by: Brad Zacher <[email protected]>
Copy file name to clipboardExpand all lines: packages/eslint-plugin/docs/rules/ban-types.md
+68-93
Original file line number
Diff line number
Diff line change
@@ -6,80 +6,61 @@ description: 'Disallow certain types.'
6
6
>
7
7
> See **https://typescript-eslint.io/rules/ban-types** for documentation.
8
8
9
-
Some builtin types have aliases, some types are considered dangerous or harmful.
9
+
Some built-in types have aliases, while some types are considered dangerous or harmful.
10
10
It's often a good idea to ban certain types to help with consistency and safety.
11
11
12
12
## Rule Details
13
13
14
14
This rule bans specific types and can suggest alternatives.
15
15
Note that it does not ban the corresponding runtime objects from being used.
16
16
17
-
## Options
18
-
19
-
```ts
20
-
typeOptions= {
21
-
types?: {
22
-
[typeName:string]:
23
-
|false
24
-
|string
25
-
| {
26
-
message:string;
27
-
fixWith?:string;
28
-
};
29
-
};
30
-
extendDefaults?:boolean;
31
-
};
32
-
```
33
-
34
-
The rule accepts a single object as options.
35
-
36
-
### `types`
17
+
Examples of code with the default options:
37
18
38
-
An object whose keys are the types you want to ban, and the values are error messages.
19
+
<!--tabs-->
39
20
40
-
The type can either be a type name literal (`Foo`), a type name with generic parameter instantiation(s) (`Foo<Bar>`), the empty object literal (`{}`), or the empty tuple type (`[]`).
21
+
### ❌ Incorrect
41
22
42
-
The values can be:
23
+
```ts
24
+
// use lower-case primitives for consistency
25
+
const str:String='foo';
26
+
const bool:Boolean=true;
27
+
const num:Number=1;
28
+
const symb:Symbol=Symbol('foo');
29
+
const bigInt:BigInt=1n;
43
30
44
-
- A string, which is the error message to be reported; or
45
-
-`false` to specifically un-ban this type (useful when you are using `extendDefaults`); or
46
-
- An object with the following properties:
47
-
-`message: string` - the message to display when the type is matched.
48
-
-`fixWith?: string` - a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done.
31
+
// use a proper function type
32
+
const func:Function= () =>1;
49
33
50
-
### `extendDefaults`
34
+
// use safer object types
35
+
const lowerObj:Object= {};
36
+
const capitalObj:Object= { a: 'string' };
51
37
52
-
If you're specifying custom `types`, you can set this to `true` to extend the default `types` configuration. This is a convenience option to save you copying across the defaults when adding another type.
38
+
const curly1: {} =1;
39
+
const curly2: {} = { a: 'string' };
40
+
```
53
41
54
-
If this is `false`, the rule will _only_ use the types defined in your configuration.
42
+
### ✅ Correct
55
43
56
-
Example configuration:
44
+
```ts
45
+
// use lower-case primitives for consistency
46
+
const str:string='foo';
47
+
const bool:boolean=true;
48
+
const num:number=1;
49
+
const symb:symbol=Symbol('foo');
50
+
const bigInt:bigint=1n;
57
51
58
-
```jsonc
59
-
{
60
-
"@typescript-eslint/ban-types": [
61
-
"error",
62
-
{
63
-
"types": {
64
-
// add a custom message to help explain why not to use it
65
-
"Foo":"Don't use Foo because it is unsafe",
52
+
// use a proper function type
53
+
const func: () =>number= () =>1;
66
54
67
-
// add a custom message, AND tell the plugin how to fix it
The default options provide a set of "best practices", intended to provide safety and standardization in your codebase:
85
66
@@ -122,7 +103,6 @@ const defaultTypes = {
122
103
message: 'Use bigint instead',
123
104
fixWith: 'bigint',
124
105
},
125
-
126
106
Function: {
127
107
message: [
128
108
'The `Function` type accepts any function-like value.',
@@ -131,7 +111,6 @@ const defaultTypes = {
131
111
'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.',
132
112
].join('\n'),
133
113
},
134
-
135
114
// object typing
136
115
Object: {
137
116
message: [
@@ -152,52 +131,48 @@ const defaultTypes = {
152
131
153
132
</details>
154
133
155
-
### Examples
156
-
157
-
Examples of code with the default options:
158
-
159
-
<!--tabs-->
134
+
### `types`
160
135
161
-
#### ❌ Incorrect
136
+
An object whose keys are the types you want to ban, and the values are error messages.
162
137
163
-
```ts
164
-
// use lower-case primitives for consistency
165
-
const str:String='foo';
166
-
const bool:Boolean=true;
167
-
const num:Number=1;
168
-
const symb:Symbol=Symbol('foo');
169
-
const bigInt:BigInt=1n;
138
+
The type can either be a type name literal (`Foo`), a type name with generic parameter instantiation(s) (`Foo<Bar>`), the empty object literal (`{}`), or the empty tuple type (`[]`).
170
139
171
-
// use a proper function type
172
-
const func:Function= () =>1;
140
+
The values can be:
173
141
174
-
// use safer object types
175
-
const capitalObj1:Object=1;
176
-
const capitalObj2:Object= { a: 'string' };
142
+
- A string, which is the error message to be reported; or
143
+
-`false` to specifically un-ban this type (useful when you are using `extendDefaults`); or
144
+
- An object with the following properties:
145
+
-`message: string` - the message to display when the type is matched.
146
+
-`fixWith?: string` - a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done.
177
147
178
-
const curly1: {} =1;
179
-
const curly2: {} = { a: 'string' };
180
-
```
148
+
### `extendDefaults`
181
149
182
-
#### ✅ Correct
150
+
If you're specifying custom `types`, you can set this to `true` to extend the default `types` configuration. This is a convenience option to save you copying across the defaults when adding another type.
183
151
184
-
```ts
185
-
// use lower-case primitives for consistency
186
-
const str:string='foo';
187
-
const bool:boolean=true;
188
-
const num:number=1;
189
-
const symb:symbol=Symbol('foo');
190
-
const bigInt:bigint=1n;
152
+
If this is `false`, the rule will _only_ use the types defined in your configuration.
191
153
192
-
// use a proper function type
193
-
const func: () =>number= () =>1;
154
+
Example configuration:
194
155
195
-
// use safer object types
196
-
const lowerObj:object= {};
156
+
```jsonc
157
+
{
158
+
"@typescript-eslint/ban-types": [
159
+
"error",
160
+
{
161
+
"types": {
162
+
// add a custom message to help explain why not to use it
0 commit comments