跳至主要內容

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

在 Playground 中試試這個規則 ↗

範例

此規則旨在強制執行類型文字中類型註釋和函數類型周圍的特定間距模式。

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 = ()=> {};
在 Playground 中開啟

選項

此規則接受以下選項

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 = () => {};
在 Playground 中開啟

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 = ()=> {};
在 Playground 中開啟

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 = () => {};
在 Playground 中開啟

overrides - arrow

{
"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 = ()=> {};
在 Playground 中開啟

何時不使用它

如果您不想強制執行類型註釋的間距,可以安全地關閉此規則。

延伸閱讀

資源