sort-type-constituents
Enforce constituents of a type union/intersection to be sorted alphabetically.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
此规则报告的一些问题可以通过编辑器 建议 手动修复。
此规则已被弃用,以支持 perfectionist/sort-intersection-types
和 perfectionist/sort-union-types
规则。
¥This rule has been deprecated in favor of the perfectionist/sort-intersection-types
and perfectionist/sort-union-types
rules.
有关更多信息,请参阅 文档:弃用 sort-type-constituents 而使用 eslint-plugin-perfectionist 和 eslint-plugin:功能冻结命名和排序样式规则。
¥See Docs: Deprecate sort-type-constituents in favor of eslint-plugin-perfectionist and eslint-plugin: Feature freeze naming and sorting stylistic rules for more information.
对并集(|
)和交集(&
)类型进行排序可以提供帮助:
¥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.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/sort-type-constituents": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/sort-type-constituents": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
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选项
该规则接受以下选项:
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 }
:
- ❌ Incorrect
- ✅ Correct
type T = 'DeletedAt' | 'DeleteForever';
Open in Playgroundtype T = 'DeleteForever' | 'DeletedAt';
Open in PlaygroundcheckIntersections
Whether to check intersection types (&
). Default: true
.
带有 { "checkIntersections": true }
(默认值)的代码示例:
¥Examples of code with { "checkIntersections": true }
(the default):
- ❌ Incorrect
- ✅ Correct
type ExampleIntersection = B & A;
Open in Playgroundtype ExampleIntersection = A & B;
Open in PlaygroundcheckUnions
Whether to check union types (|
). Default: true
.
带有 { "checkUnions": true }
(默认值)的代码示例:
¥Examples of code with { "checkUnions": true }
(the default):
- ❌ Incorrect
- ✅ Correct
type ExampleUnion = B | A;
Open in Playgroundtype ExampleUnion = A | B;
Open in PlaygroundgroupOrder
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
- 函数和构造函数类型(() => void
、new () => 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
- 关键字类型(any
、string
等)¥
keyword
- Keyword types (any
,string
, etc) -
literal
- 文字类型(1
、'b'
、true
等)¥
literal
- Literal types (1
,'b'
,true
, etc) -
named
- 命名类型 (A
、A['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 A
、typeof B
、readonly 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
-null
和undefined
¥
nullish
-null
andundefined
例如,使用 { "groupOrder": ["literal", "nullish" ]}
配置规则:
¥For example, configuring the rule with { "groupOrder": ["literal", "nullish" ]}
:
- ❌ Incorrect
- ✅ Correct
type ExampleGroup = null | 123;
Open in Playgroundtype ExampleGroup = 123 | null;
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.
'## 资源'