Skip to content
jshacks.dev
Tooling

Add type-aware lint rules with typescript-eslint

Turn on type-aware lint rules such as no-floating-promises with typescript-eslint and projectService, and see the rules that need it.

1 min read
Summarize in AI

Rules that need to know a value's type, such as "this promise is never awaited", cannot work from syntax. typescript-eslint can ask the TypeScript compiler, if you tell it where your project is.

eslint.config.jsJavaScript
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  eslint.configs.recommended,
  tseslint.configs.recommendedTypeChecked,
  {
    languageOptions: {
      parserOptions: {
        projectService: true,
        tsconfigRootDir: import.meta.dirname,
      },
    },
  },
);
src/job.tsTypeScript
async function save() {}
save(); // flagged by @typescript-eslint/no-floating-promises
Text
src/job.ts
  2:1  error  Promises must be awaited, end with a call to .catch, or end with a call to .then with a rejection handler  @typescript-eslint/no-floating-promises

recommendedTypeChecked adds rules such as no-floating-promises, no-misused-promises and await-thenable. projectService: true finds the nearest tsconfig.json for each file, which replaced the older project setting. Linting is slower, because TypeScript builds the type graph. See typescript-eslint typed linting.

Files that are not in a tsconfig, such as eslint.config.js, cause a parsing error. Turn the type-aware rules off for them with tseslint.configs.disableTypeChecked in a files block.

Also inTypeScript
Share

More snippets

TypeScript

Make every property required with Required

Required of T removes the question mark from every property, which types a config object after its defaults have been filled in. TypeScript 5 and earlier.

1 min read
Tooling

Create a Vite project from a template

Create a Vite project from a template with npm create vite, choose the vanilla-ts or react-ts template and see which files it generates.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.