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.
此规则功能已冻结: ==== 此规则由 TypeScript 类型提供支持,因此,如果类型与运行时行为不匹配,则规则可能会报告不准确。它将不再接收新功能,例如新选项。它仍将接受现有功能字段的错误和文档修复。
¥This rule is feature frozen: it will no longer receive new features such as new options. It still will accept bug and documentation fixes for its existing area of features.
随着请求的功能越来越模糊,强制命名和/或排序约定的样式规则往往会变得难以理解的复杂。此规则已达到 typescript-eslint 项目维护的合理极限。有关更多信息,请参阅 eslint-plugin:功能冻结命名和排序样式规则。
¥Stylistic rules that enforce naming and/or sorting conventions tend to grow incomprehensibly complex as increasingly obscure features are requested. This rule has reached the limit of what is reasonable for the typescript-eslint project to maintain. See eslint-plugin: Feature freeze naming and sorting stylistic rules for more information.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/member-ordering": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/member-ordering": "error"
}
};
在线运行试试这个规则 ↗
选项
¥Options
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
:所有构造(用作后备)¥
default
: all constructs (used as a fallback) -
classes
?:覆盖专门针对类的排序¥
classes
?: override ordering specifically for classes -
classExpressions
?:重写专门针对类表达式的排序¥
classExpressions
?: override ordering specifically for class expressions -
interfaces
?:重写专门针对接口的排序¥
interfaces
?: override ordering specifically for interfaces -
typeLiterals
?:专门针对类型字面量覆盖排序¥
typeLiterals
?: override ordering specifically for type literals
每种构造的 OrderConfig
设置最多可以配置三个级别的排序:
¥The OrderConfig
settings for each kind of construct may configure sorting on up to three levels:
-
memberTypes
:按成员类型组进行组织,例如方法与属性¥
memberTypes
: organizing on member type groups such as methods vs. properties -
optionalityOrder
:是将所有可选成员放在前面还是将所有必需成员放在前面¥
optionalityOrder
: whether to put all optional members first or all required members first -
order
:根据成员名称进行组织,例如按字母顺序排列¥
order
: organizing based on member names, such as alphabetically
群组
¥Groups
你可以根据成员的不同属性定义许多不同的组。支持的成员属性按顺序为:
¥You can define many different groups based on different attributes of members. The supported member attributes are, in order:
-
可访问性 (
'public' | 'protected' | 'private' | '#private'
)¥Accessibility (
'public' | 'protected' | 'private' | '#private'
) -
装饰 (
'decorated'
):成员是否具有显式可访问性装饰器¥Decoration (
'decorated'
): Whether the member has an explicit accessibility decorator -
种类(
'call-signature' | 'constructor' | 'field' | 'readonly-field' | 'get' | 'method' | 'set' | 'signature' | 'readonly-signature'
)¥Kind (
'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'
.
命令
¥Orders
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 字母顺序排序,直接使用字符串<
比较(因此B
位于a
之前)¥
alphabetically
: Sorted in a-z alphabetical order, directly using string<
comparison (soB
comes beforea
) -
alphabetically-case-insensitive
:按 a-z 字母顺序排序,忽略大小写(因此a
位于B
之前)¥
alphabetically-case-insensitive
: Sorted in a-z alphabetical order, ignoring case (soa
comes beforeB
) -
natural
:与alphabetically
相同,但使用natural-compare-lite
进行更友好的数字排序¥
natural
: Same asalphabetically
, but usingnatural-compare-lite
for more friendly sorting of numbers -
natural-case-insensitive
:与alphabetically-case-insensitive
相同,但使用natural-compare-lite
进行更友好的数字排序¥
natural-case-insensitive
: Same asalphabetically-case-insensitive
, but usingnatural-compare-lite
for more friendly sorting of numbers
默认配置
¥Default configuration
默认配置如下所 示:
¥The default configuration looks as follows:
{
"default": {
"memberTypes": [
// 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",
],
},
}
默认配置包含成员组类型,其中包含其他成员类型。这样做的目的是为了提供更好的错误消息。
¥The default configuration contains member group types which contain other member types. This is intentional to provide better error messages.
默认情况下,成员不排序。如果你想按字母顺序对它们进行排序,则必须提供自定义配置。
¥By default, the members are not sorted. If you want to sort them alphabetically, you have to provide a custom configuration.
示例
¥Examples
所有结构的通用命令
¥General Order on All Constructs
此配置指定所有构造的顺序。它忽略除签名、方法、构造函数和字段之外的成员类型。它还忽略了可访问性和范围。
¥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"] },
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
B: string; // -> field
new (); // -> constructor
A(): void; // -> method
[Z: string]: any; // -> signature
}
Open in Playgroundtype Foo = {
B: string; // -> field
// no constructor
A(): void; // -> method
// no signature
};
Open in Playgroundclass 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 Playgroundconst 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 Playgroundinterface Foo {
[Z: string]: any; // -> signature
A(): void; // -> method
new (); // -> constructor
B: string; // -> field
}
Open in Playgroundtype Foo = {
// no signature
A(): void; // -> method
// no constructor
B: string; // -> field
};
Open in Playgroundclass Foo {
[Z: string]: any; // -> signature
public static A(): void {} // -> method
public B(): void {} // -> method
constructor() {} // -> constructor
private C: string; // -> field
public D: string; // -> field
protected static E: string; // -> field
}
Open in Playgroundconst Foo = class {
[Z: string]: any; // -> signature
public static A(): void {} // -> method
public B(): void {} // -> method
constructor() {} // -> constructor
private C: string; // -> field
public D: string; // -> field
protected static E: string; // -> field
};
Open in Playground类
¥Classes
公共静态字段之前的公共实例方法
¥Public Instance Methods Before Public Static Fields
此配置指定公共实例方法应首先出现在公共静态字段之前。其他一切都可以放在任何地方 。它不适用于接口或类型字面量,因为可访问性和范围不是它们的一部分。
¥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"] },
],
},
}
- ❌ Incorrect
- ✅ Correct
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 Playgroundconst 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 Playgroundclass Foo {
public B(): void {} // -> public instance method
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)
}
Open in Playgroundconst Foo = class {
public B(): void {} // -> public instance method
private C: string; // (irrelevant)
[Z: string]: any; // (irrelevant)
public D: string; // (irrelevant)
constructor() {} // (irrelevant)
public static A(): void {} // (irrelevant)
public static E: string; // -> public static field
};
Open in Playground实例字段之前的静态字段
¥Static Fields Before Instance Fields
此配置指定静态字段应位于实例字段之前,首先是公共静态字段。它不适用于接口或类型字面量,因为可访问性和范围不是它们的一部分。
¥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"] },
],
},
}
- ❌ Incorrect
- ✅ Correct
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 Playgroundconst 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 Playgroundclass Foo {
public static A: string; // -> public static field
private static B: string; // -> static field
protected static C: string; // -> static field
private static D: string; // -> static field
private E: string; // -> instance field
[Z: string]: any; // (irrelevant)
}
Open in Playgroundconst foo = class {
[Z: string]: any; // -> signature (irrelevant)
public static A: string; // -> public static field
constructor() {} // -> constructor (irrelevant)
private static B: string; // -> static field
protected static C: string; // -> static field
private static D: string; // -> static field
private E: string; // -> instance field
public T(): void {} // -> method (irrelevant)
};
Open in Playground类声明
¥Class Declarations
此配置仅指定类的顺序:方法,然后是构造函数,然后是字段。它不适用于类表达式(对它们使用 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"] },
],
},
}
- ❌ Incorrect
- ✅ Correct
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 Playgroundclass Foo {
public static A(): void {} // -> method
public B(): void {} // -> method
constructor() {} // -> constructor
private C: string; // -> field
public D: string; // -> field
protected static E: string; // -> field
}
Open in Playground类表达式
¥Class Expressions
此配置仅指定类表达式的顺序:方法,然后是构造函数,然后是字段。它不适用于类声明(对它们使用 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"] },
],
},
}
- ❌ Incorrect
- ✅ Correct
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 Playgroundconst foo = class {
public static A(): void {} // -> method
public B(): void {} // -> method
constructor() {} // -> constructor
private C: string; // -> field
public D: string; // -> field
protected static E: string; // -> field
};
Open in Playground接口
¥Interfaces
此配置仅指定接口的顺序:签名,然后是方法,然后是构造函数,然后是字段。它不适用于类型文字(对它们使用 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
唯一允许的类型。
¥These member types are the only ones allowed for interfaces
.
// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "interfaces": ["signature", "method", "constructor", "field"] },
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
B: string; // -> field
new (); // -> constructor
A(): void; // -> method
[Z: string]: any; // -> signature
}
Open in Playgroundinterface Foo {
[Z: string]: any; // -> signature
A(): void; // -> method
new (); // -> constructor
B: string; // -> field
}
Open in Playground类型字面量
¥Type Literals
此配置仅指定类型字面量的顺序:签名,然后是方法,然后是构造函数,然后是字段。它不适用于接口(对它们使用 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
唯一允许的类型。
¥These member types are the only ones allowed for typeLiterals
.
// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{ "typeLiterals": ["signature", "method", "constructor", "field"] },
],
},
}
- ❌ Incorrect
- ✅ Correct
type Foo = {
B: string; // -> field
A(): void; // -> method
new (); // -> constructor
[Z: string]: any; // -> signature
};
Open in Playgroundtype Foo = {
[Z: string]: any; // -> signature
A(): void; // -> method
new (); // -> constructor
B: string; // -> field
};
Open in Playground排序选项
¥Sorting Options
在成员组内按字母顺序排序
¥Sorting Alphabetically Within Member Groups
如果未指定 memberTypes
,将应用默认成员顺序。你可以在 默认配置 中看到默认顺序。
¥The default member order will be applied if memberTypes
is not specified.
You can see the default order in Default Configuration.
// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{
"default": {
"order": "alphabetically",
},
},
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
a: x;
B: x;
c: x;
B(): void;
c(): void;
a(): void;
}
Open in Playgroundinterface Foo {
B: x;
a: x;
c: x;
B(): void;
a(): void;
c(): void;
}
Open in Playground在自定义成员组中按字母顺序排序
¥Sorting Alphabetically Within Custom Member Groups
此配置指定在每个自定义 memberTypes
组中,成员按字母顺序区分大小写。
¥This config specifies that within each custom memberTypes
group, members are in an alphabetic case-sensitive order.
// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{
"default": {
"memberTypes": ["method", "field"],
"order": "alphabetically",
},
},
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
B(): void;
c(): void;
a(): void;
a: x;
B: x;
c: x;
}
Open in Playgroundinterface Foo {
B(): void;
a(): void;
c(): void;
B: x;
a: x;
c: x;
}
Open in Playground在成员组内按字母顺序排序(不区分大小写)
¥Sorting Alphabetically Case Insensitive Within Member Groups
如果未指定 memberTypes
,将应用默认成员顺序。你可以在 默认配置 中看到默认顺序。
¥The default member order will be applied if memberTypes
is not specified.
You can see the default order in Default Configuration.
// .eslintrc.json
{
"rules": {
"@typescript-eslint/member-ordering": [
"error",
{
"default": {
"order": "alphabetically-case-insensitive",
},
},
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
B: x;
a: x;
c: x;
B(): void;
c(): void;
a(): void;
}
Open in Playgroundinterface Foo {
a: x;
B: x;
c: x;
a(): void;
B(): void;
c(): void;
}
Open in Playground按字母顺序排序忽略成员组
¥Sorting Alphabetically Ignoring Member Groups
此配置指定成员均按字母大小写顺序排序。通过为 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" } },
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
b(): void;
a: boolean;
[a: string]: number;
new (): Bar;
(): Baz;
}
Open in Playgroundinterface Foo {
[a: string]: number;
a: boolean;
b(): void;
(): Baz;
new (): Bar;
}
Open in Playground对可选成员进行排序
¥Sorting Optional Members First or Last
可以启用 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",
},
},
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
a: boolean;
b?: number;
c: string;
}
Open in Playgroundinterface Foo {
b?: number;
a: boolean;
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",
},
},
],
},
}
- ❌ Incorrect
- ✅ Correct
interface Foo {
a: boolean;
b?: number;
c: string;
}
Open in Playgroundinterface Foo {
a: boolean;
c: string;
b?: number;
}
Open in Playground所有支持的选项
¥All Supported Options
成员类型(细化形式)
¥Member Types (Granular Form)
有多种方法可以指定成员类型。最明确和细粒度的形式如下:
¥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 不会诉说它。
¥If you only specify some of the possible types, the non-specified ones can have any particular order. This means that they can be placed before, within or after the specified types and the linter won't complain about it.
成员组类型(具有可访问性,忽略范围)
¥Member Group Types (With Accessibility, Ignoring Scope)
还可以按成员类型的可访问性(static
、instance
、abstract
)对其进行分组,而忽略其范围。
¥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"]
]
成员组类型(具有辅助功能和装饰器)
¥Member Group Types (With Accessibility and a Decorator)
还可以使用装饰器单独对方法或字段进行分组,可以选择指定它们的可访问性。
¥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"]
]
成员组类型(有范围,忽略可访问性)
¥Member Group Types (With Scope, Ignoring Accessibility)
另一个选项是按成员类型的作用域 (public
、protected
、private
) 对成员类型进行分组,忽略其可访问性。
¥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"]
]
成员组类型(具有范围和可访问性)
¥Member Group Types (With Scope and Accessibility)
第三个分组选项是忽略范围和可访问性。
¥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"]
]
成员组类型(只读字段)
¥Member Group Types (Readonly Fields)
可以通过字段的 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"]
]
将不同成员类型分组为同一等级
¥Grouping Different Member Types at the Same Rank
还可以将不同的成员类型分组到同一等级。
¥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",
]
何时不使用它
¥When Not To Use It
如果你不关心成员的一般顺序,那么你将不需要此规则。
¥If you don't care about the general order of your members, then you will not need this rule.
'## 资源'