-
Notifications
You must be signed in to change notification settings - Fork 31
/
sort-intersection-types.md
148 lines (111 loc) · 3.37 KB
/
sort-intersection-types.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
title: sort-intersection-types
description: ESLint Plugin Perfectionist rule which enforce sorted intersection types in TypeScript
---
# sort-intersection-types
💼 This rule is enabled in the following [configs](/configs/): `recommended-alphabetical`, `recommended-line-length`, `recommended-natural`.
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
<!-- end auto-generated rule header -->
## 📖 Rule Details
Enforce sorted intersection types in TypeScript.
Adhering to the `sort-intersection-types` rule enables developers to ensure that intersection types are consistently sorted, resulting in cleaner and more maintainable code. This rule promotes a standardized ordering of intersection types, making it easier for developers to navigate and understand the structure of type intersections within the codebase.
:::info Important
If you use the [`sort-type-constituents`](https://typescript-eslint.io/rules/sort-type-constituents) rule from the [`@typescript-eslint/eslint-plugin`](https://typescript-eslint.io) plugin, it is highly recommended to [disable it](https://eslint.org/docs/latest/use/configure/rules#using-configuration-files-1) to avoid conflicts.
:::
## 💡 Examples
::: code-group
<!-- prettier-ignore -->
```ts [Alphabetical and Natural Sorting]
// ❌ Incorrect
type NetworkState =
& Failed
& LoadedFromCache
& Success
& DataLoading
// ✅ Correct
type NetworkState =
& DataLoading
& Failed
& LoadedFromCache
& Success
```
<!-- prettier-ignore -->
```ts [Sorting by Line Length]
// ❌ Incorrect
type NetworkState =
& Failed
& LoadedFromCache
& Success
& DataLoading
// ✅ Correct
type NetworkState =
& LoadedFromCache
& DataLoading
& Success
& Failed
```
:::
## 🔧 Options
This rule accepts an options object with the following properties:
```ts
interface Options {
type?: 'alphabetical' | 'natural' | 'line-length'
order?: 'asc' | 'desc'
'ignore-case'?: boolean
}
```
### type
<sub>(default: `'alphabetical'`)</sub>
- `alphabetical` - sort alphabetically.
- `natural` - sort in natural order.
- `line-length` - sort by code line length.
### order
<sub>(default: `'asc'`)</sub>
- `asc` - enforce properties to be in ascending order.
- `desc` - enforce properties to be in descending order.
### ignore-case
<sub>(default: `false`)</sub>
Only affects alphabetical and natural sorting. When `true` the rule ignores the case-sensitivity of the order.
## ⚙️ Usage
::: code-group
```json [Legacy Config]
// .eslintrc
{
"plugins": ["perfectionist"],
"rules": {
"perfectionist/sort-intersection-types": [
"error",
{
"type": "natural",
"order": "asc"
}
]
}
}
```
```js [Flat Config]
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'
export default [
{
plugins: {
perfectionist,
},
rules: {
'perfectionist/sort-intersection-types': [
'error',
{
type: 'natural',
order: 'asc',
},
],
},
},
]
```
:::
## 🚀 Version
This rule was introduced in v0.4.0.
## 📚 Resources
- [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-intersection-types.ts)
- [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/sort-intersection-types.test.ts)