0
# Code Quality Rules
1
2
Rules that enforce code quality, consistency, and best practices specific to TypeScript development. These rules help maintain clean, readable, and maintainable TypeScript codebases.
3
4
## Capabilities
5
6
### Import and Export Management
7
8
Rules that govern how imports and exports are structured and used.
9
10
```typescript { .api }
11
/**
12
* Enforce consistent type imports
13
*/
14
"consistent-type-imports": RuleModule;
15
16
/**
17
* Enforce consistent type exports
18
*/
19
"consistent-type-exports": RuleModule;
20
21
/**
22
* Disallow import type side effects
23
*/
24
"no-import-type-side-effects": RuleModule;
25
26
/**
27
* Disallow require statements except in import statements
28
*/
29
"no-require-imports": RuleModule;
30
31
/**
32
* Disallow var requires in favor of import
33
*/
34
"no-var-requires": RuleModule;
35
36
/**
37
* Disallow specified modules when loaded by import
38
*/
39
"no-restricted-imports": RuleModule;
40
```
41
42
**Usage Examples:**
43
44
```typescript
45
// ❌ Bad - consistent-type-imports
46
import { User, createUser } from './user';
47
const user: User = createUser();
48
49
// ✅ Good
50
import type { User } from './user';
51
import { createUser } from './user';
52
const user: User = createUser();
53
54
// ❌ Bad - no-var-requires
55
const fs = require('fs'); // Error in TypeScript
56
57
// ✅ Good
58
import * as fs from 'fs';
59
```
60
61
### Type Definition Management
62
63
Rules that ensure proper usage and definition of TypeScript types.
64
65
```typescript { .api }
66
/**
67
* Enforce type definitions to be consistently defined as interface or type
68
*/
69
"consistent-type-definitions": RuleModule;
70
71
/**
72
* Enforce consistent indexed object style
73
*/
74
"consistent-indexed-object-style": RuleModule;
75
76
/**
77
* Enforce consistent generic constructors
78
*/
79
"consistent-generic-constructors": RuleModule;
80
81
/**
82
* Disallow type aliases in favor of interfaces where possible
83
*/
84
"no-type-alias": RuleModule;
85
86
/**
87
* Require consistent use of type assertions
88
*/
89
"consistent-type-assertions": RuleModule;
90
91
/**
92
* Disallow inferrable types in variable declarations
93
*/
94
"no-inferrable-types": RuleModule;
95
96
/**
97
* Disallow specified types from being used
98
*/
99
"no-restricted-types": RuleModule;
100
101
/**
102
* Disallow this keywords outside of classes or class-like objects
103
*/
104
"no-invalid-this": RuleModule;
105
106
/**
107
* Disallow unnecessary namespace qualifiers
108
*/
109
"no-unnecessary-qualifier": RuleModule;
110
```
111
112
**Usage Examples:**
113
114
```typescript
115
// ❌ Bad - consistent-type-definitions (with interface preference)
116
type User = {
117
name: string;
118
email: string;
119
};
120
121
// ✅ Good
122
interface User {
123
name: string;
124
email: string;
125
}
126
127
// ❌ Bad - no-inferrable-types
128
const count: number = 0; // Type is inferrable
129
130
// ✅ Good
131
const count = 0; // Type inferred as number
132
```
133
134
### Function Definition Quality
135
136
Rules that enforce best practices for function definitions and signatures.
137
138
```typescript { .api }
139
/**
140
* Require explicit return types on functions and class methods
141
*/
142
"explicit-function-return-type": RuleModule;
143
144
/**
145
* Require explicit return and argument types on exported functions
146
*/
147
"explicit-module-boundary-types": RuleModule;
148
149
/**
150
* Enforce consistent return statements in functions
151
*/
152
"consistent-return": RuleModule;
153
154
/**
155
* Enforce default parameters to be last
156
*/
157
"default-param-last": RuleModule;
158
159
/**
160
* Enforce a maximum number of parameters in function definitions
161
*/
162
"max-params": RuleModule;
163
164
/**
165
* Disallow functions without return type annotation
166
*/
167
"typedef": RuleModule;
168
169
/**
170
* Require function overload signatures to be consecutive
171
*/
172
"adjacent-overload-signatures": RuleModule;
173
174
/**
175
* Prefer function signatures to be unified when possible
176
*/
177
"unified-signatures": RuleModule;
178
```
179
180
**Usage Examples:**
181
182
```typescript
183
// ❌ Bad - explicit-function-return-type
184
function calculateTotal(items) { // Missing return type
185
return items.length * 10;
186
}
187
188
// ✅ Good
189
function calculateTotal(items: Item[]): number {
190
return items.length * 10;
191
}
192
193
// ❌ Bad - default-param-last
194
function greet(name = 'World', greeting: string) { // Default param not last
195
return `${greeting}, ${name}!`;
196
}
197
198
// ✅ Good
199
function greet(greeting: string, name = 'World'): string {
200
return `${greeting}, ${name}!`;
201
}
202
```
203
204
### Class Definition Quality
205
206
Rules that govern class structure and member definitions.
207
208
```typescript { .api }
209
/**
210
* Require explicit accessibility modifiers on class properties and methods
211
*/
212
"explicit-member-accessibility": RuleModule;
213
214
/**
215
* Enforce consistent member declaration order
216
*/
217
"member-ordering": RuleModule;
218
219
/**
220
* Enforce consistent method signature style
221
*/
222
"method-signature-style": RuleModule;
223
224
/**
225
* Enforce consistent property style for classes
226
*/
227
"class-literal-property-style": RuleModule;
228
229
/**
230
* Enforce that class methods use this when appropriate
231
*/
232
"class-methods-use-this": RuleModule;
233
234
/**
235
* Disallow duplicate class members
236
*/
237
"no-dupe-class-members": RuleModule;
238
239
/**
240
* Disallow empty class constructors
241
*/
242
"no-useless-constructor": RuleModule;
243
244
/**
245
* Disallow classes that serve no purpose
246
*/
247
"no-extraneous-class": RuleModule;
248
249
/**
250
* Enforce or disallow parameter properties
251
*/
252
"parameter-properties": RuleModule;
253
```
254
255
**Usage Examples:**
256
257
```typescript
258
// ❌ Bad - explicit-member-accessibility
259
class User {
260
name: string; // Missing accessibility modifier
261
constructor(name: string) {
262
this.name = name;
263
}
264
}
265
266
// ✅ Good
267
class User {
268
public name: string;
269
public constructor(name: string) {
270
this.name = name;
271
}
272
}
273
274
// ❌ Bad - method-signature-style (with method preference)
275
interface Calculator {
276
add: (a: number, b: number) => number; // Property signature
277
}
278
279
// ✅ Good
280
interface Calculator {
281
add(a: number, b: number): number; // Method signature
282
}
283
```
284
285
### Variable and Declaration Management
286
287
Rules that govern variable declarations and usage patterns.
288
289
```typescript { .api }
290
/**
291
* Require or disallow initialization in variable declarations
292
*/
293
"init-declarations": RuleModule;
294
295
/**
296
* Disallow variable redeclaration
297
*/
298
"no-redeclare": RuleModule;
299
300
/**
301
* Disallow shadowing of variables
302
*/
303
"no-shadow": RuleModule;
304
305
/**
306
* Disallow use of variables before they are defined
307
*/
308
"no-use-before-define": RuleModule;
309
310
/**
311
* Disallow unused variables
312
*/
313
"no-unused-vars": RuleModule;
314
315
/**
316
* Disallow unused expressions
317
*/
318
"no-unused-expressions": RuleModule;
319
320
/**
321
* Disallow aliasing this
322
*/
323
"no-this-alias": RuleModule;
324
```
325
326
**Usage Examples:**
327
328
```typescript
329
// ❌ Bad - no-unused-vars
330
function processData(data: string, unusedParam: number) { // unusedParam not used
331
return data.toUpperCase();
332
}
333
334
// ✅ Good
335
function processData(data: string): string {
336
return data.toUpperCase();
337
}
338
339
// ❌ Bad - no-shadow
340
const name = 'global';
341
function process() {
342
const name = 'local'; // Shadows global name
343
return name;
344
}
345
346
// ✅ Good
347
const globalName = 'global';
348
function process() {
349
const localName = 'local';
350
return localName;
351
}
352
```
353
354
### Code Structure and Organization
355
356
Rules that enforce consistent code structure and organization.
357
358
```typescript { .api }
359
/**
360
* Disallow empty functions
361
*/
362
"no-empty-function": RuleModule;
363
364
/**
365
* Disallow useless empty exports
366
*/
367
"no-useless-empty-export": RuleModule;
368
369
/**
370
* Disallow meaningless void operator
371
*/
372
"no-meaningless-void-operator": RuleModule;
373
374
/**
375
* Disallow confusing void expressions in statement position
376
*/
377
"no-confusing-void-expression": RuleModule;
378
379
/**
380
* Require using Error objects as Promise rejection reasons
381
*/
382
"prefer-promise-reject-errors": RuleModule;
383
384
/**
385
* Require that function overload signatures be consecutive
386
*/
387
"adjacent-overload-signatures": RuleModule;
388
389
/**
390
* Enforce consistent spacing and formatting
391
*/
392
"comma-dangle": RuleModule;
393
394
/**
395
* Enforce dot notation when possible
396
*/
397
"dot-notation": RuleModule;
398
```
399
400
**Usage Examples:**
401
402
```typescript
403
// ❌ Bad - no-empty-function
404
function processData() {} // Empty function
405
406
// ✅ Good
407
function processData(): void {
408
// TODO: Implement data processing
409
throw new Error('Not implemented');
410
}
411
412
// ❌ Bad - prefer-promise-reject-errors
413
return Promise.reject('Error message'); // String rejection
414
415
// ✅ Good
416
return Promise.reject(new Error('Error message')); // Error object
417
```
418
419
### Deprecated and Legacy Patterns
420
421
Rules that discourage deprecated or legacy TypeScript/JavaScript patterns.
422
423
```typescript { .api }
424
/**
425
* Disallow usage of deprecated APIs
426
*/
427
"no-deprecated": RuleModule;
428
429
/**
430
* Disallow // tslint:<rule-flag> comments
431
*/
432
"ban-tslint-comment": RuleModule;
433
434
/**
435
* Disallow @ts-<directive> comments or require descriptions
436
*/
437
"ban-ts-comment": RuleModule;
438
439
/**
440
* Disallow triple slash reference directives
441
*/
442
"triple-slash-reference": RuleModule;
443
444
/**
445
* Disallow the use of require()
446
*/
447
"no-require-imports": RuleModule;
448
449
/**
450
* Disallow TypeScript namespaces
451
*/
452
"no-namespace": RuleModule;
453
454
/**
455
* Require using namespace keyword over module keyword
456
*/
457
"prefer-namespace-keyword": RuleModule;
458
```
459
460
**Usage Examples:**
461
462
```typescript
463
// ❌ Bad - ban-ts-comment
464
// @ts-ignore // Error: Use @ts-expect-error instead
465
const result = riskyOperation();
466
467
// ✅ Good
468
// @ts-expect-error - Known issue with third-party library
469
const result = riskyOperation();
470
471
// ❌ Bad - no-namespace
472
namespace Utils { // Prefer modules
473
export function helper() {}
474
}
475
476
// ✅ Good
477
// utils.ts
478
export function helper() {}
479
```
480
481
## Types
482
483
```typescript { .api }
484
interface RuleModule {
485
meta: {
486
type: "problem" | "suggestion" | "layout";
487
docs: {
488
description: string;
489
recommended?: boolean | string;
490
requiresTypeChecking?: boolean;
491
extendsBaseRule?: boolean | string;
492
};
493
messages: Record<string, string>;
494
schema: JSONSchema;
495
fixable?: "code" | "whitespace";
496
hasSuggestions?: boolean;
497
};
498
create(context: RuleContext): RuleListener;
499
}
500
501
interface RuleContext {
502
id: string;
503
options: unknown[];
504
settings: Record<string, unknown>;
505
parserOptions: ParserOptions;
506
getSourceCode(): SourceCode;
507
report(descriptor: ReportDescriptor): void;
508
getFilename(): string;
509
getCwd(): string;
510
}
511
512
interface ReportDescriptor {
513
node: Node;
514
message: string;
515
messageId?: string;
516
data?: Record<string, unknown>;
517
fix?: (fixer: RuleFixer) => Fix | null;
518
suggest?: SuggestionDescriptor[];
519
}
520
```