Why Linting Matters in TypeScript
Linting helps catch errors, enforce coding standards, and maintain code quality across teams. While TypeScript provides type checking, linters catch stylistic issues, potential bugs, and enforce best practices.
Setting Up ESLint for TypeScript
ESLint Configuration (Flat Config)
Legacy ESLint Configuration (rc format)
Essential TypeScript ESLint Rules
| Rule | Description | Recommended |
|---|---|---|
| @typescript-eslint/no-explicit-any | Disallow 'any' type | error |
| @typescript-eslint/no-unsafe-assignment | Prevent unsafe type assignments | error |
| @typescript-eslint/explicit-function-return-type | Require return type annotations | warn |
| @typescript-eslint/consistent-type-definitions | Enforce interface over type | warn |
| @typescript-eslint/no-unused-vars | Catch unused variables | error |
| @typescript-eslint/no-non-null-assertion | Disallow '!' assertions | warn |
| @typescript-eslint/ban-ts-comment | Ban @ts-ignore comments | warn |
| @typescript-eslint/array-type | Enforce consistent array types | warn |
Prettier Integration
Husky & Lint-Staged
VS Code Integration
Custom Rule Example
Common Linting Mistakes
- Too many rules - Start with recommended configs and add gradually
- Ignoring lint errors - Use CI to enforce linting pass
- Not configuring Prettier - Let Prettier handle formatting, ESLint for code quality
- Using outdated plugins - Keep @typescript-eslint packages updated
- No type-aware linting - Enable parserOptions.project for advanced rules
Type-Aware Linting
Linting in CI/CD
Conclusion
Linting is essential for maintaining TypeScript code quality. Use ESLint with typescript-eslint for code rules, Prettier for formatting, and tools like Husky to enforce standards before commits. Start with recommended configurations and customize based on your team's needs.