@typescript-eslint/sort-type-constituents
Require the members of union and intersection types to be sorted
Also called typescript-eslint/sort-type-constituents, typescript/sort-type-constituents
A long union in no particular order is hard to scan, and a member that's listed twice is easy to miss. This rule keeps the members of A | B and A & B in a fixed order.
Members are first put in groups, with named types first and null, undefined, and void last. Inside a group they're sorted by their text, ignoring case.
--fix sorts the members. If there are comments between them, the fix would delete the comments, so it's a suggestion in your editor instead.
typescript-eslint has deprecated this rule. It recommends eslint-plugin-perfectionist instead, which isn't built into bun lint.
Examples#
type Status = "open" | "closed" | "merged"Union type Status constituents must be sorted.;
type Id = null | string | UserIdUnion type Id constituents must be sorted.;
type Props = WithStyle & WithChildrenIntersection type Props constituents must be sorted.;type Status = "closed" | "merged" | "open";
type Id = UserId | string | null;
type Props = WithChildren & WithStyle;
type Key = "alt" | "Shift";Options#
| Option | Default | What it does |
|---|---|---|
checkUnions | true | Check union types (|). |
checkIntersections | true | Check intersection types (&). |
caseSensitive | false | Compare by character code, so every uppercase letter comes before every lowercase one. |
groupOrder | every group, in the order listed here | The order of the groups: "named", "keyword", "operator", "literal", "function", "import", "conditional", "object", "tuple", "intersection", "union", "nullish". Groups you leave out come last, together. |
Not reported with groupOrder: ["nullish", "keyword", "named"]:
{ "groupOrder": ["nullish", "keyword", "named"] }No problemstype Id = null | string | UserId;Reported with caseSensitive: true, because S comes before a:
{ "caseSensitive": true }1 problemtype Key = "alt" | "Shift"Union type Key constituents must be sorted.;When not to use it#
The order of an intersection can matter when its members declare the same method with different signatures. Turn off checkIntersections if you rely on that.