Skip to main content

member-ordering

Require a consistent member declaration order.


该规则旨在标准化类、接口和类型字面量的结构化和排序方式。 字段、方法和构造函数的一致顺序可以使代码更易于阅读、导航和编辑。

英:This rule aims to standardize the way classes, interfaces, and type literals are structured and ordered. A consistent ordering of fields, methods and constructors can make code easier to read, navigate, and edit.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/member-ordering": "error"
}
};
在线运行试试这个规则 ↗

选项

interface Options {
default?: OrderConfig;
classes?: OrderConfig;
classExpressions?: OrderConfig;
interfaces?: OrderConfig;
typeLiterals?: OrderConfig;
}

type OrderConfig = MemberType[] | SortedOrderConfig | 'never';

interface SortedOrderConfig {
memberTypes?: MemberType[] | 'never';
optionalityOrder?: 'optional-first' | 'required-first';
order:
| 'alphabetically'
| 'alphabetically-case-insensitive'
| 'as-written'
| 'natural'
| 'natural-case-insensitive';
}

// See below for the more specific MemberType strings
type MemberType = string | string[];

你可以配置 OrderConfig 选项:

英:You can configure OrderConfig options for:

  • default: 所有构造(用作后备)
  • classes?: 覆盖专门针对类的排序
  • classExpressions?: 重写专门针对类表达式的排序
  • interfaces?: 重写专门针对接口的排序
  • typeLiterals?: 专门针对类型字面量覆盖排序

每种构造的 OrderConfig 设置最多可以配置三个级别的排序:

英:The OrderConfig settings for each kind of construct may configure sorting on up to three levels:

  • memberTypes: 按成员类型组进行组织,例如方法与属性
  • optionalityOrder: 是将所有可选成员放在前面还是将所有必需成员放在前面
  • order: 根据成员名称进行组织,例如按字母顺序排列

群组

你可以根据成员的不同属性定义许多不同的组。 支持的成员属性按顺序为:

英:You can define many different groups based on different attributes of members. The supported member attributes are, in order:

  • 无障碍 ('public' | 'protected' | 'private' | '#private')
  • 装饰'decorated'): 成员是否具有显式可访问性装饰器
  • 种类 ('call-signature' | 'constructor' | 'field' | 'readonly-field' | 'get' | 'method' | 'set' | 'signature' | 'readonly-signature')

成员属性可以与 '-' 连接以组合成更具体的组。 例如,'public-field' 将出现在 'private-field' 之前。

英:Member attributes may be joined with a '-' to combine into more specific groups. For example, 'public-field' would come before 'private-field'.

命令

order 值指定组中成员的顺序。 它默认为 as-written,这意味着任何顺序都可以。 其他允许的值有:

英:The order value specifies what order members should be within a group. It defaults to as-written, meaning any order is fine. Other allowed values are:

  • alphabetically: 按 a-z 字母顺序排序,直接使用字符串 < 比较(所以 Ba 之前)
  • alphabetically-case-insensitive: 按 a-z 字母顺序排序,忽略大小写(因此 aB 之前)
  • natural: 与 alphabetically 相同,但使用 natural-compare-lite 对数字进行更友好的排序
  • natural-case-insensitive: 与 alphabetically-case-insensitive 相同,但使用 natural-compare-lite 对数字进行更友好的排序

默认配置

默认配置如下所示:

英:The default configuration looks as follows:

