member-delimiter-style
Require a specific member delimiter style for interfaces and type literals.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
TypeScript 允许接口和类型别名中的成员之间使用三个分隔符:
英:TypeScript allows three delimiters between members in interfaces and type aliases:
interface Foo {
// Semicolons (default, preferred in TypeScript):
name: string;
// Commas (JSON-like):
name: string,
// Line breaks (none):
name: string
}
为了代码的可读性,通常最好在代码库中一致地使用相同的样式。
英:For code readability, it's generally best to use the same style consistently in your codebase.
此规则强制保持一种可配置的代码样式。 它还可以标准化构造的最后一个成员中分隔符的存在(或不存在),以及单行声明的单独分隔符语法。
英:This rule enforces keeping to one configurable code style. It can also standardize the presence (or absence) of a delimiter in the last member of a construct, as well as a separate delimiter syntax for single line declarations.
module.exports = {
"rules": {
"@typescript-eslint/member-delimiter-style": "error"
}
};
选项
默认配置:
英:Default config:
{
"multiline": {
"delimiter": "semi",
"requireLast": true
},
"singleline": {
"delimiter": "semi",
"requireLast": false
},
"multilineDetection": "brackets"
}
multiline
配置仅适用于多行 interface
/type
定义。
singleline
配置仅适用于单行 interface
/type
定义。
这两个配置是完全独立的,互不影响。
英:multiline
config only applies to multiline interface
/type
definitions.
singleline
config only applies to single line interface
/type
definitions.
The two configs are entirely separate, and do not effect one another.
multilineDetection
确定什么算作多行
英:multilineDetection
determines what counts as multiline
"brackets"
(默认)类型或接口中的任何换行符都会使其成为多行。"last-member"
如果接口的最后一个成员与最后一个括号在同一行,则算作单行。
delimiter
接受三个值(singleline
接受两个值):
英:Accepts three values (or two for singleline
):
comma
- 每个成员应以逗号 (,
) 分隔。semi
- 每个成员应以分号 (;
) 分隔。none
- 每个成员都应该没有任何分隔。
none
不是 singleline
的选项,因为单行中的成员之间没有分隔符是 TS 中的语法错误。
requireLast
确定 interface
/type
中的最后一个成员是否应该有分隔符:
英:Determines whether or not the last member in the interface
/type
should have a delimiter:
true
- 最后一个成员 must 有一个分隔符。false
- 最后一个成员 一定不 有一个分隔符。
overrides
允许你专门为 interface
或 type
定义/内联 type
指定选项。
英:Allows you to specify options specifically for either interface
s or type
definitions / inline type
s.
例如,要求 type
使用逗号,多行 interface
使用分号:
英:For example, to require commas for type
s, and semicolons for multiline interface
s:
{
"multiline": {
"delimiter": "comma",
"requireLast": true
},
"singleline": {
"delimiter": "comma",
"requireLast": true
},
"overrides": {
"interface": {
"multiline": {
"delimiter": "semi",
"requireLast": true
}
}
}
}
示例
使用默认配置的此规则的代码示例:
英:Examples of code for this rule with the default config:
- ❌ 不正确
- ✅ 正确
// missing semicolon delimiter
interface Foo {
name: string
greet(): string
}
// using incorrect delimiter
interface Bar {
name: string,
greet(): string,
}
// missing last member delimiter
interface Baz {
name: string;
greet(): string
}
// incorrect delimiter
type FooBar = { name: string, greet(): string }
// last member should not have delimiter
type FooBar = { name: string; greet(): string; }
Open in Playgroundinterface Foo {
name: string;
greet(): string;
}
interface Foo { name: string }
type Bar = {
name: string;
greet(): string;
}
type Bar = { name: string }
type FooBar = { name: string; greet(): string }
Open in Playground何时不使用它
如果你出于风格原因特别想要使用两种成员分隔符类型,或者不希望强制使用一种风格而不是另一种风格,则可以避免此规则。
英:If you specifically want to use both member delimiter kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.
但是,请记住,不一致的风格可能会损害项目的可读性。 我们建议为此规则选择一个最适合你的项目的选项。
英:However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.
选项
该规则接受以下选项
type MultiLineOption = 'comma' | 'none' | 'semi';
type SingleLineOption = 'comma' | 'semi';
type DelimiterConfig = {
multiline?: {
delimiter?: MultiLineOption;
requireLast?: boolean;
};
singleline?: {
delimiter?: SingleLineOption;
requireLast?: boolean;
};
};
type Options = [
{
multiline?: {
delimiter?: MultiLineOption;
requireLast?: boolean;
};
multilineDetection?: 'brackets' | 'last-member';
overrides?: {
interface?: DelimiterConfig;
typeLiteral?: DelimiterConfig;
};
singleline?: {
delimiter?: SingleLineOption;
requireLast?: boolean;
};
},
];
const defaultOptions: Options = [
{
multiline: { delimiter: 'semi', requireLast: true },
singleline: { delimiter: 'semi', requireLast: false },
multilineDetection: 'brackets',
},
];