-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): split no-empty-object-type out from ban-types and no-empty-interfaces #8977
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 22 commits into
typescript-eslint:v8
from
JoshuaKGoldberg:no-empty-object-type
May 12, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
bcfbb03
feat(eslint-plugin): split no-empty-object-type rule out from ban-typ…
JoshuaKGoldberg 173251c
Mention no-props
JoshuaKGoldberg b83cb50
Update packages/eslint-plugin/docs/rules/no-empty-object-type.mdx
JoshuaKGoldberg 39870f1
Update packages/eslint-plugin/docs/rules/no-empty-object-type.mdx
JoshuaKGoldberg f90ab3f
Allow in intersections, and touch up docs per suggestions
JoshuaKGoldberg 0a5c2bd
Update packages/eslint-plugin/docs/rules/no-empty-object-type.mdx
JoshuaKGoldberg c01f10e
Trimming
JoshuaKGoldberg f021d26
Update snapshots
JoshuaKGoldberg ca7fb0f
Finish removing Record
JoshuaKGoldberg fad7a5e
Correct phrasing on Object
JoshuaKGoldberg e8a2d7f
Merge with no-empty-interface
JoshuaKGoldberg 3971d71
nit the tip
JoshuaKGoldberg d33a246
Explicit report message for interfaces
JoshuaKGoldberg 28ed70d
Add in-type-alias-with-name
JoshuaKGoldberg 9b6dd1a
Switched to more general allowWithName
JoshuaKGoldberg e0bbd1d
Merge branch 'v8'
JoshuaKGoldberg b7a70b8
snapshot -u
JoshuaKGoldberg 713404a
snapshot -u
JoshuaKGoldberg 218d8e5
Fixed up unit tests
JoshuaKGoldberg 9987c2b
Update packages/eslint-plugin/docs/rules/no-empty-object-type.mdx
JoshuaKGoldberg bd764ef
docs touchups from Kirk
JoshuaKGoldberg 7dc88d7
replacedBy too
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
145 changes: 145 additions & 0 deletions
145
packages/eslint-plugin/docs/rules/no-empty-object-type.mdx
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
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,145 @@ | ||
--- | ||
description: 'Disallow accidentally using the "empty object" 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-empty-object-type** for documentation. | ||
|
||
The `{}`, or "empty object" type in TypeScript is a common source of confusion for developers unfamiliar with TypeScript's structural typing. | ||
`{}` represents any _non-nullish value_, including literals like `0` and `""`: | ||
|
||
```ts | ||
let anyNonNullishValue: {} = 'Intentionally allowed by TypeScript.'; | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
Often, developers writing `{}` actually mean either: | ||
|
||
- `object`: representing any _object_ value | ||
- `unknown`: representing any value at all, including `null` and `undefined` | ||
|
||
In other words, the "empty object" type `{}` really means _"any value that is defined"_. | ||
That includes arrays, class instances, functions, and primitives such as `string` and `symbol`. | ||
|
||
To avoid confusion around the `{}` type allowing any _non-nullish value_, this rule bans usage of the `{}` type. | ||
That includes interfaces and object type aliases with no fields. | ||
|
||
:::tip | ||
If you do have a use case for an API allowing `{}`, you can always configure the [rule's options](#options), use an [ESLint disable comment](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1), or [disable the rule in your ESLint config](https://eslint.org/docs/latest/use/configure/rules#using-configuration-files-1). | ||
::: | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Note that this rule does not report on: | ||
|
||
- `{}` as a type constituent in an intersection type (e.g. types like TypeScript's built-in `type NonNullable<T> = T & {}`), as this can be useful in type system operations. | ||
- Interfaces that extend from multiple other interfaces. | ||
|
||
## Examples | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts | ||
let anyObject: {}; | ||
let anyValue: {}; | ||
|
||
interface AnyObjectA {} | ||
interface AnyValueA {} | ||
|
||
type AnyObjectB = {}; | ||
type AnyValueB = {}; | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts | ||
let anyObject: object; | ||
let anyValue: unknown; | ||
|
||
type AnyObjectA = object; | ||
type AnyValueA = unknown; | ||
|
||
type AnyObjectB = object; | ||
type AnyValueB = unknown; | ||
|
||
let objectWith: { property: boolean }; | ||
|
||
interface InterfaceWith { | ||
property: boolean; | ||
} | ||
|
||
type TypeWith = { property: boolean }; | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Options | ||
|
||
By default, this rule flags both interfaces and object types. | ||
|
||
### `allowInterfaces` | ||
|
||
Whether to allow empty interfaces, as one of: | ||
|
||
- `'always'`: to always allow interfaces with no fields | ||
- `'never'` _(default)_: to never allow interfaces with no fields | ||
- `'with-single-extends'`: to allow empty interfaces that `extend` from a single base interface | ||
|
||
Examples of **correct** code for this rule with `{ allowInterfaces: 'with-single-extends' }`: | ||
|
||
```ts option='{ "allowInterfaces": "with-single-extends" }' showPlaygroundButton | ||
interface Base { | ||
value: boolean; | ||
} | ||
|
||
interface Derived extends Base {} | ||
``` | ||
|
||
### `allowObjectTypes` | ||
|
||
Whether to allow empty object type literals, as one of: | ||
kirkwaiblinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- `'always'`: to always allow object type literals with no fields | ||
- `'never'` _(default)_: to never allow object type literals with no fields | ||
|
||
### `allowWithName` | ||
|
||
A stringified regular expression to allow interfaces and object type aliases with the configured name. | ||
This can be useful if your existing code style includes a pattern of declaring empty types with `{}` instead of `object`. | ||
|
||
Examples of code for this rule with `{ allowWithName: 'Props$' }`: | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts option='{ "allowWithName": "Props$" }' showPlaygroundButton | ||
interface InterfaceValue {} | ||
|
||
type TypeValue = {}; | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts option='{ "allowWithName": "Props$" }' showPlaygroundButton | ||
interface InterfaceProps {} | ||
|
||
type TypeProps = {}; | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## When Not To Use It | ||
|
||
If your code commonly needs to represent the _"any non-nullish value"_ type, this rule may not be for you. | ||
Projects that extensively use type operations such as conditional types and mapped types oftentimes benefit from disabling this rule. | ||
|
||
## Further Reading | ||
|
||
- [Enhancement: [ban-types] Split the {} ban into a separate, better-phrased rule](https://github.com/typescript-eslint/typescript-eslint/issues/8700) | ||
- [The Empty Object Type in TypeScript](https://www.totaltypescript.com/the-empty-object-type-in-typescript) | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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
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 |
---|---|---|
|
@@ -73,7 +73,10 @@ const defaultTypes: Types = { | |
message: 'Use bigint instead', | ||
fixWith: 'bigint', | ||
}, | ||
|
||
Object: { | ||
message: 'Use object instead', | ||
fixWith: 'object', | ||
}, | ||
Function: { | ||
message: [ | ||
'The `Function` type accepts any function-like value.', | ||
|
@@ -82,32 +85,6 @@ const defaultTypes: Types = { | |
'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.', | ||
].join('\n'), | ||
}, | ||
|
||
// object typing | ||
Object: { | ||
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. We didn't explicitly discuss |
||
message: [ | ||
'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.', | ||
'- If you want a type meaning "any object", you probably want `object` instead.', | ||
'- If you want a type meaning "any value", you probably want `unknown` instead.', | ||
'- If you really want a type meaning "any non-nullish value", you probably want `NonNullable<unknown>` instead.', | ||
].join('\n'), | ||
suggest: ['object', 'unknown', 'NonNullable<unknown>'], | ||
}, | ||
'{}': { | ||
message: [ | ||
'`{}` actually means "any non-nullish value".', | ||
'- If you want a type meaning "any object", you probably want `object` instead.', | ||
'- If you want a type meaning "any value", you probably want `unknown` instead.', | ||
'- If you want a type meaning "empty object", you probably want `Record<string, never>` instead.', | ||
'- If you really want a type meaning "any non-nullish value", you probably want `NonNullable<unknown>` instead.', | ||
].join('\n'), | ||
suggest: [ | ||
'object', | ||
'unknown', | ||
'Record<string, never>', | ||
'NonNullable<unknown>', | ||
], | ||
}, | ||
}; | ||
|
||
export const TYPE_KEYWORDS = { | ||
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.