Sub topic 8
Catching Real Bugs with Type-Checked Rules
This chapter assumes you know TypeScript syntax, have a working tsconfig.json, and have already enabled typescript-eslint in ESLint's flat config. It also assumes you understand that TypeScript checks types during compilation, while ESLint checks source code against rules.
What type-checked rules add
Many lint rules can inspect source text without understanding the program's types. For example, a rule can detect an unused variable by looking at declarations and references.
Type-checked rules go further. They use the TypeScript program created from your tsconfig.json. This gives ESLint information such as:
- The type returned by a function.
- Whether a value can be
nullorundefined. - Whether an expression is a
Promise. - Which properties and methods exist on a value.
- How types flow through function calls.
The recommendedTypeChecked preset enables rules that use this information. These rules can find bugs that ordinary syntax-based linting cannot see.
A common example: floating promises
A floating promise is a promise that starts but is not awaited, returned, or deliberately handled. The operation may fail after the current function has already finished.
The @typescript-eslint/no-floating-promises rule can detect this because it knows that save returns Promise<void>.
async function save(id: string): Promise<void> {
console.log(`Saving ${id}`);
}
export function run(): void {
save('a'); // Error: this promise is not handled
}
export async function runFixed(): Promise<void> {
await save('a');
}The fixed version uses await, so errors from save remain part of the control flow. Returning the promise is another valid choice when the caller should handle the result:
export function runAndDelegate(): Promise<void> {
return save('a');
}Whether you should await or return depends on the function's contract. The important point is to make the decision explicit.
Useful type-checked rules
The exact rules available depend on the typescript-eslint version and preset. These examples show the kinds of bugs that type information can expose.
| Rule | Problem it catches | Typical correction |
|---|---|---|
| `no-floating-promises` | An asynchronous operation is ignored | Await, return, or explicitly handle the promise |
| `no-misused-promises` | A promise is used where synchronous behavior is expected | Wrap the call or use an API that accepts a promise |
| `no-unnecessary-condition` | A condition cannot be true for the declared types | Remove it or correct the type |
| `no-unsafe-member-access` | A property is read from an `any` value | Validate or narrow the value first |
Enable the type-aware preset and project service in the flat config:
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,
},
},
},
);Run ESLint from the project directory:
npx eslint .Common errors
Type-checked linting can report errors when a file is not included by any tsconfig.json. It can also become slow when ESLint must build large or numerous TypeScript projects.
Check these points when setup fails:
- Run ESLint where the intended
tsconfig.jsonis discoverable. - Keep generated files and dependencies outside the lint target.
- Use a file-scoped override when JavaScript files should not use type-checked rules.
- Treat lint reports as prompts to inspect behavior, not as reasons to add blanket disable comments.
> callout warning > A type-checked rule can be correct according to the declared types while the types themselves are wrong. Keep TypeScript's compiler checks enabled. Linting complements type checking; it does not replace it.
Exercise
In run(): void, why does save('a'); trigger no-floating-promises, and name two valid fixes.
Show answer
save returns a promise that the function ignores. Await the promise or return it to the caller. You could also attach deliberate error handling when that matches the operation's contract.