1 | # `member-ordering`
|
2 |
|
3 | Require a consistent member declaration order.
|
4 |
|
5 | A consistent ordering of fields, methods and constructors can make interfaces, type literals, classes and class expressions easier to read, navigate, and edit.
|
6 |
|
7 | ## Rule Details
|
8 |
|
9 | This rule aims to standardize the way class declarations, class expressions, interfaces and type literals are structured and ordered.
|
10 |
|
11 | ## Options
|
12 |
|
13 | ```ts
|
14 | interface Options {
|
15 | default?: OrderConfig;
|
16 | classes?: OrderConfig;
|
17 | classExpressions?: OrderConfig;
|
18 | interfaces?: OrderConfig;
|
19 | typeLiterals?: OrderConfig;
|
20 | }
|
21 |
|
22 | type OrderConfig = MemberType[] | SortedOrderConfig | 'never';
|
23 |
|
24 | interface SortedOrderConfig {
|
25 | memberTypes?: MemberType[] | 'never';
|
26 | order: 'alphabetically' | 'alphabetically-case-insensitive' | 'as-written';
|
27 | }
|
28 |
|
29 | // See below for the more specific MemberType strings
|
30 | type MemberType = string | string[];
|
31 | ```
|
32 |
|
33 | You can configure `OrderConfig` options for:
|
34 |
|
35 | - **`default`**: all constructs (used as a fallback)
|
36 | - **`classes`**?: override ordering specifically for classes
|
37 | - **`classExpressions`**?: override ordering specifically for class expressions
|
38 | - **`interfaces`**?: override ordering specifically for interfaces
|
39 | - **`typeLiterals`**?: override ordering specifically for type literals
|
40 |
|
41 | The `OrderConfig` settings for each kind of construct may configure sorting on one or both two levels:
|
42 |
|
43 | - **`memberType`**: organizing on member type groups such as methods vs. properties
|
44 | - **`order`**: organizing based on member names, such as alphabetically
|
45 |
|
46 | ### Groups
|
47 |
|
48 | You can define many different groups based on different attributes of members.
|
49 | The supported member attributes are, in order:
|
50 |
|
51 | - **Accessibility** (`'public' | 'protected' | 'private'`)
|
52 | - **Decoration** (`'decorated'`): Whether the member has an explicit accessibility decorator
|
53 | - **Kind** (`'call-signature' | 'constructor' | 'field' | 'get' | 'method' | 'set' | 'signature'`)
|
54 |
|
55 | Member attributes may be joined with a `'-'` to combine into more specific groups.
|
56 | For example, `'public-field'` would come before `'private-field'`.
|
57 |
|
58 | ### Default configuration
|
59 |
|
60 | The default configuration looks as follows:
|
61 |
|
62 | ```jsonc
|
63 | {
|
64 | "default": [
|
65 | // Index signature
|
66 | "signature",
|
67 |
|
68 | // Fields
|
69 | "public-static-field",
|
70 | "protected-static-field",
|
71 | "private-static-field",
|
72 |
|
73 | "public-decorated-field",
|
74 | "protected-decorated-field",
|
75 | "private-decorated-field",
|
76 |
|
77 | "public-instance-field",
|
78 | "protected-instance-field",
|
79 | "private-instance-field",
|
80 |
|
81 | "public-abstract-field",
|
82 | "protected-abstract-field",
|
83 | "private-abstract-field",
|
84 |
|
85 | "public-field",
|
86 | "protected-field",
|
87 | "private-field",
|
88 |
|
89 | "static-field",
|
90 | "instance-field",
|
91 | "abstract-field",
|
92 |
|
93 | "decorated-field",
|
94 |
|
95 | "field",
|
96 |
|
97 | // Constructors
|
98 | "public-constructor",
|
99 | "protected-constructor",
|
100 | "private-constructor",
|
101 |
|
102 | "constructor",
|
103 |
|
104 | // Getters
|
105 | "public-static-get",
|
106 | "protected-static-get",
|
107 | "private-static-get",
|
108 |
|
109 | "public-decorated-get",
|
110 | "protected-decorated-get",
|
111 | "private-decorated-get",
|
112 |
|
113 | "public-instance-get",
|
114 | "protected-instance-get",
|
115 | "private-instance-get",
|
116 |
|
117 | "public-abstract-get",
|
118 | "protected-abstract-get",
|
119 | "private-abstract-get",
|
120 |
|
121 | "public-get",
|
122 | "protected-get",
|
123 | "private-get",
|
124 |
|
125 | "static-get",
|
126 | "instance-get",
|
127 | "abstract-get",
|
128 |
|
129 | "decorated-get",
|
130 |
|
131 | "get",
|
132 |
|
133 | // Setters
|
134 | "public-static-set",
|
135 | "protected-static-set",
|
136 | "private-static-set",
|
137 |
|
138 | "public-decorated-set",
|
139 | "protected-decorated-set",
|
140 | "private-decorated-set",
|
141 |
|
142 | "public-instance-set",
|
143 | "protected-instance-set",
|
144 | "private-instance-set",
|
145 |
|
146 | "public-abstract-set",
|
147 | "protected-abstract-set",
|
148 | "private-abstract-set",
|
149 |
|
150 | "public-set",
|
151 | "protected-set",
|
152 | "private-set",
|
153 |
|
154 | "static-set",
|
155 | "instance-set",
|
156 | "abstract-set",
|
157 |
|
158 | "decorated-set",
|
159 |
|
160 | "set",
|
161 |
|
162 | // Methods
|
163 | "public-static-method",
|
164 | "protected-static-method",
|
165 | "private-static-method",
|
166 |
|
167 | "public-decorated-method",
|
168 | "protected-decorated-method",
|
169 | "private-decorated-method",
|
170 |
|
171 | "public-instance-method",
|
172 | "protected-instance-method",
|
173 | "private-instance-method",
|
174 |
|
175 | "public-abstract-method",
|
176 | "protected-abstract-method",
|
177 | "private-abstract-method",
|
178 |
|
179 | "public-method",
|
180 | "protected-method",
|
181 | "private-method",
|
182 |
|
183 | "static-method",
|
184 | "instance-method",
|
185 | "abstract-method",
|
186 |
|
187 | "decorated-method",
|
188 |
|
189 | "method"
|
190 | ]
|
191 | }
|
192 | ```
|
193 |
|
194 | :::note
|
195 | The default configuration contains member group types which contain other member types.
|
196 | This is intentional to provide better error messages.
|
197 | :::
|
198 |
|
199 | :::tip
|
200 | By default, the members are not sorted.
|
201 | If you want to sort them alphabetically, you have to provide a custom configuration.
|
202 | :::
|
203 |
|
204 | ## Examples
|
205 |
|
206 | ### General Order on All Constructs
|
207 |
|
208 | This config specifies the order for all constructs.
|
209 | It ignores member types other than signatures, methods, constructors, and fields.
|
210 | It also ignores accessibility and scope.
|
211 |
|
212 | ```jsonc
|
213 | // .eslintrc.json
|
214 | {
|
215 | "rules": {
|
216 | "@typescript-eslint/member-ordering": [
|
217 | "error",
|
218 | { "default": ["signature", "method", "constructor", "field"] }
|
219 | ]
|
220 | }
|
221 | }
|
222 | ```
|
223 |
|
224 |
|
225 |
|
226 | #### ❌ Incorrect
|
227 |
|
228 | ```ts
|
229 | interface Foo {
|
230 | B: string; // -> field
|
231 |
|
232 | new (); // -> constructor
|
233 |
|
234 | A(): void; // -> method
|
235 |
|
236 | [Z: string]: any; // -> signature
|
237 | }
|
238 | ```
|
239 |
|
240 | ```ts
|
241 | type Foo = {
|
242 | B: string; // -> field
|
243 |
|
244 | // no constructor
|
245 |
|
246 | A(): void; // -> method
|
247 |
|
248 | // no signature
|
249 | };
|
250 | ```
|
251 |
|
252 | ```ts
|
253 | class Foo {
|
254 | private C: string; // -> field
|
255 | public D: string; // -> field
|
256 | protected static E: string; // -> field
|
257 |
|
258 | constructor() {} // -> constructor
|
259 |
|
260 | public static A(): void {} // -> method
|
261 | public B(): void {} // -> method
|
262 |
|
263 | [Z: string]: any; // -> signature
|
264 | }
|
265 | ```
|
266 |
|
267 | ```ts
|
268 | const Foo = class {
|
269 | private C: string; // -> field
|
270 | public D: string; // -> field
|
271 |
|
272 | constructor() {} // -> constructor
|
273 |
|
274 | public static A(): void {} // -> method
|
275 | public B(): void {} // -> method
|
276 |
|
277 | [Z: string]: any; // -> signature
|
278 |
|
279 | protected static E: string; // -> field
|
280 | };
|
281 | ```
|
282 |
|
283 | #### ✅ Correct
|
284 |
|
285 | ```ts
|
286 | interface Foo {
|
287 | [Z: string]: any; // -> signature
|
288 |
|
289 | A(): void; // -> method
|
290 |
|
291 | new (); // -> constructor
|
292 |
|
293 | B: string; // -> field
|
294 | }
|
295 | ```
|
296 |
|
297 | ```ts
|
298 | type Foo = {
|
299 | // no signature
|
300 |
|
301 | A(): void; // -> method
|
302 |
|
303 | // no constructor
|
304 |
|
305 | B: string; // -> field
|
306 | };
|
307 | ```
|
308 |
|
309 | ```ts
|
310 | class Foo {
|
311 | [Z: string]: any; // -> signature
|
312 |
|
313 | public static A(): void {} // -> method
|
314 | public B(): void {} // -> method
|
315 |
|
316 | constructor() {} // -> constructor
|
317 |
|
318 | private C: string; // -> field
|
319 | public D: string; // -> field
|
320 | protected static E: string; // -> field
|
321 | }
|
322 | ```
|
323 |
|
324 | ```ts
|
325 | const Foo = class {
|
326 | [Z: string]: any; // -> signature
|
327 |
|
328 | public static A(): void {} // -> method
|
329 | public B(): void {} // -> method
|
330 |
|
331 | constructor() {} // -> constructor
|
332 |
|
333 | private C: string; // -> field
|
334 | public D: string; // -> field
|
335 | protected static E: string; // -> field
|
336 | };
|
337 | ```
|
338 |
|
339 | ### Classes
|
340 |
|
341 | #### Public Instance Methods Before Public Static Fields
|
342 |
|
343 | This config specifies that public instance methods should come first before public static fields.
|
344 | Everything else can be placed anywhere.
|
345 | It doesn't apply to interfaces or type literals as accessibility and scope are not part of them.
|
346 |
|
347 | ```jsonc
|
348 | // .eslintrc.json
|
349 | {
|
350 | "rules": {
|
351 | "@typescript-eslint/member-ordering": [
|
352 | "error",
|
353 | { "default": ["public-instance-method", "public-static-field"] }
|
354 | ]
|
355 | }
|
356 | }
|
357 | ```
|
358 |
|
359 |
|
360 |
|
361 | ##### ❌ Incorrect
|
362 |
|
363 | ```ts
|
364 | class Foo {
|
365 | private C: string; // (irrelevant)
|
366 |
|
367 | public D: string; // (irrelevant)
|
368 |
|
369 | public static E: string; // -> public static field
|
370 |
|
371 | constructor() {} // (irrelevant)
|
372 |
|
373 | public static A(): void {} // (irrelevant)
|
374 |
|
375 | [Z: string]: any; // (irrelevant)
|
376 |
|
377 | public B(): void {} // -> public instance method
|
378 | }
|
379 | ```
|
380 |
|
381 | ```ts
|
382 | const Foo = class {
|
383 | private C: string; // (irrelevant)
|
384 |
|
385 | [Z: string]: any; // (irrelevant)
|
386 |
|
387 | public static E: string; // -> public static field
|
388 |
|
389 | public D: string; // (irrelevant)
|
390 |
|
391 | constructor() {} // (irrelevant)
|
392 |
|
393 | public static A(): void {} // (irrelevant)
|
394 |
|
395 | public B(): void {} // -> public instance method
|
396 | };
|
397 | ```
|
398 |
|
399 | ##### ✅ Correct
|
400 |
|
401 | ```ts
|
402 | class Foo {
|
403 | public B(): void {} // -> public instance method
|
404 |
|
405 | private C: string; // (irrelevant)
|
406 |
|
407 | public D: string; // (irrelevant)
|
408 |
|
409 | public static E: string; // -> public static field
|
410 |
|
411 | constructor() {} // (irrelevant)
|
412 |
|
413 | public static A(): void {} // (irrelevant)
|
414 |
|
415 | [Z: string]: any; // (irrelevant)
|
416 | }
|
417 | ```
|
418 |
|
419 | ```ts
|
420 | const Foo = class {
|
421 | public B(): void {} // -> public instance method
|
422 |
|
423 | private C: string; // (irrelevant)
|
424 |
|
425 | [Z: string]: any; // (irrelevant)
|
426 |
|
427 | public D: string; // (irrelevant)
|
428 |
|
429 | constructor() {} // (irrelevant)
|
430 |
|
431 | public static A(): void {} // (irrelevant)
|
432 |
|
433 | public static E: string; // -> public static field
|
434 | };
|
435 | ```
|
436 |
|
437 | #### Static Fields Before Instance Fields
|
438 |
|
439 | This config specifies that static fields should come before instance fields, with public static fields first.
|
440 | It doesn't apply to interfaces or type literals as accessibility and scope are not part of them.
|
441 |
|
442 | ```jsonc
|
443 | {
|
444 | "rules": {
|
445 | "@typescript-eslint/member-ordering": [
|
446 | "error",
|
447 | { "default": ["public-static-field", "static-field", "instance-field"] }
|
448 | ]
|
449 | }
|
450 | }
|
451 | ```
|
452 |
|
453 |
|
454 |
|
455 | ##### ❌ Incorrect
|
456 |
|
457 | ```ts
|
458 | class Foo {
|
459 | private E: string; // -> instance field
|
460 |
|
461 | private static B: string; // -> static field
|
462 | protected static C: string; // -> static field
|
463 | private static D: string; // -> static field
|
464 |
|
465 | public static A: string; // -> public static field
|
466 |
|
467 | [Z: string]: any; // (irrelevant)
|
468 | }
|
469 | ```
|
470 |
|
471 | ```ts
|
472 | const foo = class {
|
473 | public T(): void {} // method (irrelevant)
|
474 |
|
475 | private static B: string; // -> static field
|
476 |
|
477 | constructor() {} // constructor (irrelevant)
|
478 |
|
479 | private E: string; // -> instance field
|
480 |
|
481 | protected static C: string; // -> static field
|
482 | private static D: string; // -> static field
|
483 |
|
484 | [Z: string]: any; // signature (irrelevant)
|
485 |
|
486 | public static A: string; // -> public static field
|
487 | };
|
488 | ```
|
489 |
|
490 | ##### ✅ Correct
|
491 |
|
492 | ```ts
|
493 | class Foo {
|
494 | public static A: string; // -> public static field
|
495 |
|
496 | private static B: string; // -> static field
|
497 | protected static C: string; // -> static field
|
498 | private static D: string; // -> static field
|
499 |
|
500 | private E: string; // -> instance field
|
501 |
|
502 | [Z: string]: any; // (irrelevant)
|
503 | }
|
504 | ```
|
505 |
|
506 | ```ts
|
507 | const foo = class {
|
508 | [Z: string]: any; // -> signature (irrelevant)
|
509 |
|
510 | public static A: string; // -> public static field
|
511 |
|
512 | constructor() {} // -> constructor (irrelevant)
|
513 |
|
514 | private static B: string; // -> static field
|
515 | protected static C: string; // -> static field
|
516 | private static D: string; // -> static field
|
517 |
|
518 | private E: string; // -> instance field
|
519 |
|
520 | public T(): void {} // -> method (irrelevant)
|
521 | };
|
522 | ```
|
523 |
|
524 | #### Class Declarations
|
525 |
|
526 | This config only specifies an order for classes: methods, then the constructor, then fields.
|
527 | It does not apply to class expressions (use `classExpressions` for them).
|
528 | Default settings will be used for class declarations and all other syntax constructs other than class declarations.
|
529 |
|
530 | ```jsonc
|
531 | // .eslintrc.json
|
532 | {
|
533 | "rules": {
|
534 | "@typescript-eslint/member-ordering": [
|
535 | "error",
|
536 | { "classes": ["method", "constructor", "field"] }
|
537 | ]
|
538 | }
|
539 | }
|
540 | ```
|
541 |
|
542 |
|
543 |
|
544 | ##### ❌ Incorrect
|
545 |
|
546 | ```ts
|
547 | class Foo {
|
548 | private C: string; // -> field
|
549 | public D: string; // -> field
|
550 | protected static E: string; // -> field
|
551 |
|
552 | constructor() {} // -> constructor
|
553 |
|
554 | public static A(): void {} // -> method
|
555 | public B(): void {} // -> method
|
556 | }
|
557 | ```
|
558 |
|
559 | ##### ✅ Correct
|
560 |
|
561 | ```ts
|
562 | class Foo {
|
563 | public static A(): void {} // -> method
|
564 | public B(): void {} // -> method
|
565 |
|
566 | constructor() {} // -> constructor
|
567 |
|
568 | private C: string; // -> field
|
569 | public D: string; // -> field
|
570 | protected static E: string; // -> field
|
571 | }
|
572 | ```
|
573 |
|
574 | #### Class Expressions
|
575 |
|
576 | This config only specifies an order for classes expressions: methods, then the constructor, then fields.
|
577 | It does not apply to class declarations (use `classes` for them).
|
578 | Default settings will be used for class declarations and all other syntax constructs other than class expressions.
|
579 |
|
580 | ```jsonc
|
581 | // .eslintrc.json
|
582 | {
|
583 | "rules": {
|
584 | "@typescript-eslint/member-ordering": [
|
585 | "error",
|
586 | { "classExpressions": ["method", "constructor", "field"] }
|
587 | ]
|
588 | }
|
589 | }
|
590 | ```
|
591 |
|
592 |
|
593 |
|
594 | ##### ❌ Incorrect
|
595 |
|
596 | ```ts
|
597 | const foo = class {
|
598 | private C: string; // -> field
|
599 | public D: string; // -> field
|
600 | protected static E: string; // -> field
|
601 |
|
602 | constructor() {} // -> constructor
|
603 |
|
604 | public static A(): void {} // -> method
|
605 | public B(): void {} // -> method
|
606 | };
|
607 | ```
|
608 |
|
609 | ##### ✅ Correct
|
610 |
|
611 | ```ts
|
612 | const foo = class {
|
613 | public static A(): void {} // -> method
|
614 | public B(): void {} // -> method
|
615 |
|
616 | constructor() {} // -> constructor
|
617 |
|
618 | private C: string; // -> field
|
619 | public D: string; // -> field
|
620 | protected static E: string; // -> field
|
621 | };
|
622 | ```
|
623 |
|
624 | ### Interfaces
|
625 |
|
626 | This config only specifies an order for interfaces: signatures, then methods, then constructors, then fields.
|
627 | It does not apply to type literals (use `typeLiterals` for them).
|
628 | Default settings will be used for type literals and all other syntax constructs other than class expressions.
|
629 |
|
630 | :::note
|
631 | These member types are the only ones allowed for `interfaces`.
|
632 | :::
|
633 |
|
634 | ```jsonc
|
635 | // .eslintrc.json
|
636 | {
|
637 | "rules": {
|
638 | "@typescript-eslint/member-ordering": [
|
639 | "error",
|
640 | { "interfaces": ["signature", "method", "constructor", "field"] }
|
641 | ]
|
642 | }
|
643 | }
|
644 | ```
|
645 |
|
646 |
|
647 |
|
648 | #### ❌ Incorrect
|
649 |
|
650 | ```ts
|
651 | interface Foo {
|
652 | B: string; // -> field
|
653 |
|
654 | new (); // -> constructor
|
655 |
|
656 | A(): void; // -> method
|
657 |
|
658 | [Z: string]: any; // -> signature
|
659 | }
|
660 | ```
|
661 |
|
662 | #### ✅ Correct
|
663 |
|
664 | ```ts
|
665 | interface Foo {
|
666 | [Z: string]: any; // -> signature
|
667 |
|
668 | A(): void; // -> method
|
669 |
|
670 | new (); // -> constructor
|
671 |
|
672 | B: string; // -> field
|
673 | }
|
674 | ```
|
675 |
|
676 | ### Type Literals
|
677 |
|
678 | This config only specifies an order for type literals: signatures, then methods, then constructors, then fields.
|
679 | It does not apply to interfaces (use `interfaces` for them).
|
680 | Default settings will be used for interfaces and all other syntax constructs other than class expressions.
|
681 |
|
682 | :::note
|
683 | These member types are the only ones allowed for `typeLiterals`.
|
684 | :::
|
685 |
|
686 | ```jsonc
|
687 | // .eslintrc.json
|
688 | {
|
689 | "rules": {
|
690 | "@typescript-eslint/member-ordering": [
|
691 | "error",
|
692 | { "typeLiterals": ["signature", "method", "constructor", "field"] }
|
693 | ]
|
694 | }
|
695 | }
|
696 | ```
|
697 |
|
698 |
|
699 |
|
700 | #### ❌ Incorrect
|
701 |
|
702 | ```ts
|
703 | type Foo = {
|
704 | B: string; // -> field
|
705 |
|
706 | A(): void; // -> method
|
707 |
|
708 | new (); // -> constructor
|
709 |
|
710 | [Z: string]: any; // -> signature
|
711 | };
|
712 | ```
|
713 |
|
714 | #### ✅ Correct
|
715 |
|
716 | ```ts
|
717 | type Foo = {
|
718 | [Z: string]: any; // -> signature
|
719 |
|
720 | A(): void; // -> method
|
721 |
|
722 | new (); // -> constructor
|
723 |
|
724 | B: string; // -> field
|
725 | };
|
726 | ```
|
727 |
|
728 | ### Sorting Options
|
729 |
|
730 | #### Sorting Alphabetically Within Member Groups
|
731 |
|
732 | This config specifies that within each `memberTypes` group, members are in an alphabetic case-sensitive order.
|
733 | You can copy and paste the default order from [Default Configuration](#default-configuration).
|
734 |
|
735 | ```jsonc
|
736 | // .eslintrc.json
|
737 | {
|
738 | "rules": {
|
739 | "@typescript-eslint/member-ordering": [
|
740 | "error",
|
741 | {
|
742 | "default": {
|
743 | "memberTypes": [
|
744 | /* <Default Order> */
|
745 | ],
|
746 | "order": "alphabetically"
|
747 | }
|
748 | }
|
749 | ]
|
750 | }
|
751 | }
|
752 | ```
|
753 |
|
754 |
|
755 |
|
756 | ##### ❌ Incorrect
|
757 |
|
758 | ```ts
|
759 | interface Foo {
|
760 | a: x;
|
761 | B: x;
|
762 | c: x;
|
763 |
|
764 | B(): void;
|
765 | c(): void;
|
766 | a(): void;
|
767 | }
|
768 | ```
|
769 |
|
770 | ##### ✅ Correct
|
771 |
|
772 | ```ts
|
773 | interface Foo {
|
774 | B: x;
|
775 | a: x;
|
776 | c: x;
|
777 |
|
778 | B(): void;
|
779 | a(): void;
|
780 | c(): void;
|
781 | }
|
782 | ```
|
783 |
|
784 | #### Sorting Alphabetically Case Insensitive Within Member Groups
|
785 |
|
786 | This config specifies that within each `memberTypes` group, members are in an alphabetic case-sensitive order.
|
787 | You can copy and paste the default order from [Default Configuration](#default-configuration).
|
788 |
|
789 | ```jsonc
|
790 | // .eslintrc.json
|
791 | {
|
792 | "rules": {
|
793 | "@typescript-eslint/member-ordering": [
|
794 | "error",
|
795 | {
|
796 | "default": {
|
797 | "memberTypes": [
|
798 | /* <Default Order> */
|
799 | ],
|
800 | "order": "alphabetically-case-insensitive"
|
801 | }
|
802 | }
|
803 | ]
|
804 | }
|
805 | }
|
806 | ```
|
807 |
|
808 |
|
809 |
|
810 | ##### ❌ Incorrect
|
811 |
|
812 | ```ts
|
813 | interface Foo {
|
814 | B: x;
|
815 | a: x;
|
816 | c: x;
|
817 |
|
818 | B(): void;
|
819 | c(): void;
|
820 | a(): void;
|
821 | }
|
822 | ```
|
823 |
|
824 | ##### ✅ Correct
|
825 |
|
826 | ```ts
|
827 | interface Foo {
|
828 | a: x;
|
829 | B: x;
|
830 | c: x;
|
831 |
|
832 | a(): void;
|
833 | B(): void;
|
834 | c(): void;
|
835 | }
|
836 | ```
|
837 |
|
838 | #### Sorting Alphabetically Ignoring Member Groups
|
839 |
|
840 | This config specifies that members are all sorted in an alphabetic case-sensitive order.
|
841 | It ignores any member group types completely by specifying `"never"` for `memberTypes`.
|
842 |
|
843 | ```jsonc
|
844 | // .eslintrc.json
|
845 | {
|
846 | "rules": {
|
847 | "@typescript-eslint/member-ordering": [
|
848 | "error",
|
849 | { "default": { "memberTypes": "never", "order": "alphabetically" } }
|
850 | ]
|
851 | }
|
852 | }
|
853 | ```
|
854 |
|
855 |
|
856 |
|
857 | ##### ❌ Incorrect
|
858 |
|
859 | ```ts
|
860 | interface Foo {
|
861 | static c = 0;
|
862 | b(): void;
|
863 | a: boolean;
|
864 |
|
865 | [a: string]: number; // Order doesn't matter (no sortable identifier)
|
866 | new (): Bar; // Order doesn't matter (no sortable identifier)
|
867 | (): Baz; // Order doesn't matter (no sortable identifier)
|
868 | }
|
869 | ```
|
870 |
|
871 | ##### ✅ Correct
|
872 |
|
873 | ```ts
|
874 | interface Foo {
|
875 | a: boolean;
|
876 | b(): void;
|
877 | static c = 0;
|
878 |
|
879 | [a: string]: number; // Order doesn't matter (no sortable identifier)
|
880 | new (): Bar; // Order doesn't matter (no sortable identifier)
|
881 | (): Baz; // Order doesn't matter (no sortable identifier)
|
882 | }
|
883 | ```
|
884 |
|
885 | ## All Supported Options
|
886 |
|
887 | ### Member Types (Granular Form)
|
888 |
|
889 | There are multiple ways to specify the member types.
|
890 | The most explicit and granular form is the following:
|
891 |
|
892 | ```jsonc
|
893 | [
|
894 | // Index signature
|
895 | "signature",
|
896 |
|
897 | // Fields
|
898 | "public-static-field",
|
899 | "protected-static-field",
|
900 | "private-static-field",
|
901 | "public-decorated-field",
|
902 | "protected-decorated-field",
|
903 | "private-decorated-field",
|
904 | "public-instance-field",
|
905 | "protected-instance-field",
|
906 | "private-instance-field",
|
907 | "public-abstract-field",
|
908 | "protected-abstract-field",
|
909 | "private-abstract-field",
|
910 |
|
911 | // Constructors
|
912 | "public-constructor",
|
913 | "protected-constructor",
|
914 | "private-constructor",
|
915 |
|
916 | // Getters
|
917 | "public-static-get",
|
918 | "protected-static-get",
|
919 | "private-static-get",
|
920 |
|
921 | "public-decorated-get",
|
922 | "protected-decorated-get",
|
923 | "private-decorated-get",
|
924 |
|
925 | "public-instance-get",
|
926 | "protected-instance-get",
|
927 | "private-instance-get",
|
928 |
|
929 | "public-abstract-get",
|
930 | "protected-abstract-get",
|
931 | "private-abstract-get",
|
932 |
|
933 | "public-get",
|
934 | "protected-get",
|
935 | "private-get",
|
936 |
|
937 | "static-get",
|
938 | "instance-get",
|
939 | "abstract-get",
|
940 |
|
941 | "decorated-get",
|
942 |
|
943 | "get",
|
944 |
|
945 | // Setters
|
946 | "public-static-set",
|
947 | "protected-static-set",
|
948 | "private-static-set",
|
949 |
|
950 | "public-decorated-set",
|
951 | "protected-decorated-set",
|
952 | "private-decorated-set",
|
953 |
|
954 | "public-instance-set",
|
955 | "protected-instance-set",
|
956 | "private-instance-set",
|
957 |
|
958 | "public-abstract-set",
|
959 | "protected-abstract-set",
|
960 | "private-abstract-set",
|
961 |
|
962 | "public-set",
|
963 | "protected-set",
|
964 | "private-set",
|
965 |
|
966 | "static-set",
|
967 | "instance-set",
|
968 | "abstract-set",
|
969 |
|
970 | "decorated-set",
|
971 |
|
972 | "set",
|
973 |
|
974 | // Methods
|
975 | "public-static-method",
|
976 | "protected-static-method",
|
977 | "private-static-method",
|
978 | "public-decorated-method",
|
979 | "protected-decorated-method",
|
980 | "private-decorated-method",
|
981 | "public-instance-method",
|
982 | "protected-instance-method",
|
983 | "private-instance-method",
|
984 | "public-abstract-method",
|
985 | "protected-abstract-method",
|
986 | "private-abstract-method"
|
987 | ]
|
988 | ```
|
989 |
|
990 | :::note
|
991 | If you only specify some of the possible types, the non-specified ones can have any particular order.
|
992 | This means that they can be placed before, within or after the specified types and the linter won't complain about it.
|
993 | :::
|
994 |
|
995 | ### Member Group Types (With Accessibility, Ignoring Scope)
|
996 |
|
997 | It is also possible to group member types by their accessibility (`static`, `instance`, `abstract`), ignoring their scope.
|
998 |
|
999 | ```jsonc
|
1000 | [
|
1001 | // Index signature
|
1002 | // No accessibility for index signature.
|
1003 |
|
1004 | // Fields
|
1005 | "public-field", // = ["public-static-field", "public-instance-field"]
|
1006 | "protected-field", // = ["protected-static-field", "protected-instance-field"]
|
1007 | "private-field", // = ["private-static-field", "private-instance-field"]
|
1008 |
|
1009 | // Constructors
|
1010 | // Only the accessibility of constructors is configurable. See below.
|
1011 |
|
1012 | // Getters
|
1013 | "public-get", // = ["public-static-get", "public-instance-get"]
|
1014 | "protected-get", // = ["protected-static-get", "protected-instance-get"]
|
1015 | "private-get", // = ["private-static-get", "private-instance-get"]
|
1016 |
|
1017 | // Setters
|
1018 | "public-set", // = ["public-static-set", "public-instance-set"]
|
1019 | "protected-set", // = ["protected-static-set", "protected-instance-set"]
|
1020 | "private-set", // = ["private-static-set", "private-instance-set"]
|
1021 |
|
1022 | // Methods
|
1023 | "public-method", // = ["public-static-method", "public-instance-method"]
|
1024 | "protected-method", // = ["protected-static-method", "protected-instance-method"]
|
1025 | "private-method" // = ["private-static-method", "private-instance-method"]
|
1026 | ]
|
1027 | ```
|
1028 |
|
1029 | ### Member Group Types (With Accessibility and a Decorator)
|
1030 |
|
1031 | It is also possible to group methods or fields with a decorator separately, optionally specifying
|
1032 | their accessibility.
|
1033 |
|
1034 | ```jsonc
|
1035 | [
|
1036 | // Index signature
|
1037 | // No decorators for index signature.
|
1038 |
|
1039 | // Fields
|
1040 | "public-decorated-field",
|
1041 | "protected-decorated-field",
|
1042 | "private-decorated-field",
|
1043 |
|
1044 | "decorated-field", // = ["public-decorated-field", "protected-decorated-field", "private-decorated-field"]
|
1045 |
|
1046 | // Constructors
|
1047 | // There are no decorators for constructors.
|
1048 |
|
1049 | // Getters
|
1050 | "public-decorated-get",
|
1051 | "protected-decorated-get",
|
1052 | "private-decorated-get",
|
1053 |
|
1054 | "decorated-get" // = ["public-decorated-get", "protected-decorated-get", "private-decorated-get"]
|
1055 |
|
1056 | // Setters
|
1057 | "public-decorated-set",
|
1058 | "protected-decorated-set",
|
1059 | "private-decorated-set",
|
1060 |
|
1061 | "decorated-set" // = ["public-decorated-set", "protected-decorated-set", "private-decorated-set"]
|
1062 |
|
1063 | // Methods
|
1064 | "public-decorated-method",
|
1065 | "protected-decorated-method",
|
1066 | "private-decorated-method",
|
1067 |
|
1068 | "decorated-method" // = ["public-decorated-method", "protected-decorated-method", "private-decorated-method"]
|
1069 | ]
|
1070 | ```
|
1071 |
|
1072 | ### Member Group Types (With Scope, Ignoring Accessibility)
|
1073 |
|
1074 | Another option is to group the member types by their scope (`public`, `protected`, `private`), ignoring their accessibility.
|
1075 |
|
1076 | ```jsonc
|
1077 | [
|
1078 | // Index signature
|
1079 | // No scope for index signature.
|
1080 |
|
1081 | // Fields
|
1082 | "static-field", // = ["public-static-field", "protected-static-field", "private-static-field"]
|
1083 | "instance-field", // = ["public-instance-field", "protected-instance-field", "private-instance-field"]
|
1084 | "abstract-field", // = ["public-abstract-field", "protected-abstract-field", "private-abstract-field"]
|
1085 |
|
1086 | // Constructors
|
1087 | "constructor", // = ["public-constructor", "protected-constructor", "private-constructor"]
|
1088 |
|
1089 | // Getters
|
1090 | "static-get", // = ["public-static-get", "protected-static-get", "private-static-get"]
|
1091 | "instance-get", // = ["public-instance-get", "protected-instance-get", "private-instance-get"]
|
1092 | "abstract-get" // = ["public-abstract-get", "protected-abstract-get", "private-abstract-get"]
|
1093 |
|
1094 | // Setters
|
1095 | "static-set", // = ["public-static-set", "protected-static-set", "private-static-set"]
|
1096 | "instance-set", // = ["public-instance-set", "protected-instance-set", "private-instance-set"]
|
1097 | "abstract-set" // = ["public-abstract-set", "protected-abstract-set", "private-abstract-set"]
|
1098 |
|
1099 | // Methods
|
1100 | "static-method", // = ["public-static-method", "protected-static-method", "private-static-method"]
|
1101 | "instance-method", // = ["public-instance-method", "protected-instance-method", "private-instance-method"]
|
1102 | "abstract-method" // = ["public-abstract-method", "protected-abstract-method", "private-abstract-method"]
|
1103 | ]
|
1104 | ```
|
1105 |
|
1106 | ### Member Group Types (With Scope and Accessibility)
|
1107 |
|
1108 | The third grouping option is to ignore both scope and accessibility.
|
1109 |
|
1110 | ```jsonc
|
1111 | [
|
1112 | // Index signature
|
1113 | // No grouping for index signature.
|
1114 |
|
1115 | // Fields
|
1116 | "field", // = ["public-static-field", "protected-static-field", "private-static-field", "public-instance-field", "protected-instance-field", "private-instance-field",
|
1117 | // "public-abstract-field", "protected-abstract-field", private-abstract-field"]
|
1118 |
|
1119 | // Constructors
|
1120 | // Only the accessibility of constructors is configurable.
|
1121 |
|
1122 | // Getters
|
1123 | "get" // = ["public-static-get", "protected-static-get", "private-static-get", "public-instance-get", "protected-instance-get", "private-instance-get",
|
1124 | // "public-abstract-get", "protected-abstract-get", "private-abstract-get"]
|
1125 |
|
1126 | // Setters
|
1127 | "set" // = ["public-static-set", "protected-static-set", "private-static-set", "public-instance-set", "protected-instance-set", "private-instance-set",
|
1128 | // "public-abstract-set", "protected-abstract-set", "private-abstract-set"]
|
1129 |
|
1130 | // Methods
|
1131 | "method" // = ["public-static-method", "protected-static-method", "private-static-method", "public-instance-method", "protected-instance-method", "private-instance-method",
|
1132 | // "public-abstract-method", "protected-abstract-method", "private-abstract-method"]
|
1133 | ]
|
1134 | ```
|
1135 |
|
1136 | ### Grouping Different Member Types at the Same Rank
|
1137 |
|
1138 | It is also possible to group different member types at the same rank.
|
1139 |
|
1140 | ```jsonc
|
1141 | [
|
1142 | // Index signature
|
1143 | "signature",
|
1144 |
|
1145 | // Fields
|
1146 | "field",
|
1147 |
|
1148 | // Constructors
|
1149 | "constructor",
|
1150 |
|
1151 | // Getters and Setters at the same rank
|
1152 | ["get", "set"],
|
1153 |
|
1154 | // Methods
|
1155 | "method"
|
1156 | ]
|
1157 | ```
|
1158 |
|
1159 | ## When Not To Use It
|
1160 |
|
1161 | If you don't care about the general order of your members, then you will not need this rule.
|
1162 |
|
1163 | ## Related To
|
1164 |
|
1165 | - TSLint: [member-ordering](https://palantir.github.io/tslint/rules/member-ordering/)
|
1166 |
|
1167 | ## Attributes
|
1168 |
|
1169 | - Configs:
|
1170 | - [ ] ✅ Recommended
|
1171 | - [ ] 🔒 Strict
|
1172 | - [ ] 🔧 Fixable
|
1173 | - [ ] 💭 Requires type information
|