跳至主要内容

type-annotation-spacing

已弃用

格式化规则现在位于 eslint-stylistic@stylistic/ts/type-annotation-spacing 是此规则的替代品。
有关更多信息,请参阅 弃用格式化规则

要求类型注释周围的间距一致。

🔧

此规则报告的一些问题可以通过 --fix ESLint 命令行选项 自动修复。

类型注释周围的间距提高了代码的可读性。虽然 TypeScript 中最常用的类型注释样式指南规定在冒号后添加空格,但在冒号前不添加空格,但这取决于项目的偏好。例如

// 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"
}
};

在游乐场中尝试此规则 ↗

示例

此规则旨在强制执行类型文字中类型注释和函数类型周围的特定间距模式。

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 = ()=> {};
在游乐场中打开

选项

此规则接受以下选项

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 = [{}];

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 = () => {};
在游乐场中打开

before

{ "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 = ()=> {};
在游乐场中打开

overrides - colon

{
"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 = () => {};
在游乐场中打开

覆盖 - 箭头

{
"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 = ()=> {};
在游乐场中打开

何时不使用它

如果您不想为类型注释强制执行空格,可以安全地关闭此规则。

进一步阅读

资源