-
-
Notifications
You must be signed in to change notification settings - Fork 2.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JoshuaKGoldberg
merged 36 commits into
typescript-eslint:v8
from
JoshuaKGoldberg:no-ban-types-splitup
Jun 13, 2024
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
452fe6d
feat(eslint-plugin): add no-restricted-types, no-uppercase-alias-type…
JoshuaKGoldberg 1791e4f
Fix lint issues
JoshuaKGoldberg 7c606fc
Apply suggestions from code review
JoshuaKGoldberg 7734da7
Kirk suggestions
JoshuaKGoldberg d1127f5
yarn test -u
JoshuaKGoldberg aae4be7
test: fix tests for useProgramFromProjectService
JoshuaKGoldberg c411cdb
Rename to no-wrapper-object-types
JoshuaKGoldberg 057b34a
excess myObject in docs
JoshuaKGoldberg d4baaef
Fix rules/index.ts
JoshuaKGoldberg f25f333
Merge branch 'v8' into no-ban-types-splitup
JoshuaKGoldberg 88bd835
Clarify ban-types deprecation message
JoshuaKGoldberg 874e97a
Remove ban-types altogether
JoshuaKGoldberg 1a854f1
fix website build with permalink to ban-types
JoshuaKGoldberg 48afe9d
Merge branch 'v8'
JoshuaKGoldberg 2bfa14b
Update configs
JoshuaKGoldberg 43fa798
Apply suggestions from code review
JoshuaKGoldberg c220790
fix ban-types.md
JoshuaKGoldberg 732e812
Update packages/eslint-plugin/docs/rules/no-wrapper-object-types.mdx
JoshuaKGoldberg b5e5410
lil nit
JoshuaKGoldberg 45df4d6
remove callable type
JoshuaKGoldberg fec4311
Split out no-unsafe-function-type too
JoshuaKGoldberg 01a6f4d
Merge branch 'v8'
JoshuaKGoldberg e357c8e
fix /blog link in ban-types.md
JoshuaKGoldberg 2508d84
Mention args: never
JoshuaKGoldberg 2a32c12
Regenerate configs
JoshuaKGoldberg 41db8fe
lil phrasing nit
JoshuaKGoldberg 22bec9d
chore: fix snapshot
JoshuaKGoldberg 3858d65
fix lint complaint
JoshuaKGoldberg e078105
Address feedback
JoshuaKGoldberg e22cbb9
Apply suggestions from code review
JoshuaKGoldberg 8872bdc
Mention concepts and equality
JoshuaKGoldberg 2e33cee
fix: scope analysis
JoshuaKGoldberg 184e4ee
ban-types.md rule mentions
JoshuaKGoldberg cad7178
Merge branch 'v8'
JoshuaKGoldberg 7d73bf3
fix no-restricted-types docs test
JoshuaKGoldberg 75543e9
wrong list, kronk
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
:::danger Deprecated | ||
|
||
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 resolved
Hide resolved
|
63 changes: 63 additions & 0 deletions
63
packages/eslint-plugin/docs/rules/no-unsafe-function-type.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.