liveBook · Manning

Linting

As explained in Essential TypeScript 5, Third Edition

A linter is a software tool that analyzes source code against configurable rules to identify problems or undesirable patterns.

A linter checks source files against rules for confusing code, unexpected behavior, and readability problems. It complements the TypeScript compiler by finding common problems the compiler does not detect. For TypeScript, typescript-eslint adapts ESLint’s JavaScript linting to TypeScript code. section 6.3 section 6.5

To lint a project in the tools folder:

  • Install the development packages: `eslint@8.36.0`, `@typescript-eslint/parser@5.55.0`, and `@typescript-eslint/eslint-plugin@5.55.0`. section 6.3
  • Create `tools/.eslintrc`. Set the TypeScript parser, point `parserOptions.project` at `./tsconfig.json`, and extend the recommended ESLint and typescript-eslint rule sets. Ignore `node_modules` and `dist`. section 6.3
  • From `tools`, run `npx eslint .`. The final period tells ESLint to check the current directory. section 6.3
Listing 6.10 The contents of the .eslintrc file in the tools folder
{
   "root": true,
   "ignorePatterns": ["node_modules", "dist"],
   "parser": "@typescript-eslint/parser",
   "parserOptions": {
     "project": "./tsconfig.json"
   },    
   "plugins": [
     "@typescript-eslint"
   ],
   "extends": [
     "eslint:recommended",
     "plugin:@typescript-eslint/eslint-recommended",
     "plugin:@typescript-eslint/recommended"
   ]
 }

A complete `.eslintrc` shows how the parser, TypeScript project file, ignored folders, and recommended rules fit together. section 6.3

Reading a lint result

If a variable declared with `let` is never reassigned, `prefer-const` reports it. If code contains `debugger`, `no-debugger` reports that statement. The diagnostic output identifies the file, line, rule, and number of errors; it also indicates which errors may be fixable with `--fix`. section 6.3

C:\tools\src\index.ts
  3:5  error  'printMessage' is never reassigned. 
      Use 'const' instead  prefer-const
          
  5:5  error  'message' is never reassigned. 
      Use 'const' instead       prefer-const
  8:1  error  Unexpected 'debugger' statement no-debugger
 10:5  error  'total' is never reassigned. 
      Use 'const' instead         prefer-const
          
4 problems (4 errors, 0 warnings)
 3 errors and 0 warnings potentially fixable with the `--fix` option.

The sample output shows rule names beside specific source locations and reports three potentially fixable errors. section 6.3

Keep the rules useful enough that important reports do not disappear in unwanted warnings. To disable `prefer-const` throughout the project, add `"rules": { "prefer-const": 0 }` to `.eslintrc`. To retain `no-debugger` generally while allowing a deliberate breakpoint, write `debugger; // eslint-disable-line no-debugger` in the source file. A project-wide rule change and a one-line exception solve different problems. section 6.3.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Walks through package installation, configuration, diagnostics, and rule exceptions rather than linter internals.
Examples
Uses `prefer-const` and `no-debugger` diagnostics, a `.eslintrc` rule override, and an inline `eslint-disable-line` comment.
What it assumes
Assumes a TypeScript project with a `tsconfig.json` file; teaches ESLint package installation and configuration explicitly.
What you can do after
Apply it via ESLint and typescript-eslint to check TypeScript files and tailor rules to a project.
How it connects
Connects linting to the TypeScript compiler, debugging, ESLint configuration, and TypeScript parsing.

Also explained in

Compare how 8 books explain linting →

Related concepts

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