0
# Stylistic Rules
1
2
Collection of 25 TypeScript-specific formatting and style rules for comprehensive code style enforcement. These rules cover spacing, indentation, quotes, semicolons, object/array formatting, and TypeScript-specific constructs.
3
4
## Capabilities
5
6
### Spacing Rules
7
8
Rules that enforce consistent spacing in TypeScript code.
9
10
#### Block Spacing
11
12
Disallow or enforce spaces inside of blocks after opening block and before closing block.
13
14
```typescript { .api }
15
/**
16
* Disallow or enforce spaces inside of blocks after opening block and before closing block
17
* @see https://eslint.style/rules/ts/block-spacing
18
*/
19
"block-spacing": BlockSpacingRuleOptions;
20
21
type BlockSpacingRuleOptions = ["always" | "never"];
22
```
23
24
#### Comma Spacing
25
26
Enforce consistent spacing before and after commas.
27
28
```typescript { .api }
29
/**
30
* Enforce consistent spacing before and after commas
31
* @see https://eslint.style/rules/ts/comma-spacing
32
*/
33
"comma-spacing": CommaSpacingRuleOptions;
34
35
interface CommaSpacingRuleOptions {
36
before?: boolean;
37
after?: boolean;
38
}
39
```
40
41
#### Function Call Spacing
42
43
Require or disallow spacing between function identifiers and their invocations.
44
45
```typescript { .api }
46
/**
47
* Require or disallow spacing between function identifiers and their invocations
48
* @see https://eslint.style/rules/ts/function-call-spacing
49
*/
50
"func-call-spacing": FunctionCallSpacingRuleOptions;
51
"function-call-spacing": FunctionCallSpacingRuleOptions; // Alias
52
53
type FunctionCallSpacingRuleOptions =
54
| ["never"]
55
| ["always", { allowNewlines?: boolean }];
56
```
57
58
#### Key Spacing
59
60
Enforce consistent spacing between property names and type annotations in types and interfaces.
61
62
```typescript { .api }
63
/**
64
* Enforce consistent spacing between property names and type annotations
65
* @see https://eslint.style/rules/ts/key-spacing
66
*/
67
"key-spacing": KeySpacingRuleOptions;
68
69
interface KeySpacingRuleOptions {
70
beforeColon?: boolean;
71
afterColon?: boolean;
72
mode?: "strict" | "minimum";
73
align?: "value" | "colon";
74
}
75
```
76
77
#### Keyword Spacing
78
79
Enforce consistent spacing before and after keywords.
80
81
```typescript { .api }
82
/**
83
* Enforce consistent spacing before and after keywords
84
* @see https://eslint.style/rules/ts/keyword-spacing
85
*/
86
"keyword-spacing": KeywordSpacingRuleOptions;
87
88
interface KeywordSpacingRuleOptions {
89
before?: boolean;
90
after?: boolean;
91
overrides?: Record<string, { before?: boolean; after?: boolean }>;
92
}
93
```
94
95
#### Semi Spacing
96
97
Enforce consistent spacing before and after semicolons.
98
99
```typescript { .api }
100
/**
101
* Enforce consistent spacing before and after semicolons
102
* @see https://eslint.style/rules/ts/semi-spacing
103
*/
104
"semi-spacing": SemiSpacingRuleOptions;
105
106
interface SemiSpacingRuleOptions {
107
before?: boolean;
108
after?: boolean;
109
}
110
```
111
112
#### Space Before Blocks
113
114
Enforce consistent spacing before blocks.
115
116
```typescript { .api }
117
/**
118
* Enforce consistent spacing before blocks
119
* @see https://eslint.style/rules/ts/space-before-blocks
120
*/
121
"space-before-blocks": SpaceBeforeBlocksRuleOptions;
122
123
type SpaceBeforeBlocksRuleOptions =
124
| "always"
125
| "never"
126
| {
127
functions?: "always" | "never";
128
keywords?: "always" | "never";
129
classes?: "always" | "never";
130
};
131
```
132
133
#### Space Before Function Paren
134
135
Enforce consistent spacing before function parenthesis.
136
137
```typescript { .api }
138
/**
139
* Enforce consistent spacing before function parenthesis
140
* @see https://eslint.style/rules/ts/space-before-function-paren
141
*/
142
"space-before-function-paren": SpaceBeforeFunctionParenRuleOptions;
143
144
type SpaceBeforeFunctionParenRuleOptions =
145
| "always"
146
| "never"
147
| {
148
anonymous?: "always" | "never" | "ignore";
149
named?: "always" | "never" | "ignore";
150
asyncArrow?: "always" | "never" | "ignore";
151
};
152
```
153
154
#### Space Infix Ops
155
156
Require spacing around infix operators.
157
158
```typescript { .api }
159
/**
160
* Require spacing around infix operators
161
* @see https://eslint.style/rules/ts/space-infix-ops
162
*/
163
"space-infix-ops": SpaceInfixOpsRuleOptions;
164
165
interface SpaceInfixOpsRuleOptions {
166
int32Hint?: boolean;
167
}
168
```
169
170
#### Type Annotation Spacing
171
172
Require consistent spacing around type annotations.
173
174
```typescript { .api }
175
/**
176
* Require consistent spacing around type annotations
177
* @see https://eslint.style/rules/ts/type-annotation-spacing
178
*/
179
"type-annotation-spacing": TypeAnnotationSpacingRuleOptions;
180
181
interface TypeAnnotationSpacingRuleOptions {
182
before?: boolean;
183
after?: boolean;
184
overrides?: {
185
colon?: { before?: boolean; after?: boolean };
186
arrow?: { before?: boolean; after?: boolean };
187
variable?: { before?: boolean; after?: boolean };
188
parameter?: { before?: boolean; after?: boolean };
189
property?: { before?: boolean; after?: boolean };
190
returnType?: { before?: boolean; after?: boolean };
191
};
192
}
193
```
194
195
### Style Rules
196
197
Rules that enforce consistent code style and formatting.
198
199
#### Brace Style
200
201
Enforce consistent brace style for blocks.
202
203
```typescript { .api }
204
/**
205
* Enforce consistent brace style for blocks
206
* @see https://eslint.style/rules/ts/brace-style
207
*/
208
"brace-style": BraceStyleRuleOptions;
209
210
type BraceStyleRuleOptions =
211
| ["1tbs", { allowSingleLine?: boolean }]
212
| ["stroustrup", { allowSingleLine?: boolean }]
213
| ["allman", { allowSingleLine?: boolean }];
214
```
215
216
#### Comma Dangle
217
218
Require or disallow trailing commas.
219
220
```typescript { .api }
221
/**
222
* Require or disallow trailing commas
223
* @see https://eslint.style/rules/ts/comma-dangle
224
*/
225
"comma-dangle": CommaDangleRuleOptions;
226
227
type CommaDangleRuleOptions =
228
| "never"
229
| "always"
230
| "always-multiline"
231
| "only-multiline"
232
| {
233
arrays?: "never" | "always" | "always-multiline" | "only-multiline";
234
objects?: "never" | "always" | "always-multiline" | "only-multiline";
235
imports?: "never" | "always" | "always-multiline" | "only-multiline";
236
exports?: "never" | "always" | "always-multiline" | "only-multiline";
237
functions?: "never" | "always" | "always-multiline" | "only-multiline";
238
enums?: "never" | "always" | "always-multiline" | "only-multiline";
239
generics?: "never" | "always" | "always-multiline" | "only-multiline";
240
tuples?: "never" | "always" | "always-multiline" | "only-multiline";
241
};
242
```
243
244
#### Indent
245
246
Enforce consistent indentation.
247
248
```typescript { .api }
249
/**
250
* Enforce consistent indentation
251
* @see https://eslint.style/rules/ts/indent
252
*/
253
"indent": IndentRuleOptions;
254
255
type IndentRuleOptions = [
256
number | "tab",
257
{
258
SwitchCase?: number;
259
VariableDeclarator?: number | { var?: number; let?: number; const?: number };
260
outerIIFEBody?: number;
261
MemberExpression?: number | "off";
262
FunctionDeclaration?: { parameters?: number | "first" | "off"; body?: number };
263
FunctionExpression?: { parameters?: number | "first" | "off"; body?: number };
264
CallExpression?: { arguments?: number | "first" | "off" };
265
ArrayExpression?: number | "first" | "off";
266
ObjectExpression?: number | "first" | "off";
267
ImportDeclaration?: number | "first" | "off";
268
flatTernaryExpressions?: boolean;
269
offsetTernaryExpressions?: boolean;
270
ignoredNodes?: string[];
271
ignoreComments?: boolean;
272
}?
273
];
274
```
275
276
#### Member Delimiter Style
277
278
Require a specific member delimiter style for interfaces and type literals.
279
280
```typescript { .api }
281
/**
282
* Require a specific member delimiter style for interfaces and type literals
283
* @see https://eslint.style/rules/ts/member-delimiter-style
284
*/
285
"member-delimiter-style": MemberDelimiterStyleRuleOptions;
286
287
interface MemberDelimiterStyleRuleOptions {
288
multiline?: {
289
delimiter?: "none" | "semi" | "comma";
290
requireLast?: boolean;
291
};
292
singleline?: {
293
delimiter?: "semi" | "comma";
294
requireLast?: boolean;
295
};
296
overrides?: {
297
interface?: MemberDelimiterConfig;
298
typeLiteral?: MemberDelimiterConfig;
299
};
300
multilineDetection?: "brackets" | "last-member";
301
}
302
303
interface MemberDelimiterConfig {
304
multiline?: {
305
delimiter?: "none" | "semi" | "comma";
306
requireLast?: boolean;
307
};
308
singleline?: {
309
delimiter?: "semi" | "comma";
310
requireLast?: boolean;
311
};
312
}
313
```
314
315
#### Quote Props
316
317
Require quotes around object literal, type literal, interfaces and enums property names.
318
319
```typescript { .api }
320
/**
321
* Require quotes around object literal property names
322
* @see https://eslint.style/rules/ts/quote-props
323
*/
324
"quote-props": QuotePropsRuleOptions;
325
326
type QuotePropsRuleOptions =
327
| ["always"]
328
| ["as-needed", { keywords?: boolean; unnecessary?: boolean; numbers?: boolean }]
329
| ["consistent"]
330
| ["consistent-as-needed", { keywords?: boolean }];
331
```
332
333
#### Quotes
334
335
Enforce the consistent use of either backticks, double, or single quotes.
336
337
```typescript { .api }
338
/**
339
* Enforce the consistent use of quotes
340
* @see https://eslint.style/rules/ts/quotes
341
*/
342
"quotes": QuotesRuleOptions;
343
344
type QuotesRuleOptions = [
345
"single" | "double" | "backtick",
346
{
347
avoidEscape?: boolean;
348
allowTemplateLiterals?: boolean;
349
}?
350
];
351
```
352
353
#### Semi
354
355
Require or disallow semicolons instead of ASI.
356
357
```typescript { .api }
358
/**
359
* Require or disallow semicolons instead of ASI
360
* @see https://eslint.style/rules/ts/semi
361
*/
362
"semi": SemiRuleOptions;
363
364
type SemiRuleOptions =
365
| ["always", { omitLastInOneLineBlock?: boolean; omitLastInOneLineClassBody?: boolean }]
366
| ["never", { beforeStatementContinuationChars?: "always" | "any" | "never" }];
367
```
368
369
### Object and Array Rules
370
371
Rules for formatting objects and arrays.
372
373
#### Object Curly Newline
374
375
Enforce consistent line breaks after opening and before closing braces.
376
377
```typescript { .api }
378
/**
379
* Enforce consistent line breaks after opening and before closing braces
380
* @see https://eslint.style/rules/ts/object-curly-newline
381
*/
382
"object-curly-newline": ObjectCurlyNewlineRuleOptions;
383
384
type ObjectCurlyNewlineRuleOptions =
385
| "always"
386
| "never"
387
| {
388
ObjectExpression?: "always" | "never" | { multiline?: boolean; minProperties?: number; consistent?: boolean };
389
ObjectPattern?: "always" | "never" | { multiline?: boolean; minProperties?: number; consistent?: boolean };
390
ImportDeclaration?: "always" | "never" | { multiline?: boolean; minProperties?: number; consistent?: boolean };
391
ExportDeclaration?: "always" | "never" | { multiline?: boolean; minProperties?: number; consistent?: boolean };
392
};
393
```
394
395
#### Object Curly Spacing
396
397
Enforce consistent spacing inside braces.
398
399
```typescript { .api }
400
/**
401
* Enforce consistent spacing inside braces
402
* @see https://eslint.style/rules/ts/object-curly-spacing
403
*/
404
"object-curly-spacing": ObjectCurlySpacingRuleOptions;
405
406
type ObjectCurlySpacingRuleOptions = [
407
"always" | "never",
408
{
409
arraysInObjects?: boolean;
410
objectsInObjects?: boolean;
411
}?
412
];
413
```
414
415
#### Object Property Newline
416
417
Enforce placing object properties on separate lines.
418
419
```typescript { .api }
420
/**
421
* Enforce placing object properties on separate lines
422
* @see https://eslint.style/rules/ts/object-property-newline
423
*/
424
"object-property-newline": ObjectPropertyNewlineRuleOptions;
425
426
interface ObjectPropertyNewlineRuleOptions {
427
allowAllPropertiesOnSameLine?: boolean;
428
allowMultiplePropertiesPerLine?: boolean;
429
}
430
```
431
432
### Parentheses Rules
433
434
Rules for handling parentheses usage.
435
436
#### No Extra Parens
437
438
Disallow unnecessary parentheses.
439
440
```typescript { .api }
441
/**
442
* Disallow unnecessary parentheses
443
* @see https://eslint.style/rules/ts/no-extra-parens
444
*/
445
"no-extra-parens": NoExtraParensRuleOptions;
446
447
type NoExtraParensRuleOptions =
448
| ["all"]
449
| ["all", {
450
conditionalAssign?: boolean;
451
returnAssign?: boolean;
452
nestedBinaryExpressions?: boolean;
453
ignoreJSX?: "none" | "all" | "single-line" | "multi-line";
454
enforceForArrowConditionals?: boolean;
455
enforceForSequenceExpressions?: boolean;
456
enforceForNewInMemberExpressions?: boolean;
457
enforceForFunctionPrototypeMethods?: boolean;
458
}]
459
| ["functions"];
460
```
461
462
#### No Extra Semi
463
464
Disallow unnecessary semicolons.
465
466
```typescript { .api }
467
/**
468
* Disallow unnecessary semicolons
469
* @see https://eslint.style/rules/ts/no-extra-semi
470
*/
471
"no-extra-semi": NoExtraSemiRuleOptions;
472
473
type NoExtraSemiRuleOptions = [];
474
```
475
476
### Line Rules
477
478
Rules for managing line breaks and spacing between statements.
479
480
#### Lines Around Comment
481
482
Require empty lines around comments.
483
484
```typescript { .api }
485
/**
486
* Require empty lines around comments
487
* @see https://eslint.style/rules/ts/lines-around-comment
488
*/
489
"lines-around-comment": LinesAroundCommentRuleOptions;
490
491
interface LinesAroundCommentRuleOptions {
492
beforeBlockComment?: boolean;
493
afterBlockComment?: boolean;
494
beforeLineComment?: boolean;
495
afterLineComment?: boolean;
496
allowBlockStart?: boolean;
497
allowBlockEnd?: boolean;
498
allowObjectStart?: boolean;
499
allowObjectEnd?: boolean;
500
allowArrayStart?: boolean;
501
allowArrayEnd?: boolean;
502
allowClassStart?: boolean;
503
allowClassEnd?: boolean;
504
ignorePattern?: string;
505
applyDefaultIgnorePatterns?: boolean;
506
}
507
```
508
509
#### Lines Between Class Members
510
511
Require or disallow an empty line between class members.
512
513
```typescript { .api }
514
/**
515
* Require or disallow an empty line between class members
516
* @see https://eslint.style/rules/ts/lines-between-class-members
517
*/
518
"lines-between-class-members": LinesBetweenClassMembersRuleOptions;
519
520
type LinesBetweenClassMembersRuleOptions = [
521
"always" | "never",
522
{
523
exceptAfterSingleLine?: boolean;
524
exceptAfterOverload?: boolean;
525
}?
526
];
527
```
528
529
#### Padding Line Between Statements
530
531
Require or disallow padding lines between statements.
532
533
```typescript { .api }
534
/**
535
* Require or disallow padding lines between statements
536
* @see https://eslint.style/rules/ts/padding-line-between-statements
537
*/
538
"padding-line-between-statements": PaddingLineBetweenStatementsRuleOptions;
539
540
type PaddingLineBetweenStatementsRuleOptions = Array<{
541
blankLine: "always" | "never" | "any";
542
prev: StatementType | StatementType[];
543
next: StatementType | StatementType[];
544
}>;
545
546
type StatementType =
547
| "block-like"
548
| "exports"
549
| "require"
550
| "directive"
551
| "expression"
552
| "iife"
553
| "multiline-block-like"
554
| "multiline-expression"
555
| "multiline-const"
556
| "multiline-let"
557
| "multiline-var"
558
| "singleline-const"
559
| "singleline-let"
560
| "singleline-var"
561
| "block"
562
| "empty"
563
| "function"
564
| "break"
565
| "case"
566
| "class"
567
| "const"
568
| "continue"
569
| "debugger"
570
| "default"
571
| "do"
572
| "export"
573
| "for"
574
| "if"
575
| "import"
576
| "let"
577
| "return"
578
| "switch"
579
| "throw"
580
| "try"
581
| "var"
582
| "while"
583
| "with"
584
| "interface"
585
| "type";
586
```
587
588
## Usage Examples
589
590
### Basic Rule Configuration
591
592
```typescript
593
import stylistic from "@stylistic/eslint-plugin-ts";
594
595
export default [
596
{
597
plugins: {
598
"@stylistic/ts": stylistic,
599
},
600
rules: {
601
// Spacing rules
602
"@stylistic/ts/block-spacing": "error",
603
"@stylistic/ts/comma-spacing": ["error", { before: false, after: true }],
604
"@stylistic/ts/key-spacing": ["error", { beforeColon: false, afterColon: true }],
605
606
// Style rules
607
"@stylistic/ts/brace-style": ["error", "1tbs", { allowSingleLine: true }],
608
"@stylistic/ts/comma-dangle": ["error", "always-multiline"],
609
"@stylistic/ts/indent": ["error", 2],
610
"@stylistic/ts/quotes": ["error", "single"],
611
"@stylistic/ts/semi": ["error", "always"],
612
613
// TypeScript specific
614
"@stylistic/ts/member-delimiter-style": ["error", {
615
multiline: { delimiter: "semi", requireLast: true },
616
singleline: { delimiter: "semi", requireLast: false },
617
}],
618
"@stylistic/ts/type-annotation-spacing": ["error", {
619
before: false,
620
after: true,
621
}],
622
},
623
},
624
];
625
```
626
627
### Comprehensive Style Configuration
628
629
```typescript
630
import stylistic from "@stylistic/eslint-plugin-ts";
631
632
export default [
633
{
634
plugins: {
635
"@stylistic/ts": stylistic,
636
},
637
rules: {
638
// Object/Array formatting
639
"@stylistic/ts/object-curly-spacing": ["error", "always"],
640
"@stylistic/ts/object-curly-newline": ["error", {
641
ObjectExpression: { multiline: true, consistent: true },
642
ObjectPattern: { multiline: true, consistent: true },
643
}],
644
"@stylistic/ts/object-property-newline": ["error", {
645
allowAllPropertiesOnSameLine: false,
646
}],
647
648
// Function spacing
649
"@stylistic/ts/function-call-spacing": ["error", "never"],
650
"@stylistic/ts/space-before-function-paren": ["error", {
651
anonymous: "always",
652
named: "never",
653
asyncArrow: "always",
654
}],
655
656
// Line management
657
"@stylistic/ts/lines-between-class-members": ["error", "always", {
658
exceptAfterSingleLine: true,
659
exceptAfterOverload: true,
660
}],
661
"@stylistic/ts/padding-line-between-statements": ["error",
662
{ blankLine: "always", prev: "import", next: "*" },
663
{ blankLine: "never", prev: "import", next: "import" },
664
{ blankLine: "always", prev: "*", next: "export" },
665
],
666
667
// Parentheses
668
"@stylistic/ts/no-extra-parens": ["error", "all", {
669
ignoreJSX: "multi-line",
670
enforceForArrowConditionals: false,
671
}],
672
"@stylistic/ts/no-extra-semi": "error",
673
},
674
},
675
];
676
```