Skip to main content

sort-type-constituents

Enforce constituents of a type union/intersection to be sorted alphabetically.

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。

对并集(|)和交集(&)类型进行排序可以提供帮助:

¥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.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/sort-type-constituents": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

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 Playground

选项

该规则接受以下选项:

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',
],
},
];

¥Options

caseSensitive

Whether to sort using case sensitive string comparisons. Default: false.

带有 { "caseSensitive": true } 的代码示例:

¥Examples of code with { "caseSensitive": true }:

type T = 'DeletedAt' | 'DeleteForever';
Open in Playground

checkIntersections

Whether to check intersection types (&). Default: true.

带有 { "checkIntersections": true }(默认值)的代码示例:

¥Examples of code with { "checkIntersections": true } (the default):

type ExampleIntersection = B & A;
Open in Playground

checkUnions

Whether to check union types (|). Default: true.

带有 { "checkUnions": true }(默认值)的代码示例:

¥Examples of code with { "checkUnions": true } (the default):

type ExampleUnion = B | A;
Open in Playground

groupOrder

Ordering of the groups. Default: ["named","keyword","operator","literal","function","import","conditional","object","tuple","intersection","union","nullish"].

该类型的每个组成部分都被放入一个组中,然后该规则在每个组内按字母顺序排序。组的顺序由该选项确定。

¥Each constituent of the type is placed into a group, and then the rule sorts alphabetically within each group. The ordering of groups is determined by this option.

  • conditional - 条件类型 (A extends B ? C : D)

    ¥conditional - Conditional types (A extends B ? C : D)

  • function - 函数和构造函数类型(() => voidnew () => type

    ¥function - Function and constructor types (() => void, new () => type)

  • import - 导入类型(import('path')

    ¥import - Import types (import('path'))

  • intersection - 交叉类型(A & B

    ¥intersection - Intersection types (A & B)

  • keyword - 关键字类型(anystring 等)

    ¥keyword - Keyword types (any, string, etc)

  • literal - 文字类型(1'b'true 等)

    ¥literal - Literal types (1, 'b', true, etc)

  • named - 命名类型 (AA['prop']B[]Array<C>)

    ¥named - Named types (A, A['prop'], B[], Array<C>)

  • object - 对象类型({ a: string }{ [key: string]: number })

    ¥object - Object types ({ a: string }, { [key: string]: number })

  • operator - 运算符类型(keyof Atypeof Breadonly C[]

    ¥operator - Operator types (keyof A, typeof B, readonly C[])

  • tuple - 元组类型 ([A, B, C])

    ¥tuple - Tuple types ([A, B, C])

  • union - 联合类型 (A | B)

    ¥union - Union types (A | B)

  • nullish - nullundefined

    ¥nullish - null and undefined

例如,使用 { "groupOrder": ["literal", "nullish" ]} 配置规则:

¥For example, configuring the rule with { "groupOrder": ["literal", "nullish" ]}:

type ExampleGroup = null | 123;
Open in Playground

何时不使用它

¥When Not To Use It

此规则对于其认为可变的内容非常严格。如果你不想让交集和并集类型保持一致、可预测的顺序,则可以将其关闭。但是,请记住,不一致的风格可能会损害项目的可读性。

¥This rule is purely a stylistic rule for maintaining consistency in your project. You can turn it off if you don't want to keep a consistent, predictable order for intersection and union types. However, keep in mind that inconsistent style can harm readability in a project.

'## 资源'