Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add
no-redundant-type-constituents
rule (#4378)
- Loading branch information
1 parent
f106e4b
commit 63d051e
Showing
9 changed files
with
1,376 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
85 changes: 85 additions & 0 deletions
85
packages/eslint-plugin/docs/rules/no-redundant-type-constituents.md
This file contains 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,85 @@ | ||
# `no-redundant-type-constituents` | ||
|
||
Disallow members of unions and intersections that do nothing or override type information. | ||
|
||
## Rule Details | ||
|
||
Some types can override some other types ("constituents") in a union or intersection and/or be overridden by some other types. | ||
TypeScript's set theory of types includes cases where a constituent type might be useless in the parent union or intersection. | ||
|
||
Within `|` unions: | ||
|
||
- `any` and `unknown` "override" all other union members | ||
- `never` is dropped from unions in any position except when in a return type position | ||
- primitive types such as `string` "override" any of their literal types such as `""` | ||
|
||
Within `&` intersections: | ||
|
||
- `any` and `never` "override" all other intersection members | ||
- `unknown` is dropped from intersections | ||
- literal types "override" any primitive types in an intersection | ||
- literal types such as `""` "override" any of their primitive types such as `string` | ||
|
||
Examples of code for this rule: | ||
|
||
<!--tabs--> | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
type UnionAny = any | 'foo'; | ||
type UnionUnknown = unknown | 'foo'; | ||
type UnionNever = never | 'foo'; | ||
|
||
type UnionBooleanLiteral = boolean | false; | ||
type UnionNumberLiteral = number | 1; | ||
type UnionStringLiteral = string | 'foo'; | ||
|
||
type IntersectionAny = any & 'foo'; | ||
type IntersectionUnknown = string & unknown; | ||
type IntersectionNever = string | never; | ||
|
||
type IntersectionBooleanLiteral = boolean & false; | ||
type IntersectionNumberLiteral = number & 1; | ||
type IntersectionStringLiteral = string & 'foo'; | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
type UnionAny = any; | ||
type UnionUnknown = unknown; | ||
type UnionNever = never; | ||
|
||
type UnionBooleanLiteral = boolean; | ||
type UnionNumberLiteral = number; | ||
type UnionStringLiteral = string; | ||
|
||
type IntersectionAny = any; | ||
type IntersectionUnknown = string; | ||
type IntersectionNever = string; | ||
|
||
type IntersectionBooleanLiteral = false; | ||
type IntersectionNumberLiteral = 1; | ||
type IntersectionStringLiteral = 'foo'; | ||
|
||
type ReturnUnionNever = () => string | never; | ||
``` | ||
|
||
## Limitations | ||
|
||
This rule plays it safe and only works with bottom types, top types, and comparing literal types to primitive types. | ||
It also does not provide an auto-fixer just yet. | ||
|
||
## Further Reading | ||
|
||
- [Union Types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) | ||
- [Intersection Types](https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types) | ||
- [Bottom Types](https://en.wikipedia.org/wiki/Bottom_type) | ||
- [Top Types](https://en.wikipedia.org/wiki/Top_type) | ||
|
||
## Attributes | ||
|
||
- [ ] ✅ Recommended | ||
- [ ] 🔧 Fixable | ||
- [x] 💭 Requires type information |
This file contains 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 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.