Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): replace ban-types with no-restricted-types, no-unsafe-function-type, no-wrapper-object-types#9102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
452fe6d
1791e4f
7c606fc
7734da7
d1127f5
aae4be7
c411cdb
057b34a
d4baaef
f25f333
88bd835
874e97a
1a854f1
48afe9d
2bfa14b
43fa798
c220790
732e812
b5e5410
45df4d6
fec4311
01a6f4d
e357c8e
2508d84
2a32c12
41db8fe
22bec9d
3858d65
e078105
e22cbb9
8872bdc
2e33cee
184e4ee
cad7178
7d73bf3
75543e9
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
:::danger Deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. | ||
The old `ban-types` rule encompassed multiple areas of functionality, and so has been split into several rules. | ||
**[`no-restricted-types`](./no-restricted-types.mdx)** is the new rule for banning a configurable list of type names. | ||
It has no options enabled by default and is akin to rules like [`no-restricted-globals`](https://eslint.org/docs/latest/rules/no-restricted-globals), [`no-restricted-properties`](https://eslint.org/docs/latest/rules/no-restricted-properties), and [`no-restricted-syntax`](https://eslint.org/docs/latest/rules/no-restricted-syntax). | ||
The default options from `ban-types` are now covered by: | ||
- **[`no-empty-object-type`](./no-empty-object-type.mdx)**: banning the built-in `{}` type in confusing locations | ||
- **[`no-unsafe-function-type`](./no-unsafe-function-type.mdx)**: banning the built-in `Function` | ||
- **[`no-wrapper-object-types`](./no-wrapper-object-types.mdx)**: banning `Object` and built-in class wrappers such as `Number` | ||
`ban-types` itself is removed in typescript-eslint v8. | ||
See [Announcing typescript-eslint v8 Beta](/blog/announcing-typescript-eslint-v8-beta) for more details. | ||
::: | ||
<!-- This doc file has been left on purpose because `ban-types` is a well-known | ||
rule. This exists to help direct people to the replacement rules. | ||
Note that there is no actual way to get to this page in the normal navigation, | ||
so end-users will only be able to get to this page from the search bar. --> |
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
description: 'Disallow certain types.' | ||
--- | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-restricted-types** for documentation. | ||
It can sometimes be useful to ban specific types from being used in type annotations. | ||
For example, a project might be migrating from using one type to another, and want to ban references to the old type. | ||
This rule can be configured to ban a list of specific types and can suggest alternatives. | ||
Note that it does not ban the corresponding runtime objects from being used. | ||
## Options | ||
### `types` | ||
An object whose keys are the types you want to ban, and the values are error messages. | ||
The type can either be a type name literal (`OldType`) or a a type name with generic parameter instantiation(s) (`OldType<MyArgument>`). | ||
The values can be: | ||
- A string, which is the error message to be reported; or | ||
- `false` to specifically un-ban this type (useful when you are using `extendDefaults`); or | ||
- An object with the following properties: | ||
- `message: string`: the message to display when the type is matched. | ||
- `fixWith?: string`: a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done. | ||
- `suggest?: string[]`: a list of suggested replacements for the banned type. | ||
Example configuration: | ||
```jsonc | ||
{ | ||
"@typescript-eslint/no-restricted-types": [ | ||
"error", | ||
{ | ||
"types": { | ||
// add a custom message to help explain why not to use it | ||
"OldType": "Don't use OldType because it is unsafe", | ||
// add a custom message, and tell the plugin how to fix it | ||
"OldAPI": { | ||
"message": "Use NewAPI instead", | ||
"fixWith": "NewAPI", | ||
}, | ||
// add a custom message, and tell the plugin how to suggest a fix | ||
"SoonToBeOldAPI": { | ||
"message": "Use NewAPI instead", | ||
"suggest": ["NewAPIOne", "NewAPITwo"], | ||
}, | ||
}, | ||
}, | ||
], | ||
} | ||
``` | ||
## When Not To Use It | ||
If you have no need to ban specific types from being used in type annotations, you don't need this rule. | ||
## Related To | ||
- [`no-empty-object-type`](./no-empty-object-type.mdx) | ||
- [`no-unsafe-function-type`](./no-unsafe-function-type.mdx) | ||
- [`no-wrapper-object-types`](./no-wrapper-object-types.mdx) | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
description: 'Disallow using the unsafe built-in Function type.' | ||
--- | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-unsafe-function-type** for documentation. | ||
TypeScript's built-in `Function` type allows being called with any number of arguments and returns type `any`. | ||
`Function` also allows classes or plain objects that happen to possess all properties of the `Function` class. | ||
It's generally better to specify function parameters and return types with the function type syntax. | ||
"Catch-all" function types include: | ||
- `() => void`: a function that has no parameters and whose return is ignored | ||
- `(...args: never) => unknown`: a "top type" for functions that can be assigned any function type, but can't be called | ||
Examples of code for this rule: | ||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
```ts | ||
let noParametersOrReturn: Function; | ||
noParametersOrReturn = () => {}; | ||
let stringToNumber: Function; | ||
stringToNumber = (text: string) => text.length; | ||
let identity: Function; | ||
identity = value => value; | ||
``` | ||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
```ts | ||
let noParametersOrReturn: () => void; | ||
noParametersOrReturn = () => {}; | ||
let stringToNumber: (text: string) => number; | ||
stringToNumber = text => text.length; | ||
let identity: <T>(value: T) => T; | ||
identity = value => value; | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
## When Not To Use It | ||
If your project is still onboarding to TypeScript, it might be difficult to fully replace all unsafe `Function` types with more precise function types. | ||
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. | ||
## Related To | ||
- [`no-empty-object-type`](./no-empty-object-type.mdx) | ||
- [`no-restricted-types`](./no-restricted-types.mdx) | ||
- [`no-wrapper-object-types`](./no-wrapper-object-types.mdx) |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.