liveBook · Manning

Typescript-eslint

As explained in Secrets of the JavaScript Ninja, Third Edition

A collection of tools and configurations for TypeScript-aware linting with ESLint.

typescript-eslint connects TypeScript’s type information to ESLint, enabling rules that can detect issues ordinary lint rules may miss. Its rule sets also let teams enforce coding conventions, with broader checks trading faster linting for deeper analysis. section 2.2.1 section 2.4

A TypeScript-aware ESLint setup can:

  • Apply strictTypeChecked and stylisticTypeChecked rules to TypeScript files, using project-service configuration to provide type information. section 2.2.1
  • Handle an opinionated rule by rewriting code, disabling the rule project-wide, or adding a targeted suppression comment. section 2.2.2

Catching an unnecessary condition

If `ninja` is inferred as either `"ryu"` or `"naruto"`, `if (ninja)` is always truthy. The `@typescript-eslint/no-unnecessary-condition` rule reports it; a condition like `ninja === "ryu" || "naruto"` also triggers ESLint’s `no-constant-condition` rule. section 2.2.1

Listing 2.8 A starter ESLint configuration for a TypeScript project
// @ts-check #A
import eslint from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
 
export default defineConfig([
  { #B
    extends: [eslint.configs.recommended], #B
  }, #B
  {
    files: ["**/*.{ts,mts}"], #C
    extends: [ #C
      tseslint.configs.strictTypeChecked #C
      tseslint.configs.stylisticTypeChecked, #C
    ], #C
    rules: { #D
      "@typescript-eslint/no-non-null-assertion": "off", #D
    }, #D
    languageOptions: { #E
      parserOptions: { #E
        projectService: true, #E
        tsconfigRootDir: import.meta.dirname, #E
      }, #E
    }, #E
  },
]);

The configuration separates general ESLint rules from TypeScript-file rules and enables type information for typed linting. section 2.2.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Configures typed rule sets and project-service integration, noting that type-aware linting expands checks but runs more slowly.
Examples
Demonstrates unnecessary-condition diagnostics and choices for handling a non-null assertion rule.
What it assumes
Introduces installation and configuration steps rather than assuming an existing TypeScript-aware ESLint setup.
What you can do after
Configure typed ESLint rules and choose whether to rewrite, disable, or locally suppress a flagged pattern.
How it connects
Connects ESLint, TypeScript, type information, and configurable rule sets.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.