{
"default": [
// Index signature
"signature",
"call-signature",

// Fields
"public-static-field",
"protected-static-field",
"private-static-field",
"#private-static-field",

"public-decorated-field",
"protected-decorated-field",
"private-decorated-field",

"public-instance-field",
"protected-instance-field",
"private-instance-field",
"#private-instance-field",

"public-abstract-field",
"protected-abstract-field",

"public-field",
"protected-field",
"private-field",
"#private-field",

"static-field",
"instance-field",
"abstract-field",

"decorated-field",

"field",

// Static initialization
"static-initialization",

// Constructors
"public-constructor",
"protected-constructor",
"private-constructor",

"constructor",

// Accessors
"public-static-accessor",
"protected-static-accessor",
"private-static-accessor",
"#private-static-accessor",

"public-decorated-accessor",
"protected-decorated-accessor",
"private-decorated-accessor",

"public-instance-accessor",
"protected-instance-accessor",
"private-instance-accessor",
"#private-instance-accessor",

"public-abstract-accessor",
"protected-abstract-accessor",

"public-accessor",
"protected-accessor",
"private-accessor",
"#private-accessor",

"static-accessor",
"instance-accessor",
"abstract-accessor",

"decorated-accessor",

"accessor",

// Getters
"public-static-get",
"protected-static-get",
"private-static-get",
"#private-static-get",

"public-decorated-get",
"protected-decorated-get",
"private-decorated-get",

"public-instance-get",
"protected-instance-get",
"private-instance-get",
"#private-instance-get",

"public-abstract-get",
"protected-abstract-get",

"public-get",
"protected-get",
"private-get",
"#private-get",

"static-get",
"instance-get",
"abstract-get",

"decorated-get",

"get",

// Setters
"public-static-set",
"protected-static-set",
"private-static-set",
"#private-static-set",

"public-decorated-set",
"protected-decorated-set",
"private-decorated-set",

"public-instance-set",
"protected-instance-set",
"private-instance-set",
"#private-instance-set",

"public-abstract-set",
"protected-abstract-set",

"public-set",
"protected-set",
"private-set",
"#private-set",

"static-set",
"instance-set",
"abstract-set",

"decorated-set",

"set",

// Methods
"public-static-method",
"protected-static-method",
"private-static-method",
"#private-static-method",

"public-decorated-method",
"protected-decorated-method",
"private-decorated-method",

"public-instance-method",
"protected-instance-method",
"private-instance-method",
"#private-instance-method",

"public-abstract-method",
"protected-abstract-method",

"public-method",
"protected-method",
"private-method",
"#private-method",

"static-method",
"instance-method",
"abstract-method",

"decorated-method",

"method"
]
}
注意

默认配置包含成员组类型,其中包含其他成员类型。 这样做的目的是为了提供更好的错误消息。

提示

默认情况下,成员不排序。 如果你想按字母顺序对它们进行排序,则必须提供自定义配置。

示例

所有结构的通用命令

此配置指定所有构造的顺序。 它忽略除签名、方法、构造函数和字段之外的成员类型。 它还忽略了可访问性和范围。

英:This config specifies the order for all constructs. It ignores member types other than signatures, methods, constructors, and fields. It also ignores accessibility and scope.

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "default": ["signature", "method", "constructor", "field"] }
]
}
}
interface Foo {
B: string; // -> field

new (); // -> constructor

A(): void; // -> method

[Z: string]: any; // -> signature
}
Open in Playground
type Foo = {
B: string; // -> field

// no constructor

A(): void; // -> method

// no signature
};
Open in Playground
class Foo {
private C: string; // -> field
public D: string; // -> field
protected static E: string; // -> field

constructor() {} // -> constructor

public static A(): void {} // -> method
public B(): void {} // -> method

[Z: string]: any; // -> signature
}
Open in Playground
const Foo = class {
private C: string; // -> field
public D: string; // -> field

constructor() {} // -> constructor

public static A(): void {} // -> method
public B(): void {} // -> method

[Z: string]: any; // -> signature

protected static E: string; // -> field
};
Open in Playground

公共静态字段之前的公共实例方法

此配置指定公共实例方法应首先出现在公共静态字段之前。 其他一切都可以放在任何地方。 它不适用于接口或类型字面量,因为可访问性和范围不是它们的一部分。

