sort-type-constituents
Enforce constituents of a type union/intersection to be sorted alphabetically.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
此规则报告的一些问题可以通过编辑器 建议 手动修复。
对并集 (|
) 和交集 (&
) 类型进行排序可以帮助:
英:Sorting union (|
) and intersection (&
) types can help:
- 保持代码库标准化
- 查找重复类型
- 减少差异流失
此规则报告未按字母顺序排序的任何类型。
英:This rule reports on any types that aren't sorted alphabetically.
类型的排序不区分大小写,并像人类一样对待数字,在出现平局时回退到字符代码排序。
module.exports = {
"rules": {
"@typescript-eslint/sort-type-constituents": "error"
}
};
示例
- ❌ 不正确
- ✅ 正确
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 Playground选项
checkIntersections
是否检查交叉点类型(&
)。
英:Whether to check intersection types (&
).
带有 { "checkIntersections": true }
(默认值)的代码示例:
英:Examples of code with { "checkIntersections": true }
(the default):
- ❌ 不正确
- ✅ 正确
type ExampleIntersection = B & A;
Open in Playgroundtype ExampleIntersection = A & B;
Open in PlaygroundcheckUnions
是否检查联合类型(|
)。
英:Whether to check union types (|
).
带有 { "checkUnions": true }
(默认值)的代码示例:
英:Examples of code with { "checkUnions": true }
(the default):
- ❌ 不正确
- ✅ 正确
type ExampleUnion = B | A;
Open in Playgroundtype ExampleUnion = A | B;
Open in PlaygroundgroupOrder
该类型的每个组成部分都被放入一个组中,然后该规则在每个组内按字母顺序排序。 组的顺序由该选项确定。
英: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
)function
- 函数和构造函数类型(() => void
、new () => type
)import
- 导入类型 (import('path')
)intersection
- 交集类型 (A & B
)keyword
- 关键字类型(any
、string
等)literal
- 字面量类型(1
、'b'
、true
等)named
- 命名类型(A
、A['prop']
、B[]
、Array<C>
)object
- 对象类型({ a: string }
、{ [key: string]: number }
)operator
- 运算符类型(keyof A
、typeof B
、readonly C[]
)tuple
- 元组类型 ([A, B, C]
)union
- 联合类型 (A | B
)nullish
-null
和undefined
例如,配置规则为 { "groupOrder": ["literal", "nullish" ]}
:
英:For example, configuring the rule with { "groupOrder": ["literal", "nullish" ]}
:
- ❌ 不正确
- ✅ 正确
type ExampleGroup = null | 123;
Open in Playgroundtype ExampleGroup = 123 | null;
Open in Playground何时不使用它
该规则纯粹是用于保持项目一致性的风格规则。 如果你不想让交集和并集类型保持一致、可预测的顺序,则可以将其关闭。 但是,请记住,不一致的风格可能会损害项目的可读性。
英: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.
选项
该规则接受以下选项
type Options = [
{
/** 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 = [
{
checkIntersections: true,
checkUnions: true,
groupOrder: [
'named',
'keyword',
'operator',
'literal',
'function',
'import',
'conditional',
'object',
'tuple',
'intersection',
'union',
'nullish',
],
},
];