Sub topic 7
Turning on Type-Aware Linting
This chapter assumes you already have a working eslint.config.mjs with the typescript-eslint recommended preset, a tsconfig.json that includes your source files, and Node.js 20.11 or later (needed for import.meta.dirname). Here you will switch on type information for lint rules, see how ESLint obtains it, and fix the errors that setup problems usually cause.
What type-aware linting adds
By default, typescript-eslint parses your code into a syntax tree. Rules can inspect that tree, but they cannot know what any expression means. A rule that sees save('a'); cannot tell whether save returns a Promise, because that depends on a function declared elsewhere.
Type-aware linting gives rules access to the TypeScript type checker. A rule can then ask what type an expression has, whether a value is a Promise, or whether a call returns any. Rules that do this are called type-checked rules. They find bugs that syntax-only rules cannot, such as unawaited promises, unsafe use of any, and comparisons between incompatible types.
The trade-off is cost. ESLint now has to build a TypeScript program for your project, and that takes time and memory.
How the project service works
Type-checked rules need to know which tsconfig.json governs each file. typescript-eslint answers that question with the project service, an option you enable in the parser settings. For each file ESLint lints, the service locates the nearest tsconfig.json, in the same way your editor's TypeScript server does. It then builds or reuses a TypeScript program for that project.
This replaces the older parserOptions.project setting, where you listed tsconfig paths by hand. With projectService you do not list them. The service finds them, and it follows the same rules as your editor, so lint and editor agree about which files belong to which project.
Enabling it
Two changes are needed. Swap the syntax-only preset for its type-checked variant, and turn on projectService.
// 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 contains everything in recommended plus the rules that need type information. The strict and stylistic presets have matching strictTypeChecked and stylisticTypeChecked variants. Use the variant of whichever preset you chose in the previous chapter. You do not add the plain and type-checked versions of the same preset together, because the type-checked version already includes the plain one.
tsconfigRootDir tells the parser where to resolve tsconfig lookups from. Setting it to import.meta.dirname (the folder containing the config file) makes the result independent of the directory you run ESLint from. This matters in editors and in monorepos, where the working directory is often not the project root.
Your tsconfig.json must include the files you lint. A typical one looks like this:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"outDir": "dist"
},
"include": ["src", "test"]
}Type-aware rules work best with strict enabled. Without it, TypeScript treats more values as any, and several rules then have little to report.
A rule that needs types
@typescript-eslint/no-floating-promises is included in recommendedTypeChecked. It reports a promise that is created and then ignored, so a rejection would go unnoticed.
async function save(id: string): Promise<void> {
// persist the record
}
export function run(): void {
save('a'); // @typescript-eslint/no-floating-promises reports this call
}
export async function runFixed(): Promise<void> {
await save('a');
}The rule only works because it can see that save returns Promise<void>. With a syntax-only config, ESLint reports nothing for run. Run npx eslint . and you should see an error on the save('a') line. Awaiting the call removes it.
If the promise is deliberately not awaited, you have two honest options. Handle the failure with .catch(...), or mark the call with the void operator, which the rule accepts by default (ignoreVoid is true). A void marks the choice as intentional for the next reader.
Other rules in the same preset follow the same pattern:
await-thenablereportsawaiton a value that is not a promise.no-misused-promisesreports a promise passed where a plain boolean or a function returningvoidis expected, such as in anifcondition.no-unsafe-assignmentand its relatives report values of typeanyflowing into typed variables, arguments, and returns.
Files outside the project
The most common failure after enabling the project service is a parsing error saying a file "was not found by the project service". It means the file is not included in the nearest tsconfig.json. There are three usual causes and fixes.
| Situation | Typical file | Fix |
|---|---|---|
| Source or test file missing from tsconfig `include` | `test/api.test.ts` | Add its folder to `include` in `tsconfig.json`. |
| A config file that is not part of the build | `eslint.config.mjs`, `vitest.config.ts` | List it in `allowDefaultProject`, or add it to tsconfig. |
| Plain JavaScript with no TypeScript project | `scripts/build.js` | Turn off type-checked rules for those files. |
The first fix is the best one when the file really belongs to your project. Tests are the usual case, because many projects exclude them from the build tsconfig and then wonder why lint fails on them. A common answer is a separate tsconfig.json in the test folder, because the project service picks the nearest one.
For a small number of stray files, use allowDefaultProject:
// additional entry in the flat config array
{
languageOptions: {
parserOptions: {
projectService: {
allowDefaultProject: ['eslint.config.mjs'],
},
tsconfigRootDir: import.meta.dirname,
},
},
},allowDefaultProject takes file paths or simple globs with no folder wildcards such as **. It is capped at a handful of files by default. Each of these files is linted against a default TypeScript project, which is slow, and the cap exists to stop you from using it as a replacement for a real tsconfig.
For JavaScript files that are not in any project, disable the type-checked rules instead:
// additional entry in the flat config array
{
files: ['**/*.js', '**/*.mjs'],
extends: [tseslint.configs.disableTypeChecked],
},disableTypeChecked switches off every rule that needs types, so the remaining rules still run on those files without error.
Other errors to expect
- A rule requires type information but none is available. You enabled a type-checked preset without
projectService, or the file matched by the entry is outside the project. Check that the parser options appear in an entry that applies to the file. - Results differ between your editor and the command line. This usually points to a wrong
tsconfigRootDir. Set it explicitly as shown above. - A flood of
no-unsafe-*errors in a new project. These rules reportanyvalues, often from untyped libraries orJSON.parse. The cause is usually missing types, not a lint problem. Install@typespackages or add types at the boundary where the data enters your code. - Stale results after editing tsconfig. Restart the ESLint server in your editor, because the program is cached.
The performance cost
Building a TypeScript program means parsing and type-checking your project, so type-aware linting takes roughly as long as running tsc on the same files, and then some. Plan for this:
- Run
npx eslint . --cachelocally so unchanged files are skipped on later runs. - Narrow the
filesglobs of the type-checked config so it only applies to TypeScript sources, and usedisableTypeCheckedelsewhere. - Keep
includein your tsconfig no wider than needed.
Monorepos and CI timing are covered in a later chapter. For now, measure before and after enabling type-aware linting so you know what it costs your project.
Exercise
You enable recommendedTypeChecked and projectService: true. ESLint now fails on vitest.config.ts with "was not found by the project service". The file is not part of your build and you do not want to add it to tsconfig.json. What is the narrowest fix, and what should you avoid?
Show answer
Add vitest.config.ts to allowDefaultProject under projectService, since it is a single stray file. Avoid broad globs there, because the option is capped and slow. If the project had many such files, a tsconfig that includes them would be the better fix.
Exercise
A teammate enables strictTypeChecked and also keeps tseslint.configs.strict in the config array. Is the second entry needed?
Show answer
No. The type-checked variant already includes everything in the plain preset, so listing both adds nothing.