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