AphrodyBun GitHub

Runtime docs · Linter · Rules

@typescript-eslint/sort-type-constituents

Require the members of union and intersection types to be sorted

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

Reported:

type Status = "open" | "closed" | "merged";
type Id = null | string | UserId;
type Props = WithStyle & WithChildren;

Not reported:

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"]:

type Id = null | string | UserId;

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

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.