Sub topic 3
Catching Real Bugs with Type-Checked Rules
This chapter assumes you know TypeScript, have a tsconfig.json, can run npm commands, and use ESLint flat config.
Type-checked rules
Basic lint rules inspect source text and syntax. Type-checked rules also read TypeScript's type information. They can detect problems such as using a promise without handling it.
async function save(id: string): Promise<void> {
console.log(`Saved ${id}`);
}
export function run(): void {
save("a"); // @typescript-eslint/no-floating-promises reports this
}
export async function runFixed(): Promise<void> {
await save("a");
}Enable these rules with the project service:
// eslint.config.mjs
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import { defineConfig } from "eslint/config";
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
);Suppressions with a reason
Suppress a single exception instead of disabling a rule broadly. Explain why the exception is safe.
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- legacy payload has no known shape
function parseLegacy(input: any): string {
return String(input);
}Formatting, editors, and CI
Linting finds suspicious or error-prone code. Formatting makes layout consistent. Keep those jobs separate so each tool has a clear purpose.
Run ESLint locally and in continuous integration (CI):
npx eslint .
npx eslint . --fixConfigure an editor's ESLint extension to show diagnostics while you work. CI should run the non-fixing command and fail when errors remain.
Performance and larger repositories
Type-aware linting is slower because ESLint loads TypeScript projects. Start with changed files during development, and lint the full repository in CI. In a monorepo, keep project boundaries clear and avoid including generated files.
Custom rules and migration
Use a custom rule when a repeated project policy cannot be expressed by existing rules. Test it against valid and invalid examples before sharing it.
When migrating from .eslintrc, move settings into eslint.config.mjs. Replace string-based extends entries with imported configs, then verify file patterns and ignores because flat config uses different resolution behavior.
Exercise
Why does await save("a") satisfy no-floating-promises?
Show answer
It explicitly handles the promise, so the type-checked rule no longer sees an unhandled asynchronous operation.