Require consistent spacing around type annotations (type-annotation-spacing)
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:
Rule Details
This rule aims to enforce specific spacing patterns around type annotations and function types in type literals.
Options
This rule has an object option:
"before": false
, (default for colon) disallows spaces before the colon/arrow."before": true
, (default for arrow) requires a space before the colon/arrow."after": true
, (default) requires a space after the colon/arrow."after": false
, disallows spaces after the colon/arrow."overrides"
, overrides the default options for type annotations withcolon
(e.g.const foo: string
) and function types witharrow
(e.g.type Foo = () => {}
). Additionally allows granular overrides forvariable
(const foo: string
),parameter
(function foo(bar: string) {...}
),property
(interface Foo { bar: string }
) andreturnType
(function foo(): string {...}
) annotations.
defaults
Examples of incorrect code for this rule with no options at all:
Examples of correct code for this rule with no options at all:
after
Examples of incorrect code for this rule with { "before": false, "after": true }
:
Examples of correct code for this rule with { "before": false, "after": true }
:
before
Examples of incorrect code for this rule with { "before": true, "after": true }
options:
Examples of correct code for this rule with { "before": true, "after": true }
options:
overrides - colon
Examples of incorrect code for this rule with { "before": false, "after": false, overrides: { colon: { before: true, after: true }} }
options:
Examples of correct code for this rule with { "before": true, "after": true, overrides: { colon: { before: true, after: true }} }
options:
overrides - arrow
Examples of incorrect code for this rule with { "before": false, "after": false, overrides: { arrow: { before: true, after: true }} }
options:
Examples of correct code for this rule with { "before": false, "after": false, overrides: { arrow: { before: true, after: true }} }
options:
When Not To Use It
If you don't want to enforce spacing for your type annotations, you can safely turn this rule off.
Further Reading
Compatibility
TSLint:
typedef-whitespace
Last updated