Linting and formatting

Topic 2 of 4 in Configuration and tooling7 min readIntermediateFree course

Overview

TypeScript catches type errors, but plenty of bugs are perfectly well-typed: a promise nobody awaits, an any that quietly spreads, a variable that’s assigned but never used. A linter catches those. And every team wastes time debating semicolons and indentation until a formatter settles it automatically. This lesson sets up both, with TypeScript 7.

Three tools, three jobs. tsc is the type checker: wrong types, missing properties, impossible cases. ESLint plus typescript-eslint is the linter: floating promises, unused variables, risky patterns. Prettier or Biome is the formatter: indentation, quotes and commas, line length. Scripts: “check”: “tsc –noEmit”, “lint”: “eslint .”, “format”: “prettier –write .”. Run all three in CI. Format on save in your editor, so formatting never shows up in code review.

ESLint and typescript-eslint

ESLint is the standard JavaScript linter. typescript-eslint lets it parse TypeScript and adds TypeScript-aware rules — including rules that use the type checker to find real bugs.

Installing alongside TypeScript 7

typescript-eslint reads your code through TypeScript’s programmatic API. TypeScript 7.0 doesn’t have that API yet (it’s planned for 7.1), so typescript-eslint supports TypeScript up to 6.0, and installing it next to typescript@7 fails with an npm ERESOLVE peer-dependency error.

The TypeScript team’s recommended setup is to install both versions under different names:

JSON
{
  "devDependencies": {
    "typescript": "npm:@typescript/typescript6@^6.0.2",
    "@typescript/native": "npm:typescript@^7.0.2"
  }
}

Tools that import "typescript" (typescript-eslint, some build plugins) get the 6.0 API; the tsc command comes from TypeScript 7 — npx tsc --version prints Version 7.0.2, and tsc6 is available if you need it. Both check code the same way. Once typescript-eslint supports TypeScript 7, you can go back to a single typescript dependency.

Then:

Terminal
npm install -D eslint @eslint/js typescript-eslint

Configuration

Create eslint.config.js:

JavaScript
import eslint from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";

export default defineConfig(
  eslint.configs.recommended,
  tseslint.configs.recommendedTypeChecked,
  {
    languageOptions: {
      parserOptions: {
        projectService: true,
      },
    },
  },
  { ignores: ["dist/", "eslint.config.js"] },
);
  • recommendedTypeChecked turns on rules that use type information; projectService: true lets them find your tsconfig automatically. (Use tseslint.configs.recommended for a faster setup without type information, and strictTypeChecked for more rules.)

Run it:

Terminal
npx eslint .
Text
src/app.ts
   7:3   error  Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator  @typescript-eslint/no-floating-promises
  10:7   error  'unused' is assigned a value but never used                @typescript-eslint/no-unused-vars
  11:7   error  Unsafe assignment of an `any` value                        @typescript-eslint/no-unsafe-assignment
  11:14  error  Unexpected any. Specify a different type                   @typescript-eslint/no-explicit-any
  12:19  error  Unsafe member access .name on an `any` value               @typescript-eslint/no-unsafe-member-access

✖ 5 problems (5 errors, 0 warnings)

tsc had no complaints about this file. Each of these is a real problem: a promise whose failure would go unnoticed, dead code, and an any that switches checking off.

Rules worth knowing

Rule Catches
no-floating-promises calling an async function without await, .catch or void
no-misused-promises passing an async function where a sync callback is expected (e.g. if (asyncCheck()))
no-unsafe-* values of type any flowing into typed code
no-explicit-any writing any at all
switch-exhaustiveness-check a switch on a union missing cases (strict configs)
consistent-type-imports enforcing import type style

Disabling a rule (sparingly)

When a rule is wrong for one specific line, disable it there — with a reason:

TypeScript
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- third-party callback is untyped
const handler = (event: any) => { … };

Change rule severity for the whole project in the config: rules: { "@typescript-eslint/no-explicit-any": "warn" }.

Formatting with Prettier

Prettier reformats code to one consistent style. It has very few options on purpose — the point is to stop discussing style.

Terminal
npm install -D prettier
npx prettier --write .    # format everything
npx prettier --check .    # CI: fail if anything isn't formatted

Add a .prettierrc only if you need to change a default (e.g. { "printWidth": 100 }), and a .prettierignore for generated files. Prettier and ESLint don’t overlap: typescript-eslint’s recommended configs contain no formatting rules.

Or: Biome

Biome is a single fast tool (written in Rust) that formats and lints JavaScript and TypeScript, with Prettier-compatible formatting. Its lint rules don’t use the type checker, so rules like no-floating-promises aren’t available in the same form — some teams use Biome for formatting and quick lint rules, plus a small type-aware ESLint setup.

Wiring it together

package.json scripts

JSON
{
  "scripts": {
    "check": "tsc",
    "lint": "eslint .",
    "format": "prettier --write .",
    "format:check": "prettier --check ."
  }
}

In the editor

  • Install the ESLint and Prettier extensions in VS Code.
  • Turn on Format on Save ("editor.formatOnSave": true) and set Prettier as the default formatter.
  • Lint errors appear as squiggles alongside type errors.

In CI

Run all of them on every pull request, so problems never reach the main branch:

YAML
- run: npm ci
- run: npm run check
- run: npm run lint
- run: npm run format:check
- run: npm test

Remember: the type checker, the linter and the formatter each do a different job. Run all three in your editor and in CI — and fix lint errors rather than silencing them.

Exercise

Make the linter happy

Set up linting in a project as in the lesson: install the TypeScript 7 alias pair, eslint, @eslint/js, typescript-eslint and prettier, and create eslint.config.js with recommendedTypeChecked. The starter src/app.ts type-checks fine but ESLint reports five problems: a floating promise, an unused variable, any, and two unsafe uses of any. Fix each one properly (don’t disable rules): await or void the promise deliberately, remove dead code, and replace any with unknown plus a check. Make sure npm run lint, npm run check and npx prettier --check src all pass.

TypeScript
// src/app.ts
async function save(data: string): Promise<void> {
  await new Promise((resolve) => setTimeout(resolve, 10));
  console.log("saved", data);
}

function onClick(): void {
  save("draft");
}

const unused = 1;
const value: any = JSON.parse('{"name": "Asha"}');
console.log(value.name);
onClick();

Summary

  • tsc checks types; a linter catches risky patterns (floating promises, misused any); a formatter handles layout.
  • typescript-eslint’s type-aware configs (recommendedTypeChecked) find bugs the compiler allows.
  • Until typescript-eslint supports TypeScript 7, alias typescript to @typescript/typescript6 and install 7 as @typescript/native.
  • Prettier (or Biome) formats code automatically — run it on save and check it in CI.
  • Fix lint errors properly; disable a rule only for a specific line, with a comment explaining why.