英:This config specifies that public instance methods should come first before public static fields. Everything else can be placed anywhere. It doesn't apply to interfaces or type literals as accessibility and scope are not part of them.

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "default": ["public-instance-method", "public-static-field"] }
]
}
}
class Foo {
private C: string; // (irrelevant)

public D: string; // (irrelevant)

public static E: string; // -> public static field

constructor() {} // (irrelevant)

public static A(): void {} // (irrelevant)

[Z: string]: any; // (irrelevant)

public B(): void {} // -> public instance method
}
Open in Playground
const Foo = class {
private C: string; // (irrelevant)

[Z: string]: any; // (irrelevant)

public static E: string; // -> public static field

public D: string; // (irrelevant)

constructor() {} // (irrelevant)

public static A(): void {} // (irrelevant)

public B(): void {} // -> public instance method
};
Open in Playground

实例字段之前的静态字段

此配置指定静态字段应位于实例字段之前,首先是公共静态字段。 它不适用于接口或类型字面量,因为可访问性和范围不是它们的一部分。

英:This config specifies that static fields should come before instance fields, with public static fields first. It doesn't apply to interfaces or type literals as accessibility and scope are not part of them.

{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "default": ["public-static-field", "static-field", "instance-field"] }
]
}
}
class Foo {
private E: string; // -> instance field

private static B: string; // -> static field
protected static C: string; // -> static field
private static D: string; // -> static field

public static A: string; // -> public static field

[Z: string]: any; // (irrelevant)
}
Open in Playground
const foo = class {
public T(): void {} // method (irrelevant)

private static B: string; // -> static field

constructor() {} // constructor (irrelevant)

private E: string; // -> instance field

protected static C: string; // -> static field
private static D: string; // -> static field

[Z: string]: any; // signature (irrelevant)

public static A: string; // -> public static field
};
Open in Playground

类声明

此配置仅指定类的顺序: 方法,然后是构造函数,然后是字段。 它不适用于类表达式(使用 classExpressions)。 默认设置将用于类声明和除类声明之外的所有其他语法结构。

英:This config only specifies an order for classes: methods, then the constructor, then fields. It does not apply to class expressions (use classExpressions for them). Default settings will be used for class declarations and all other syntax constructs other than class declarations.

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "classes": ["method", "constructor", "field"] }
]
}
}
class Foo {
private C: string; // -> field
public D: string; // -> field
protected static E: string; // -> field

constructor() {} // -> constructor

public static A(): void {} // -> method
public B(): void {} // -> method
}
Open in Playground

类表达式

此配置仅指定类表达式的顺序: 方法,然后是构造函数,然后是字段。 它不适用于类声明(使用 classes)。 默认设置将用于类声明和除类表达式之外的所有其他语法构造。

英:This config only specifies an order for classes expressions: methods, then the constructor, then fields. It does not apply to class declarations (use classes for them). Default settings will be used for class declarations and all other syntax constructs other than class expressions.

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "classExpressions": ["method", "constructor", "field"] }
]
}
}
const foo = class {
private C: string; // -> field
public D: string; // -> field
protected static E: string; // -> field

constructor() {} // -> constructor

public static A(): void {} // -> method
public B(): void {} // -> method
};
Open in Playground

接口

此配置仅指定接口的顺序: 签名,然后是方法,然后是构造函数,然后是字段。 它不适用于类型字面量(使用 typeLiterals)。 默认设置将用于类型字面量和除类表达式之外的所有其他语法结构。

英:This config only specifies an order for interfaces: signatures, then methods, then constructors, then fields. It does not apply to type literals (use typeLiterals for them). Default settings will be used for type literals and all other syntax constructs other than class expressions.

注意

这些成员类型是 interfaces 唯一允许的成员类型。

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "interfaces": ["signature", "method", "constructor", "field"] }
]
}
}
interface Foo {
B: string; // -> field

new (); // -> constructor

A(): void; // -> method

[Z: string]: any; // -> signature
}
Open in Playground

类型字面量

此配置仅指定类型字面量的顺序: 签名,然后是方法,然后是构造函数,然后是字段。 它不适用于接口(使用 interfaces)。 默认设置将用于接口和除类表达式之外的所有其他语法结构。

英:This config only specifies an order for type literals: signatures, then methods, then constructors, then fields. It does not apply to interfaces (use interfaces for them). Default settings will be used for interfaces and all other syntax constructs other than class expressions.

注意

