Menu

Linting and Formatting

Lesson 1: Linting and Formatting

What do linters add to the compiler?

Tool Catches Example
TypeScript compiler Type errors Wrong argument type
typescript-eslint Type-correct but risky code A Promise that is never awaited
Prettier or Biome Formatting differences Quotes, spacing, line length

Example

npm install -D eslint @eslint/js typescript-eslint
// eslint.config.mjs
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 },
    },
    rules: {
      '@typescript-eslint/consistent-type-imports': 'error',
      '@typescript-eslint/no-floating-promises': 'error',
    },
  },
);

> Note: Linting tools such as typescript-eslint update their configuration format and TypeScript version support often, so package names, imports, and options may differ from the code above. Always follow the official typescript-eslint documentation for the versions you install.

Code the compiler accepts but the linter flags:

async function sendOrderEmail(orderId: string): Promise<void> { /* ... */ }

function placeOrder(orderId: string) {
  sendOrderEmail(orderId);
  // no-floating-promises: Promises must be awaited or handled.
}

const data: any = JSON.parse(raw);
const total: number = data.total;
// no-unsafe-assignment: Unsafe assignment of an `any` value.

How it works

Rule Why it matters
no-floating-promises Unhandled Promises hide failures
no-misused-promises Passing async functions where a sync callback is expected
no-explicit-any, no-unsafe-* Stops any from spreading silently
consistent-type-imports Enforces import type
switch-exhaustiveness-check Flags switch statements that miss union members
no-unnecessary-condition Finds checks that are always true or false
  • recommendedTypeChecked rules use type information, so they are slower but catch much more.
  • projectService lets the linter find the right tsconfig.json automatically.
  • typescript-eslint relies on the TypeScript compiler API. Check its documentation for TypeScript 7 support, and keep the @typescript/typescript6 compatibility package if needed (Lesson 4).

Common mistakes

Mistake Effect Fix
Using only non-type-aware rules Misses Promise and any bugs Enable recommendedTypeChecked
Arguing about formatting in code review Wasted time Let Prettier or Biome format automatically
Disabling rules project-wide when one line is a problem Protection lost everywhere Disable on the line with a reason

Key takeaways

  • The compiler checks types; typed lint rules catch risky patterns the compiler allows.
  • Automate formatting so reviews focus on logic.

Try it: Add typescript-eslint with recommendedTypeChecked to QuickCart, run it, and fix every no-floating-promises report.


TypeScript Tutorial

J

Jebasta





Get in Touch For Details