0
# TSLint
1
2
TSLint is an extensible static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors. It provides comprehensive linting capabilities with 163+ built-in rules, 13 formatters, custom rule development APIs, and programmatic integration support.
3
4
> **Note:** TSLint is deprecated. The TypeScript team recommends migrating to ESLint with `@typescript-eslint/parser` for new projects.
5
6
## Installation
7
8
```bash
9
npm install tslint typescript
10
```
11
12
## Package Information
13
14
- **Package Name**: tslint
15
- **Package Type**: npm
16
- **Language**: TypeScript
17
- **Installation**: `npm install tslint typescript`
18
19
## Core Imports
20
21
```typescript
22
import { Linter, Configuration } from 'tslint';
23
import { RuleFailure, AbstractRule } from 'tslint';
24
```
25
26
CommonJS:
27
```javascript
28
const { Linter, Configuration } = require('tslint');
29
```
30
31
## Basic Usage
32
33
### Command Line Interface
34
35
```bash
36
# Lint TypeScript files
37
tslint [options] [files...]
38
39
# Basic linting
40
tslint src/**/*.ts
41
42
# With configuration
43
tslint -c tslint.json src/**/*.ts
44
45
# Auto-fix violations
46
tslint --fix src/**/*.ts
47
48
# Generate initial configuration
49
tslint --init
50
```
51
52
### Programmatic Usage
53
54
```typescript
55
import { Linter, Configuration } from 'tslint';
56
import * as fs from 'fs';
57
58
// Create linter instance
59
const linter = new Linter({
60
fix: false,
61
formatter: 'stylish'
62
});
63
64
// Load configuration
65
const configuration = Configuration.findConfiguration(
66
'tslint.json',
67
'src/myfile.ts'
68
).results;
69
70
// Lint a file
71
const sourceCode = fs.readFileSync('src/myfile.ts', 'utf8');
72
linter.lint('src/myfile.ts', sourceCode, configuration);
73
74
// Get results
75
const result = linter.getResult();
76
console.log(result.output);
77
console.log(`Errors: ${result.errorCount}, Warnings: ${result.warningCount}`);
78
```
79
80
## Key API Components
81
82
### Core Classes
83
84
```typescript { .api }
85
// Main linter class
86
class Linter {
87
constructor(options: ILinterOptions, program?: ts.Program)
88
lint(fileName: string, source: string, configuration?: IConfigurationFile): void
89
getResult(): LintResult
90
91
// Static utilities
92
static VERSION: string
93
static createProgram(configFile: string, projectDirectory?: string): ts.Program
94
static findConfiguration(configFile: string, inputFilePath: string): IConfigurationLoadResult
95
}
96
```
97
98
```typescript { .api }
99
// Linter options interface
100
interface ILinterOptions {
101
fix: boolean;
102
formatter?: string | FormatterConstructor;
103
formattersDirectory?: string;
104
quiet?: boolean;
105
rulesDirectory?: string | string[];
106
}
107
```
108
109
```typescript { .api }
110
// Linting result interface
111
interface LintResult {
112
errorCount: number;
113
warningCount: number;
114
failures: RuleFailure[];
115
fixes?: RuleFailure[];
116
format: string | FormatterConstructor;
117
output: string;
118
}
119
```
120
121
### Configuration Management
122
123
```typescript { .api }
124
import { Configuration } from 'tslint';
125
126
// Find and load configuration
127
const config = Configuration.findConfiguration(
128
'./tslint.json',
129
'./src/file.ts'
130
);
131
132
// Load from specific path
133
const config2 = Configuration.loadConfigurationFromPath('./tslint.json');
134
135
// Configuration interface
136
interface IConfigurationFile {
137
extends: string[];
138
jsRules: Map<string, Partial<IOptions>>;
139
linterOptions?: {
140
exclude: string[];
141
format: string;
142
};
143
rulesDirectory: string[];
144
rules: Map<string, Partial<IOptions>>;
145
}
146
```
147
148
### Rule Development
149
150
```typescript { .api }
151
import { Rules, RuleFailure } from 'tslint';
152
import * as ts from 'typescript';
153
154
// Base rule class
155
abstract class AbstractRule implements IRule {
156
static metadata: IRuleMetadata;
157
158
constructor(options: IOptions)
159
getOptions(): IOptions
160
isEnabled(): boolean
161
apply(sourceFile: ts.SourceFile): RuleFailure[]
162
applyWithWalker(walker: IWalker): RuleFailure[]
163
}
164
165
// Rule metadata interface
166
interface IRuleMetadata {
167
ruleName: string;
168
type: "functionality" | "maintainability" | "style" | "typescript" | "formatting";
169
description: string;
170
optionsDescription: string;
171
options: any;
172
optionExamples?: Array<true | any[]>;
173
typescriptOnly: boolean;
174
hasFix?: boolean;
175
requiresTypeInfo?: boolean;
176
}
177
178
// Core rule interfaces
179
interface IRule {
180
getOptions(): IOptions;
181
isEnabled(): boolean;
182
apply(sourceFile: ts.SourceFile): RuleFailure[];
183
applyWithWalker(walker: IWalker): RuleFailure[];
184
}
185
186
interface ITypedRule extends IRule {
187
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[];
188
}
189
190
interface IOptions {
191
ruleArguments: any[];
192
ruleSeverity: RuleSeverity;
193
ruleName: string;
194
disabledIntervals: IDisabledInterval[];
195
}
196
197
interface IDisabledInterval {
198
startPosition: number;
199
endPosition: number;
200
}
201
```
202
203
## Built-in Features
204
205
### Rules (163 total)
206
- **Functionality**: 65 rules for correctness and best practices
207
- **Maintainability**: 14 rules for code maintainability
208
- **Style**: 79 rules for coding style and conventions
209
- **Formatting**: 5 rules for code formatting
210
211
### Formatters (13 total)
212
- **checkstyle**: Checkstyle XML format
213
- **json**: JSON format for machine processing
214
- **stylish**: Human-readable format (default)
215
- **junit**: JUnit XML format
216
- **prose**: Verbose human-readable format
217
- And 8 more specialized formatters
218
219
### Testing Framework
220
Comprehensive testing utilities for validating custom rules with markup-based test files.
221
222
## Advanced Features
223
224
### Type-Aware Linting
225
```typescript { .api }
226
import { Rules } from 'tslint';
227
228
// Rules that require TypeScript type information
229
class MyTypedRule extends Rules.TypedRule {
230
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
231
// Access type checker for advanced analysis
232
const typeChecker = program.getTypeChecker();
233
// Implementation
234
return [];
235
}
236
}
237
```
238
239
### Comment-Based Rule Control
240
```typescript
241
// Disable rules inline
242
// tslint:disable-next-line:no-console
243
console.log('Debug message');
244
245
// Disable specific rule for file section
246
// tslint:disable:no-unused-variable
247
let unused = 'temporary';
248
// tslint:enable:no-unused-variable
249
```
250
251
## Related Documentation
252
253
For detailed information on specific areas:
254
255
- **[Core Linting](./linting.md)** - Linter class, options, and programmatic usage
256
- **[Configuration](./configuration.md)** - Configuration loading, file formats, and inheritance
257
- **[Built-in Rules](./rules.md)** - Complete reference of 163+ built-in rules
258
- **[Custom Rules](./rules.md#custom-rule-development)** - Rule development APIs and patterns
259
- **[Formatters](./formatters.md)** - Output formatting and custom formatter development
260
- **[Testing](./testing.md)** - Rule testing framework and utilities
261
- **[CLI Tool](./cli.md)** - Command-line interface and options
262
263
## Migration Note
264
265
TSLint is deprecated in favor of ESLint with TypeScript support. For new projects, consider using:
266
267
```bash
268
npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
269
```
270
271
The TypeScript team provides migration tools and guides for transitioning from TSLint to ESLint.