1 | # Require a specific member delimiter style for interfaces and type literals (member-delimiter-style)
|
2 |
|
3 | Enforces a consistent member delimiter style in interfaces and type literals. There are three member delimiter styles primarily used in TypeScript:
|
4 | - Semicolon style (default, preferred in TypeScript).
|
5 | ```ts
|
6 | interface Foo {
|
7 | name: string;
|
8 | greet(): void; // last semicolon can be ignored
|
9 | }
|
10 |
|
11 | type Bar = {
|
12 | name: string;
|
13 | greet(): void; // last semicolon can be ignored
|
14 | }
|
15 | ```
|
16 |
|
17 | - Comma style (JSON style).
|
18 | ```ts
|
19 | interface Foo {
|
20 | name: string,
|
21 | greet(): void, // last comma can be ignored
|
22 | }
|
23 |
|
24 | type Bar = {
|
25 | name: string,
|
26 | greet(): void, // last comma can be ignored
|
27 | }
|
28 | ```
|
29 |
|
30 | - Linebreak style.
|
31 | ```ts
|
32 | interface Foo {
|
33 | name: string
|
34 | greet(): void
|
35 | }
|
36 |
|
37 | type Bar = {
|
38 | name: string
|
39 | greet(): void
|
40 | }
|
41 | ```
|
42 |
|
43 | The rule also enforces the presence of the delimiter in the last member of the interface and/or type literal, allowing single line declarations to be ignored.
|
44 |
|
45 | ## Rule Details
|
46 |
|
47 | This rule aims to standardise the way interface and type literal members are delimited.
|
48 |
|
49 | ## Options
|
50 |
|
51 | This rule, in its default state, does not require any argument, in which case a **semicolon** is used as a
|
52 | delimiter and **all members** require a delimiter, except in single line interfaces or type literals, in which case
|
53 | the delimiter of the **last member** can be omitted.
|
54 |
|
55 | The rule can also take one or more of the following options:
|
56 | - `"delimiter": "semi"`, (default) use this to require a semicolon.
|
57 | - `"delimiter": "comma"`, use this to require a comma.
|
58 | - `"delimiter": "none"`, use this to require a linebreak.
|
59 | - `"requireLast": true`, (default) use this to require a delimiter for all members of the interface and/or type literal.
|
60 | - `"requireLast": false`, use this to ignore the last member of the interface and/or type literal.
|
61 | - `"ignoreSingleLine": true`, (default) use this to override the `requireLast` in single line declarations.
|
62 | - `"ignoreSingleLine": false`, use this to enfore the `requireLast` in single line declarations.
|
63 | - `"overrides"`, overrides the default options for **interfaces** and **type literals**.
|
64 |
|
65 | ### defaults
|
66 | Examples of **incorrect** code for this rule with the defaults
|
67 | `{ delimiter: "semi", requireLast: true, ignoreSingleLine: true }` or no option at all:
|
68 | ```ts
|
69 | // missing semicolon delimiter
|
70 | interface Foo {
|
71 | name: string
|
72 | greet(): string
|
73 | }
|
74 |
|
75 | // using incorrect delimiter
|
76 | interface Bar {
|
77 | name: string,
|
78 | greet(): string,
|
79 | }
|
80 |
|
81 | // missing last member delimiter
|
82 | interface Baz {
|
83 | name: string;
|
84 | greet(): string
|
85 | }
|
86 |
|
87 | // missing semicolon delimiter
|
88 | type Foo = {
|
89 | name: string
|
90 | greet(): string
|
91 | }
|
92 |
|
93 | // using incorrect delimiter
|
94 | type Bar = {
|
95 | name: string,
|
96 | greet(): string,
|
97 | }
|
98 |
|
99 | // missing last member delimiter
|
100 | type Baz = {
|
101 | name: string,
|
102 | greet(): string
|
103 | }
|
104 | ```
|
105 |
|
106 | Examples of **correct** code for this rule with the default
|
107 | `{ delimiter: "semi", requireLast: true, ignoreSingleLine: true }`:
|
108 | ```ts
|
109 | interface Foo {
|
110 | name: string;
|
111 | greet(): string;
|
112 | }
|
113 |
|
114 | interface Foo { name: string }
|
115 |
|
116 | interface Foo { name: string; }
|
117 |
|
118 | type Bar = {
|
119 | name: string;
|
120 | greet(): string;
|
121 | }
|
122 |
|
123 | type Bar = { name: string }
|
124 |
|
125 | type Bar = { name: string; }
|
126 | ```
|
127 |
|
128 | ### delimiter - semi
|
129 | Examples of **incorrect** code for this rule with `{ delimiter: "semi" }`:
|
130 | ```ts
|
131 | // missing semicolon delimiter
|
132 | interface Foo {
|
133 | name: string
|
134 | greet(): string
|
135 | }
|
136 |
|
137 | // using incorrect delimiter
|
138 | interface Bar {
|
139 | name: string,
|
140 | greet(): string,
|
141 | }
|
142 | ```
|
143 |
|
144 | Examples of **correct** code for this rule with `{ delimiter: "semi" }`:
|
145 | ```ts
|
146 | // with requireLast = true/false
|
147 | interface Foo {
|
148 | name: string;
|
149 | greet(): string;
|
150 | }
|
151 |
|
152 | type Bar = {
|
153 | name: string;
|
154 | greet(): string;
|
155 | }
|
156 |
|
157 | // with requireLast = false
|
158 | interface Foo {
|
159 | name: string;
|
160 | greet(): string
|
161 | }
|
162 |
|
163 | type Bar = {
|
164 | name: string;
|
165 | greet(): string
|
166 | }
|
167 | ```
|
168 |
|
169 | ### delimiter - comma
|
170 | Examples of **incorrect** code for this rule with `{ delimiter: "comma" }`:
|
171 | ```ts
|
172 | // missing comma delimiter
|
173 | interface Foo {
|
174 | name: string
|
175 | greet(): string
|
176 | }
|
177 |
|
178 | // using incorrect delimiter
|
179 | interface Bar {
|
180 | name: string;
|
181 | greet(): string;
|
182 | }
|
183 | ```
|
184 |
|
185 | Examples of **correct** code for this rule with `{ delimiter: "comma" }`:
|
186 | ```ts
|
187 | // with requireLast = true/false
|
188 | interface Foo {
|
189 | name: string,
|
190 | greet(): string,
|
191 | }
|
192 |
|
193 | type Bar = {
|
194 | name: string,
|
195 | greet(): string,
|
196 | }
|
197 |
|
198 | // with requireLast = false
|
199 | interface Foo {
|
200 | name: string,
|
201 | greet(): string
|
202 | }
|
203 |
|
204 | type Bar = {
|
205 | name: string,
|
206 | greet(): string
|
207 | }
|
208 | ```
|
209 |
|
210 | ### delimiter - none
|
211 | Examples of **incorrect** code for this rule with `{ delimiter: "none" }`:
|
212 | ```ts
|
213 | // using incorrect delimiter
|
214 | interface Foo {
|
215 | name: string;
|
216 | greet(): string;
|
217 | }
|
218 |
|
219 | // using incorrect delimiter
|
220 | interface Bar {
|
221 | name: string,
|
222 | greet(): string,
|
223 | }
|
224 | ```
|
225 |
|
226 | Examples of **correct** code for this rule with `{ delimiter: "none" }`:
|
227 | ```ts
|
228 | interface Foo {
|
229 | name: string
|
230 | greet(): string
|
231 | }
|
232 |
|
233 | type Bar = {
|
234 | name: string
|
235 | greet(): string
|
236 | }
|
237 | ```
|
238 |
|
239 | ### requireLast
|
240 | Examples of **incorrect** code for this rule with `{ requireLast: true }`:
|
241 | ```ts
|
242 | // using incorrect delimiter
|
243 | interface Foo {
|
244 | name: string;
|
245 | greet(): string
|
246 | }
|
247 |
|
248 | // using incorrect delimiter
|
249 | type Bar = {
|
250 | name: string,
|
251 | greet(): string
|
252 | }
|
253 | ```
|
254 |
|
255 | Examples of **correct** code for this rule with `{ requireLast: true }`:
|
256 | ```ts
|
257 | interface Foo {
|
258 | name: string;
|
259 | greet(): string;
|
260 | }
|
261 |
|
262 | type Bar = {
|
263 | name: string,
|
264 | greet(): string,
|
265 | }
|
266 | ```
|
267 |
|
268 | Examples of **correct** code for this rule with `{ requireLast: false }`:
|
269 | ```ts
|
270 | interface Foo {
|
271 | name: string
|
272 | greet(): string
|
273 | }
|
274 |
|
275 | interface Bar {
|
276 | name: string;
|
277 | greet(): string
|
278 | }
|
279 |
|
280 | interface Baz {
|
281 | name: string;
|
282 | greet(): string;
|
283 | }
|
284 |
|
285 | type Foo = {
|
286 | name: string
|
287 | greet(): string
|
288 | }
|
289 |
|
290 | type Bar = {
|
291 | name: string,
|
292 | greet(): string
|
293 | }
|
294 |
|
295 | type Baz = {
|
296 | name: string,
|
297 | greet(): string,
|
298 | }
|
299 | ```
|
300 |
|
301 | ### ignoreSingleLine
|
302 | Examples of **incorrect** code for this rule with `{ ignoreSingleLine: true }`:
|
303 | ```ts
|
304 | // using incorrect delimiter
|
305 | interface Foo { name: string, }
|
306 |
|
307 | // using incorrect delimiter
|
308 | type Bar = { name: string, }
|
309 | ```
|
310 |
|
311 | Examples of **correct** code for this rule with `{ ignoreSingleLine: true }`:
|
312 | ```ts
|
313 | // can have a delimiter or not
|
314 | interface Foo { name: string }
|
315 |
|
316 | interface Foo { name: string; }
|
317 |
|
318 | // can have a delimiter or not
|
319 | type Bar = { name: string }
|
320 |
|
321 | type Bar = { name: string; }
|
322 | ```
|
323 |
|
324 | Examples of **incorrect** code for this rule with `{ ignoreSingleLine: false }`:
|
325 | ```ts
|
326 | // missing delimiter
|
327 | interface Foo { name: string }
|
328 |
|
329 | // missing delimiter
|
330 | type Bar = { name: string }
|
331 | ```
|
332 |
|
333 | Examples of **correct** code for this rule with `{ ignoreSingleLine: false }`:
|
334 | ```ts
|
335 | interface Foo { name: string; }
|
336 |
|
337 | type Bar = { name: string; }
|
338 | ```
|
339 |
|
340 | ### overrides - interface
|
341 | Examples of **incorrect** code for this rule with `{ delimiter: "comma", requireLast: true, overrides: { interface: { delimiter: "semi" } } }`:
|
342 | ```ts
|
343 | // expecting a semicolon
|
344 | interface Foo {
|
345 | name: string,
|
346 | greet(): string,
|
347 | }
|
348 |
|
349 | // this is fine, using default
|
350 | type Bar = {
|
351 | name: string,
|
352 | greet(): string,
|
353 | }
|
354 | ```
|
355 |
|
356 | Examples of **correct** code for this rule with `{ delimiter: "comma", requireLast: true, overrides: { interface: { delimiter: "semi" } } }`:
|
357 | ```ts
|
358 | // this is fine, using override
|
359 | interface Foo {
|
360 | name: string;
|
361 | greet(): string;
|
362 | }
|
363 |
|
364 | // this is fine, using default
|
365 | type Bar = {
|
366 | name: string,
|
367 | greet(): string,
|
368 | }
|
369 | ```
|
370 |
|
371 | ### overrides - typeLiteral
|
372 | Examples of **incorrect** code for this rule with `{ delimiter: "semi", requireLast: true, overrides: { typeLiteral: { delimiter: "comma", requireLast: false } } }`:
|
373 | ```ts
|
374 | // this is fine, using default
|
375 | interface Foo {
|
376 | name: string;
|
377 | greet(): string;
|
378 | }
|
379 |
|
380 | // expecting a comma
|
381 | type Bar = {
|
382 | name: string;
|
383 | greet(): string
|
384 | }
|
385 | ```
|
386 |
|
387 | Examples of **correct** code for this rule with `{ delimiter: "semi", requireLast: true, overrides: { typeLiteral: { delimiter: "comma", requireLast: false } } }`:
|
388 | ```ts
|
389 | // this is fine, using default
|
390 | interface Foo {
|
391 | name: string;
|
392 | greet(): string;
|
393 | }
|
394 |
|
395 | // this is fine, using override
|
396 | type Bar = {
|
397 | name: string,
|
398 | greet(): string
|
399 | }
|
400 | ```
|
401 |
|
402 | ## When Not To Use It
|
403 |
|
404 | If you don't care about enforcing a consistent member delimiter in interfaces and type literals, then you will not need this rule.
|