这些成员类型是 typeLiterals 唯一允许的成员类型。

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "typeLiterals": ["signature", "method", "constructor", "field"] }
]
}
}
type Foo = {
B: string; // -> field

A(): void; // -> method

new (); // -> constructor

[Z: string]: any; // -> signature
};
Open in Playground

排序选项

在成员组内按字母顺序排序

此配置指定在每个 memberTypes 组中,成员按字母顺序排列,区分大小写。 你可以复制并粘贴 默认配置 中的默认订单。

英:This config specifies that within each memberTypes group, members are in an alphabetic case-sensitive order. You can copy and paste the default order from Default Configuration.

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{
"default": {
"memberTypes": [
/* <Default Order> */
],
"order": "alphabetically"
}
}
]
}
}
interface Foo {
a: x;
B: x;
c: x;

B(): void;
c(): void;
a(): void;
}
Open in Playground

在成员组内按字母顺序排序(不区分大小写)

此配置指定在每个 memberTypes 组中,成员按字母顺序排列,不区分大小写。 你可以复制并粘贴 默认配置 中的默认订单。

英:This config specifies that within each memberTypes group, members are in an alphabetic case-insensitive order. You can copy and paste the default order from Default Configuration.

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{
"default": {
"memberTypes": [
/* <Default Order> */
],
"order": "alphabetically-case-insensitive"
}
}
]
}
}
interface Foo {
B: x;
a: x;
c: x;

B(): void;
c(): void;
a(): void;
}
Open in Playground

按字母顺序排序忽略成员组

此配置指定成员均按字母大小写顺序排序。 它通过为 memberTypes 指定 "never" 来完全忽略任何成员组类型。

英:This config specifies that members are all sorted in an alphabetic case-sensitive order. It ignores any member group types completely by specifying "never" for memberTypes.

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "default": { "memberTypes": "never", "order": "alphabetically" } }
]
}
}
interface Foo {
static c = 0;
b(): void;
a: boolean;

[a: string]: number; // Order doesn't matter (no sortable identifier)
new (): Bar; // Order doesn't matter (no sortable identifier)
(): Baz; // Order doesn't matter (no sortable identifier)
}
Open in Playground

对可选成员进行排序

可以启用 optionalityOrder 选项以将组中的所有可选成员放置在该组的开头或结尾。

英:The optionalityOrder option may be enabled to place all optional members in a group at the beginning or end of that group.

此配置将所有可选成员放在所有必需成员之前:

英:This config places all optional members before all required members:

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{
"default": {
"optionalityOrder": "optional-first",
"order": "alphabetically"
}
}
]
}
}
interface Foo {
a: boolean;
b?: number;
c: string;
}
Open in Playground

此配置将所有必需成员放在所有可选成员之前:

英:This config places all required members before all optional members:

// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{
"default": {
"optionalityOrder": "required-first",
"order": "alphabetically"
}
}
]
}
}
interface Foo {
a: boolean;
b?: number;
c: string;
}
Open in Playground

所有支持的选项

成员类型(细化形式)

有多种方法可以指定成员类型。 最明确和细粒度的形式如下:

英:There are multiple ways to specify the member types. The most explicit and granular form is the following:

