UNPKG

5.32 kBMarkdownView Raw
1# Require consistent spacing around type annotations (type-annotation-spacing)
2
3Spacing 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:
4
5```ts
6// with space after, but not before (default if no option is specified)
7let foo: string = "bar";
8
9// with no spaces
10let foo:string = "bar";
11
12// with space before and after
13let foo : string = "bar";
14
15// with space before, but not after
16let foo :string = "bar";
17
18// with spaces before and after the fat arrow (default if no option is specified)
19type Foo = (string: name) => string;
20
21// with no spaces between the fat arrow
22type Foo = (string: name)=>string;
23
24// with space after, but not before the fat arrow
25type Foo = (string: name)=> string;
26
27// with space before, but not after the fat arrow
28type Foo = (string: name) =>string;
29```
30
31## Rule Details
32
33This rule aims to enforce specific spacing patterns around type annotations and function types in type literals.
34
35## Options
36
37This rule has an object option:
38- `"before": false`, (default for colon) disallows spaces before the colon/arrow.
39- `"before": true`, (default for arrow) requires a space before the colon/arrow.
40- `"after": true`, (default) requires a space after the colon/arrow.
41- `"after": false`, disallows spaces after the colon/arrow.
42- `"overrides"`, overrides the default options for type annotations with `colon` (e.g. `const foo: string`) and function types with `arrow` (e.g. `type Foo = () => {}`).
43
44### defaults
45Examples of **incorrect** code for this rule with no options at all:
46```ts
47let foo:string = "bar";
48let foo :string = "bar";
49let foo : string = "bar";
50
51function foo():string {}
52function foo() :string {}
53function foo() : string {}
54
55class Foo {
56 name:string;
57}
58
59class Foo {
60 name :string;
61}
62
63class Foo {
64 name : string;
65}
66
67type Foo = ()=> {};
68```
69
70Examples of **correct** code for this rule with no options at all:
71```ts
72let foo: string = "bar";
73
74function foo(): string {}
75
76class Foo {
77 name: string;
78}
79
80type Foo = () => {};
81```
82
83### after
84Examples of **incorrect** code for this rule with `{ "before": false, "after": true }`:
85```ts
86let foo:string = "bar";
87let foo :string = "bar";
88let foo : string = "bar";
89
90function foo():string {}
91function foo() :string {}
92function foo() : string {}
93
94class Foo {
95 name:string;
96}
97
98class Foo {
99 name :string;
100}
101
102class Foo {
103 name : string;
104}
105
106type Foo = () => {};
107```
108
109Examples of **correct** code for this rule with `{ "before": false, "after": true }`:
110```ts
111let foo: string = "bar";
112
113function foo(): string {}
114
115class Foo {
116 name: string;
117}
118
119type Foo = ()=> {};
120```
121
122### before
123Examples of **incorrect** code for this rule with `{ "before": true, "after": true }` options:
124```ts
125let foo: string = "bar";
126let foo:string = "bar";
127let foo :string = "bar";
128
129function foo(): string {}
130function foo():string {}
131function foo() :string {}
132
133class Foo {
134 name: string;
135}
136
137class Foo {
138 name:string;
139}
140
141class Foo {
142 name :string;
143}
144```
145
146Examples of **correct** code for this rule with `{ "before": true, "after": true }` options:
147```ts
148let foo : string = "bar";
149
150function foo() : string {}
151
152class Foo {
153 name : string;
154}
155```
156
157### overrides - colon
158Examples of **incorrect** code for this rule with `{ "before": false, "after": false, overrides: { colon: { before: true, after: true }} }` options:
159```ts
160let foo: string = "bar";
161let foo:string = "bar";
162let foo :string = "bar";
163
164function foo(): string {}
165function foo():string {}
166function foo() :string {}
167
168class Foo {
169 name: string;
170}
171
172class Foo {
173 name:string;
174}
175
176class Foo {
177 name :string;
178}
179
180type Foo = {
181 name: (name:string) => string;
182}
183```
184
185Examples of **correct** code for this rule with `{ "before": true, "after": true, overrides: { colon: { before: true, after: true }} }` options:
186```ts
187let foo : string = "bar";
188
189function foo() : string {}
190
191class Foo {
192 name : string;
193}
194
195type Foo = {
196 name: (name : string)=>string;
197}
198```
199
200### overrides - arrow
201Examples of **incorrect** code for this rule with `{ "before": false, "after": false, overrides: { arrow: { before: true, after: true }} }` options:
202```ts
203let foo: string = "bar";
204let foo : string = "bar";
205let foo :string = "bar";
206
207function foo(): string {}
208function foo():string {}
209function foo() :string {}
210
211class Foo {
212 name: string;
213}
214
215class Foo {
216 name : string;
217}
218
219class Foo {
220 name :string;
221}
222
223type Foo = {
224 name: (name : string)=>string;
225}
226
227type Foo = {
228 name: (name : string) =>string;
229}
230
231type Foo = {
232 name: (name : string)=> string;
233}
234```
235
236Examples of **correct** code for this rule with `{ "before": false, "after": false, overrides: { arrow: { before: true, after: true }} }` options:
237```ts
238let foo:string = "bar";
239
240function foo():string {}
241
242class Foo {
243 name:string;
244}
245
246type Foo = {
247 name: (name:string) => string;
248}
249```
250
251## When Not To Use It
252
253If you don't want to enforce spacing for your type annotations, you can safely turn this rule off.
254
255## Further Reading
256
257* [TypeScript Type System](https://basarat.gitbooks.io/typescript/docs/types/type-system.html)
258* [Type Inference](https://www.typescriptlang.org/docs/handbook/type-inference.html)