On this page

Linting & Formatting

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Linting and formatting are essential parts of a professional TypeScript workflow.

They ensure:

This module shows how to set up ESLint for TypeScript and integrate Prettier for formatting—without conflicts.


Why Linting & Formatting Matter​

TypeScript catches type errors, but not:

ESLint + Prettier fill those gaps.

Together, they create a clean, safe, consistent codebase.


ESLint + TypeScript​

ESLint is the standard linter for JavaScript and TypeScript.

To use it with TypeScript, you need:


1. Install ESLint for TypeScript​


npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

2. Create an ESLint config​

.eslintrc.cjs​


module.exports = {

  parser: "@typescript-eslint/parser",

  parserOptions: {

    ecmaVersion: "latest",

    sourceType: "module",

  },

  plugins: ["@typescript-eslint"],

  extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],

  rules: {

    // Add custom rules here

  },

};

What this does​

This is the minimal, modern setup.


3. Run ESLint​


npx eslint src --ext .ts

Fix automatically:


npx eslint src --ext .ts --fix

Useful TypeScript‑Specific Rules​

1. No implicit any​


"@typescript-eslint/no-explicit-any": "warn"

2. Require explicit return types​


"@typescript-eslint/explicit-function-return-type": "off"

(Usually turned off for ergonomics.)

3. No unused variables​


"@typescript-eslint/no-unused-vars": "warn"

4. Enforce consistent type imports​


"@typescript-eslint/consistent-type-imports": "warn"

This avoids mixing:


import type { User } from "./types";

import { User } from "./types";

Prettier Integration​

Prettier is a formatter, not a linter.

It handles:

ESLint handles logic; Prettier handles style.

To avoid conflicts, we use:


1. Install Prettier + ESLint integration​


npm install --save-dev prettier eslint-config-prettier eslint-plugin-prettier

2. Add Prettier config​

.prettierrc​


{

  "semi": false,

  "singleQuote": true,

  "trailingComma": "all"

}

(Use whatever style your team prefers.)


3. Update ESLint config​

.eslintrc.cjs​


module.exports = {

  parser: "@typescript-eslint/parser",

  parserOptions: {

    ecmaVersion: "latest",

    sourceType: "module",

  },

  plugins: ["@typescript-eslint", "prettier"],

  extends: [

    "eslint:recommended",

    "plugin:@typescript-eslint/recommended",

    "plugin:prettier/recommended",

  ],

  rules: {

    "prettier/prettier": "warn",

  },

};

What this does​

Now running ESLint will also format your code.


4. Format Code Automatically​


npx eslint src --ext .ts --fix

Or run Prettier directly:


npx prettier --write .

Editor Integration​

Most editors (VS Code, WebStorm) support:

VS Code recommended settings​

.vscode/settings.json


{

  "editor.formatOnSave": true,

  "editor.defaultFormatter": "esbenp.prettier-vscode",

  "eslint.validate": ["typescript"]

}

This gives you a seamless experience.


Common Pitfalls​

1. ESLint and Prettier fighting each other​

Fix:

Use eslint-config-prettier and plugin:prettier/recommended.


2. ESLint not picking up TypeScript​

Fix:

Ensure:


"parser": "@typescript-eslint/parser"

3. Prettier not formatting​

Fix:

Set Prettier as the default formatter in your editor.


4. Slow ESLint performance​

Fix:

Use:


"parserOptions": { "project": "./tsconfig.json" }

only when necessary (e.g., for rules requiring type info).


Recommended Setup for Most Projects​

package.json​


{

  "scripts": {

    "lint": "eslint src --ext .ts",

    "lint:fix": "eslint src --ext .ts --fix",

    "format": "prettier --write ."

  }

}

Workflow​

This keeps your codebase clean and consistent.


Summary​

In this lesson, you learned how to set up linting and formatting for TypeScript:

1. ESLint + TypeScript​

2. Prettier integration​

Together, ESLint + Prettier form the backbone of a professional TypeScript workflow.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Bundlers & Frameworks

Next

Testing TypeScript