[
// Index signature
"signature",
"readonly-signature",

// Fields
"public-static-field",
"public-static-readonly-field",
"protected-static-field",
"protected-static-readonly-field",
"private-static-field",
"private-static-readonly-field",
"#private-static-field",
"#private-static-readonly-field",

"public-decorated-field",
"public-decorated-readonly-field",
"protected-decorated-field",
"protected-decorated-readonly-field",
"private-decorated-field",
"private-decorated-readonly-field",

"public-instance-field",
"public-instance-readonly-field",
"protected-instance-field",
"protected-instance-readonly-field",
"private-instance-field",
"private-instance-readonly-field",
"#private-instance-field",
"#private-instance-readonly-field",

"public-abstract-field",
"public-abstract-readonly-field",
"protected-abstract-field",
"protected-abstract-readonly-field",

"public-field",
"public-readonly-field",
"protected-field",
"protected-readonly-field",
"private-field",
"private-readonly-field"
"#private-field",
"#private-readonly-field"

"static-field",
"static-readonly-field",
"instance-field",
"instance-readonly-field"
"abstract-field",
"abstract-readonly-field",

"decorated-field",
"decorated-readonly-field",

"field",
"readonly-field",

// Static initialization
"static-initialization",

// Constructors
"public-constructor",
"protected-constructor",
"private-constructor",

// Getters
"public-static-get",
"protected-static-get",
"private-static-get",
"#private-static-get",

"public-decorated-get",
"protected-decorated-get",
"private-decorated-get",

"public-instance-get",
"protected-instance-get",
"private-instance-get",
"#private-instance-get",

"public-abstract-get",
"protected-abstract-get",

"public-get",
"protected-get",
"private-get",
"#private-get",

"static-get",
"instance-get",
"abstract-get",

"decorated-get",

"get",

// Setters
"public-static-set",
"protected-static-set",
"private-static-set",
"#private-static-set",

"public-decorated-set",
"protected-decorated-set",
"private-decorated-set",

"public-instance-set",
"protected-instance-set",
"private-instance-set",
"#private-instance-set",

"public-abstract-set",
"protected-abstract-set",

"public-set",
"protected-set",
"private-set",

"static-set",
"instance-set",
"abstract-set",

"decorated-set",

"set",

// Methods
"public-static-method",
"protected-static-method",
"private-static-method",
"#private-static-method",
"public-decorated-method",
"protected-decorated-method",
"private-decorated-method",
"public-instance-method",
"protected-instance-method",
"private-instance-method",
"#private-instance-method",
"public-abstract-method",
"protected-abstract-method"
]
注意

如果你仅指定一些可能的类型,则未指定的类型可以具有任何特定顺序。 这意味着它们可以放置在指定类型之前、之内或之后,并且 linter 不会诉说它。

成员组类型(具有可访问性,忽略范围)

还可以根据成员类型的可访问性(staticinstanceabstract)对成员类型进行分组,而忽略其范围。

英:It is also possible to group member types by their accessibility (static, instance, abstract), ignoring their scope.

[
// Index signature
// No accessibility for index signature.

// Fields
"public-field", // = ["public-static-field", "public-instance-field"]
"protected-field", // = ["protected-static-field", "protected-instance-field"]
"private-field", // = ["private-static-field", "private-instance-field"]

// Static initialization
// No accessibility for static initialization.

// Constructors
// Only the accessibility of constructors is configurable. See below.

// Getters
"public-get", // = ["public-static-get", "public-instance-get"]
"protected-get", // = ["protected-static-get", "protected-instance-get"]
"private-get", // = ["private-static-get", "private-instance-get"]

// Setters
"public-set", // = ["public-static-set", "public-instance-set"]
"protected-set", // = ["protected-static-set", "protected-instance-set"]
"private-set", // = ["private-static-set", "private-instance-set"]

// Methods
"public-method", // = ["public-static-method", "public-instance-method"]
"protected-method", // = ["protected-static-method", "protected-instance-method"]
"private-method" // = ["private-static-method", "private-instance-method"]
]

成员组类型(具有辅助功能和装饰器)

还可以使用装饰器单独对方法或字段进行分组,可以选择指定它们的可访问性。

英:It is also possible to group methods or fields with a decorator separately, optionally specifying their accessibility.

[
// Index signature
// No decorators for index signature.

// Fields
"public-decorated-field",
"protected-decorated-field",
"private-decorated-field",

"decorated-field", // = ["public-decorated-field", "protected-decorated-field", "private-decorated-field"]

// Static initialization
// No decorators for static initialization.

// Constructors
// There are no decorators for constructors.

// Getters
"public-decorated-get",
"protected-decorated-get",
"private-decorated-get",

"decorated-get", // = ["public-decorated-get", "protected-decorated-get", "private-decorated-get"]

// Setters
"public-decorated-set",
"protected-decorated-set",
"private-decorated-set",

"decorated-set", // = ["public-decorated-set", "protected-decorated-set", "private-decorated-set"]

// Methods
"public-decorated-method",
"protected-decorated-method",
"private-decorated-method",

"decorated-method" // = ["public-decorated-method", "protected-decorated-method", "private-decorated-method"]
]

