sort-type-constituents
Enforce constituents of a type union/intersection to be sorted alphabetically.
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
Some problems reported by this rule are manually fixable by editor suggestions.
This rule has been deprecated in favor of the perfectionist/sort-intersection-types
and perfectionist/sort-union-types
rules.
See Docs: Deprecate sort-type-constituents in favor of eslint-plugin-perfectionist and eslint-plugin: Feature freeze naming and sorting stylistic rules for more information.
Sorting union (|
) and intersection (&
) types can help:
- keep your codebase standardized
- find repeated types
- reduce diff churn
This rule reports on any types that aren't sorted alphabetically.
Types are sorted case-insensitively and treating numbers like a human would, falling back to character code sorting in case of ties.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/sort-type-constituents": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/sort-type-constituents": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
type T1 = B | A;
type T2 = { b: string } & { a: string };
type T3 = [1, 2, 4] & [1, 2, 3];
type T4 =
| [1, 2, 4]
| [1, 2, 3]
| { b: string }
| { a: string }
| (() => void)
| (() => string)
| 'b'
| 'a'
| 'b'
| 'a'
| readonly string[]
| readonly number[]
| string[]
| number[]
| B
| A
| string
| any;
Open in Playgroundtype T1 = A | B;
type T2 = { a: string } & { b: string };
type T3 = [1, 2, 3] & [1, 2, 4];
type T4 =
| A
| B
| number[]
| string[]
| any
| string
| readonly number[]
| readonly string[]
| 'a'
| 'a'
| 'b'
| 'b'
| (() => string)
| (() => void)
| { a: string }
| { b: string }
| [1, 2, 3]
| [1, 2, 4];
Open in PlaygroundOptions
This rule accepts the following options:
type Options = [
{
/** Whether to sort using case sensitive string comparisons. */
caseSensitive?: boolean;
/** Whether to check intersection types (`&`). */
checkIntersections?: boolean;
/** Whether to check union types (`|`). */
checkUnions?: boolean;
/** Ordering of the groups. */
groupOrder?: (
| 'conditional'
| 'function'
| 'import'
| 'intersection'
| 'keyword'
| 'literal'
| 'named'
| 'nullish'
| 'object'
| 'operator'
| 'tuple'
| 'union'
)[];
},
];
const defaultOptions: Options = [
{
caseSensitive: false,
checkIntersections: true,
checkUnions: true,
groupOrder: [
'named',
'keyword',
'operator',
'literal',
'function',
'import',
'conditional',
'object',
'tuple',
'intersection',
'union',
'nullish',
],
},
];
caseSensitive
Whether to sort using case sensitive string comparisons. Default: false
.
Examples of code with { "caseSensitive": true }
:
- ❌ Incorrect
- ✅ Correct
type T = 'DeletedAt' | 'DeleteForever';
Open in Playgroundtype T = 'DeleteForever' | 'DeletedAt';
Open in PlaygroundcheckIntersections
Whether to check intersection types (&
). Default: true
.
Examples of code with { "checkIntersections": true }
(the default):
- ❌ Incorrect