Sub topic 7
Turning on Type-Aware Linting
Assume you know basic TypeScript, have a tsconfig.json, and can run npm commands. This chapter uses ESLint’s flat config format and the typescript-eslint package.
Why type-aware linting matters
Basic linting checks syntax and patterns in source code. Type-aware linting also reads TypeScript’s type information. It can then detect problems that are invisible from the text alone.
For example, ESLint can see that save is called. A type-aware rule can also see that save returns a Promise. The rule can report that the promise is ignored instead of awaited.
Type-aware linting needs two parts:
- A TypeScript project that describes the files and compiler settings.
- An ESLint configuration that asks
typescript-eslintto use that project.
flowchart TD
A[TypeScript files] --> B[tsconfig.json]
B --> C[TypeScript project service]
C --> D[type information]
D --> E[type-checked ESLint rules]Enable the project service
Install ESLint and the TypeScript integration if they are not already dependencies:
npm install --save-dev eslint typescript typescript-eslintCreate or update 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,
},
},
},
);recommendedTypeChecked enables recommended rules that need type information. The projectService: true option lets typescript-eslint find the appropriate TypeScript project for each file. tsconfigRootDir makes the project lookup relative to the directory containing this config file.
Run ESLint from the project directory:
npx eslint .The project service normally uses the nearest tsconfig.json. That file must include the TypeScript files you want to lint. If ESLint reports that a file is not included in the project, check the include, exclude, and project references in your TypeScript configuration.
> callout note > Type-aware linting is more expensive than syntax-only linting because TypeScript must build project information. Enable it for files where its checks provide value, and avoid creating unnecessary duplicate TypeScript projects.
Fix a promise violation
Consider this code:
async function save(id: string): Promise<void> {
console.log(`Saved ${id}`);
}
export function run(): void {
save('a');
}The save call creates a promise, but run does not wait for it. With the type-checked recommended preset, @typescript-eslint/no-floating-promises reports this call.
Make run asynchronous and await the promise:
async function save(id: string): Promise<void> {
console.log(`Saved ${id}`);
}
export async function runFixed(): Promise<void> {
await save('a');
}Changing the function’s return type to Promise<void> tells callers that they must also handle the asynchronous operation. This is usually safer than hiding the promise.
When to use type-aware rules
Use type-aware linting when your project benefits from rules about:
- Unhandled promises.
- Unsafe assignments, calls, or member access.
- Redundant conditions that types can prove.
- Incorrect use of asynchronous functions.
For a small project, enable the preset globally. In a large repository, measure lint time and consider applying type-aware settings only to project source files. Keep generated files, build output, and unrelated JavaScript outside the checked scope.
Exercise
What two settings make recommended type-checked rules work with a TypeScript project?
Show answer
Use tseslint.configs.recommendedTypeChecked and set languageOptions.parserOptions.projectService to true. The project service then obtains type information from tsconfig.json.