成员组类型(有范围,忽略可访问性)

另一种选择是按成员类型的范围(publicprotectedprivate)对成员类型进行分组,忽略其可访问性。

英:Another option is to group the member types by their scope (public, protected, private), ignoring their accessibility.

[
// Index signature
// No scope for index signature.

// Fields
"static-field", // = ["public-static-field", "protected-static-field", "private-static-field"]
"instance-field", // = ["public-instance-field", "protected-instance-field", "private-instance-field"]
"abstract-field", // = ["public-abstract-field", "protected-abstract-field"]

// Static initialization
// No scope for static initialization.

// Constructors
"constructor", // = ["public-constructor", "protected-constructor", "private-constructor"]

// Getters
"static-get", // = ["public-static-get", "protected-static-get", "private-static-get"]
"instance-get", // = ["public-instance-get", "protected-instance-get", "private-instance-get"]
"abstract-get", // = ["public-abstract-get", "protected-abstract-get"]

// Setters
"static-set", // = ["public-static-set", "protected-static-set", "private-static-set"]
"instance-set", // = ["public-instance-set", "protected-instance-set", "private-instance-set"]
"abstract-set", // = ["public-abstract-set", "protected-abstract-set"]

// Methods
"static-method", // = ["public-static-method", "protected-static-method", "private-static-method"]
"instance-method", // = ["public-instance-method", "protected-instance-method", "private-instance-method"]
"abstract-method" // = ["public-abstract-method", "protected-abstract-method"]
]

成员组类型(具有范围和可访问性)

第三个分组选项是忽略范围和可访问性。

英:The third grouping option is to ignore both scope and accessibility.

[
// Index signature
// No grouping for index signature.

// Fields
"field", // = ["public-static-field", "protected-static-field", "private-static-field", "public-instance-field", "protected-instance-field", "private-instance-field",
// "public-abstract-field", "protected-abstract-field"]

// Static initialization
// No grouping for static initialization.

// Constructors
// Only the accessibility of constructors is configurable.

// Getters
"get", // = ["public-static-get", "protected-static-get", "private-static-get", "public-instance-get", "protected-instance-get", "private-instance-get",
// "public-abstract-get", "protected-abstract-get"]

// Setters
"set", // = ["public-static-set", "protected-static-set", "private-static-set", "public-instance-set", "protected-instance-set", "private-instance-set",
// "public-abstract-set", "protected-abstract-set"]

// Methods
"method" // = ["public-static-method", "protected-static-method", "private-static-method", "public-instance-method", "protected-instance-method", "private-instance-method",
// "public-abstract-method", "protected-abstract-method"]
]

成员组类型(只读字段)

可以通过 readonly 修饰符对字段进行分组。

英:It is possible to group fields by their readonly modifiers.

[
// Index signature
"readonly-signature",
"signature",

// Fields
"readonly-field", // = ["public-static-readonly-field", "protected-static-readonly-field", "private-static-readonly-field", "public-instance-readonly-field", "protected-instance-readonly-field", "private-instance-readonly-field", "public-abstract-readonly-field", "protected-abstract-readonly-field"]
"field" // = ["public-static-field", "protected-static-field", "private-static-field", "public-instance-field", "protected-instance-field", "private-instance-field", "public-abstract-field", "protected-abstract-field"]
]

将不同成员类型分组为同一等级

还可以将不同的成员类型分组到同一等级。

英:It is also possible to group different member types at the same rank.

[
// Index signature
"signature",

// Fields
"field",

// Static initialization
"static-initialization",

// Constructors
"constructor",

// Getters and Setters at the same rank
["get", "set"],

// Methods
"method"
]

何时不使用它

如果你不关心成员的一般顺序,那么你将不需要此规则。

英:If you don't care about the general order of your members, then you will not need this rule.

选项

资源