目录

@typescript-eslint/type-annotation-spacing

Enforce a consistent spacing pattern around type annotations.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/type-annotation-spacing": "error"
  }
}

Options

This rule supports the following options:

type Options = {
  before: boolean;
  after: boolean;
  overrides: {
    colon: {
      before: boolean;
      after: boolean;
    };
    arrow: {
      before: boolean;
      after: boolean;
    };
    variable: {
      before: boolean;
      after: boolean;
    };
    parameter: {
      before: boolean;
      after: boolean;
    };
    property: {
      before: boolean;
      after: boolean;
    };
    returnType: {
      before: boolean;
      after: boolean;
    };
  }
}

Example:

"@typescript-eslint/type-annotation-spacing": [
  "error",
  {
    "before": true,
    "after": true,
    "overrides": {
      "colon": {
        "before": false,
        "after": true
      },
      "arrow": {
        "before": true,
        "after": true
      },
      "variable": {
        "before": true,
        "after": false
      },
      "parameter": {
        "before": false,
        "after": true
      },
      "property": {
        "before": true,
        "after": false
      },
      "returnType": {
        "before": true,
        "after": false
      }
    }
  }
]

Configuration options follow a priority order: settings under overrides take precedence over those outside of overrides. Specifically, the priority hierarchy is: overrides.variable/overrides.parameter/overrides.property/overrides.returnType > overrides.colon/overrides.arrow > before/after.

Example of Correct Code

// By default, there should be no space before the colon (:) and one space after the colon (:).
export const foo1: string = 'bar';

export declare function foo2(): string;

export class Foo3 {
  public name: string = 'hello';
}
// By default, there should be spaces before and after the arrow.
export declare type Foo4 = () => void;

Example of Incorrect Code

// By default, there should be no space before the colon (:) and one space after the colon (:).
export const foo1 :string = 'bar';

export declare function foo2() :string;

export class Foo3 {
  public name :string = 'hello';
}
// By default, there should be spaces before and after the arrow.
export declare type Foo4 = ()=>void;

Rule Set

plugin:@typescript-eslint/all

For details about how to configure Code Linter check rules, see Code Linter.