type-annotation-spacing
danger
This rule will soon be moved to eslint-stylistic. See What About Formatting? for more information.
Require consistent spacing around type annotations.
🔧
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
类型注释周围的间距提高了代码的可读性。 尽管 TypeScript 中类型注释最常用的样式指南规定在冒号后面添加空格,但在冒号之前不添加空格,但这取决于项目的偏好。 例如:
英:Spacing around type annotations improves readability of the code. Although the most commonly used style guideline for type annotations in TypeScript prescribes adding a space after the colon, but not before it, it is subjective to the preferences of a project. For example:
// with space after, but not before (default if no option is specified)
let foo: string = "bar";
// with no spaces
let foo:string = "bar";
// with space before and after
let foo : string = "bar";
// with space before, but not after
let foo :string = "bar";
// with spaces before and after the fat arrow (default if no option is specified)
type Foo = (string: name) => string;
// with no spaces between the fat arrow
type Foo = (string: name)=>string;
// with space after, but not before the fat arrow
type Foo = (string: name)=> string;
// with space before, but not after the fat arrow
type Foo = (string: name) =>string;
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/type-annotation-spacing": "error"
}
};
示例
此规则旨在强制类型字面量中类型注释和函数类型周围的特定间距模式。
英:This rule aims to enforce specific spacing patterns around type annotations and function types in type literals.
- ❌ 不正确
- ✅ 正确
let foo:string = "bar";
let foo :string = "bar";
let foo : string = "bar";
function foo():string {}
function foo() :string {}
function foo() : string {}
class Foo {
name:string;
}
class Foo {
name :string;
}
class Foo {
name : string;
}
type Foo = ()=>{};
type Foo = () =>{};
type Foo = ()=> {};
Open in Playgroundlet foo: string = "bar";
function foo(): string {}
class Foo {
name: string;
}
type Foo = () => {};
Open in Playground选项
after
{ "before": false, "after": true }
- ❌ 不正确
- ✅ 正确
let foo:string = "bar";
let foo :string = "bar";
let foo : string = "bar";
function foo():string {}
function foo() :string {}
function foo() : string {}
class Foo {
name:string;
}
class Foo {
name :string;
}
class Foo {
name : string;
}
type Foo = ()=>{};
type Foo = () =>{};
type Foo = () => {};
Open in Playgroundlet foo: string = "bar";
function foo(): string {}
class Foo {
name: string;
}
type Foo = ()=> {};
Open in Playgroundbefore
{ "before": true, "after": true }
- ❌ 不正确
- ✅ 正确
let foo: string = "bar";
let foo:string = "bar";
let foo :string = "bar";
function foo(): string {}
function foo():string {}
function foo() :string {}
class Foo {
name: string;
}
class Foo {
name:string;
}
class Foo {
name :string;
}
type Foo = ()=>{};
type Foo = () =>{};
type Foo = ()=> {};
Open in Playgroundlet foo : string = "bar";
function foo() : string {}
class Foo {
name : string;
}
type Foo = () => {};
Open in Playground覆盖 - 冒号
{
"before": false,
"after": false,
"overrides": { "colon": { "before": true, "after": true } }
}
- ❌ 不正确
- ✅ 正确
let foo: string = "bar";
let foo:string = "bar";
let foo :string = "bar";
function foo(): string {}
function foo():string {}
function foo() :string {}
class Foo {
name: string;
}
class Foo {
name:string;
}
class Foo {
name :string;
}
type Foo = () =>{};
type Foo = ()=> {};
type Foo = () => {};
Open in Playgroundlet foo : string = "bar";
function foo() : string {}
class Foo {
name : string;
}
type Foo = {
name: (name : string)=>string;
}
type Foo = ()=>{};
Open in Playground覆盖 - 箭头
{
"before": false,
"after": false,
"overrides": { "arrow": { "before": true, "after": true } }
}
- ❌ 不正确
- ✅ 正确
let foo: string = "bar";
let foo : string = "bar";
let foo :string = "bar";
function foo(): string {}
function foo():string {}
function foo() :string {}
class Foo {
name: string;
}
class Foo {
name : string;
}
class Foo {
name :string;
}
type Foo = ()=>{};
type Foo = () =>{};
type Foo = ()=> {};
Open in Playgroundlet foo:string = "bar";
function foo():string {}
class Foo {
name:string;
}
type Foo = () => {};
Open in Playground何时不使用它
如果你不想强制类型注释的间距,则可以安全地关闭此规则。
英:If you don't want to enforce spacing for your type annotations, you can safely turn this rule off.
进一步阅读
选项
该规则接受以下选项
type SpacingConfig = {
after?: boolean;
before?: boolean;
};
type Options = [
{
after?: boolean;
before?: boolean;
overrides?: {
arrow?: SpacingConfig;
colon?: SpacingConfig;
parameter?: SpacingConfig;
property?: SpacingConfig;
returnType?: SpacingConfig;
variable?: SpacingConfig;
};
},
];
const defaultOptions: Options = [{}];