@typescript-eslint/sort-type-constituents

Require the members of union and intersection types to be sorted

deprecatedfixable

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#

example.ts3 problems
type Status = "open" | "closed" | "merged";
type Id = null | string | UserId;
type Props = WithStyle & WithChildren;
example.tsNo problems
type Status = "closed" | "merged" | "open";
type Id = UserId | string | null;
type Props = WithChildren & WithStyle;
type Key = "alt" | "Shift";

Options#

OptionDefaultWhat it does
checkUnionstrueCheck union types (|).
checkIntersectionstrueCheck intersection types (&).
caseSensitivefalseCompare by character code, so every uppercase letter comes before every lowercase one.
groupOrderevery group, in the order listed hereThe 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"]:

example.tswith { "groupOrder": ["nullish", "keyword", "named"] }No problems
type Id = null | string | UserId;

Reported with caseSensitive: true, because S comes before a:

example.tswith { "caseSensitive": true }1 problem
type Key = "alt" | "Shift";

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.