UNPKG

3.18 kBMarkdownView Raw
1# `sort-type-union-intersection-members`
2
3Enforces that members of a type union/intersection are sorted alphabetically.
4
5Sorting union (`|`) and intersection (`&`) types can help:
6
7- keep your codebase standardized
8- find repeated types
9- reduce diff churn
10
11## Rule Details
12
13Sorting within each group is done using the following code:
14
15```ts
16const collator = new Intl.Collator('en', {
17 sensitivity: 'base',
18 numeric: true,
19});
20
21function compare(a, b) {
22 return collator.compare(a, b) || (a < b ? -1 : a > b ? 1 : 0);
23}
24```
25
26In other words, the types are sorted alphabetically, case-insensitively and treating numbers like a human would, falling back to character code sorting in case of ties.
27
28Examples of code for this rule:
29
30<!--tabs-->
31
32### ❌ Incorrect
33
34```ts
35type T1 = B | A;
36
37type T2 = { b: string } & { a: string };
38
39type T3 = [1, 2, 4] & [1, 2, 3];
40
41type T4 =
42 | [1, 2, 4]
43 | [1, 2, 3]
44 | { b: string }
45 | { a: string }
46 | (() => void)
47 | (() => string)
48 | 'b'
49 | 'a'
50 | 'b'
51 | 'a'
52 | readonly string[]
53 | readonly number[]
54 | string[]
55 | number[]
56 | B
57 | A
58 | string
59 | any;
60```
61
62### ✅ Correct
63
64```ts
65type T1 = A | B;
66
67type T2 = { a: string } & { b: string };
68
69type T3 = [1, 2, 3] & [1, 2, 4];
70
71type T4 =
72 | any
73 | string
74 | A
75 | B
76 | number[]
77 | string[]
78 | readonly number[]
79 | readonly string[]
80 | 'a'
81 | 'b'
82 | 'a'
83 | 'b'
84 | (() => string)
85 | (() => void)
86 | { a: string }
87 | { b: string }
88 | [1, 2, 3]
89 | [1, 2, 4];
90```
91
92## Options
93
94```ts
95type Options = {
96 // true to check intersection types, false otherwise
97 checkIntersections?: boolean;
98 // true to check union types, false otherwise
99 checkUnions?: boolean;
100 // the ordering of the groups
101 groupOrder?: (
102 | 'conditional'
103 | 'function'
104 | 'import'
105 | 'intersection'
106 | 'keyword'
107 | 'literal'
108 | 'named'
109 | 'object'
110 | 'operator'
111 | 'tuple'
112 | 'union'
113 | 'nullish'
114 )[];
115};
116
117const defaultOptions: Options = {
118 checkIntersections: true,
119 checkUnions: true,
120 groupOrder: [
121 'named',
122 'keyword',
123 'operator',
124 'literal',
125 'function',
126 'import',
127 'conditional',
128 'object',
129 'tuple',
130 'intersection',
131 'union',
132 'nullish',
133 ],
134};
135```
136
137### `groupOrder`
138
139Each member of the type is placed into a group, and then the rule sorts alphabetically within each group.
140The ordering of groups is determined by this option.
141
142- `conditional` - Conditional types (`A extends B ? C : D`)
143- `function` - Function and constructor types (`() => void`, `new () => type`)
144- `import` - Import types (`import('path')`)
145- `intersection` - Intersection types (`A & B`)
146- `keyword` - Keyword types (`any`, `string`, etc)
147- `literal` - Literal types (`1`, `'b'`, `true`, etc)
148- `named` - Named types (`A`, `A['prop']`, `B[]`, `Array<C>`)
149- `object` - Object types (`{ a: string }`, `{ [key: string]: number }`)
150- `operator` - Operator types (`keyof A`, `typeof B`, `readonly C[]`)
151- `tuple` - Tuple types (`[A, B, C]`)
152- `union` - Union types (`A | B`)
153- `nullish` - `null` and `undefined`
154
155## Attributes
156
157- Configs:
158 - [ ] ✅ Recommended
159 - [ ] 🔒 Strict
160- [x] 🔧 Fixable
161- [ ] 💭 Requires type information