Back to Typescript Eslint

Getting Started

docs/getting-started/Quickstart.mdx

8.60.15.1 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Quickstart

This page is a quick-start for ESLint's new "flat" config format to go from zero to linting with our recommended rules on your TypeScript code as quickly as possible.

:::note

:::

Step 1: Installation

First, install the required packages for ESLint, TypeScript, and our tooling:

bash
npm install --save-dev eslint @eslint/js typescript typescript-eslint

Step 2: Configuration

Next, create an eslint.config.mjs config file in the root of your project, and populate it with the following:

js
// @ts-check

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

export default defineConfig({
  files: ['**/*.{js,ts}'],
  extends: [js.configs.recommended, tseslint.configs.recommended],
});

This code will enable our recommended configuration for linting.

Details

  • // @ts-check enables TypeScript to typecheck the config file, which improves editor support and can help catch errors when authoring your ESLint config. It's also fine to leave this off if it causes problems with your TypeScript setup.
  • defineConfig(...) is an optional helper function built in to current versions of ESLint. See the ESLint configuration docs for more detail.
  • files/extends: this setup is an optional feature of defineConfig(...) that restricts the extends configs to JS and TS files, and can be modified to suite your project's needs. We recommend this in order to avoid issues if you enable ESLint to run on other file types such as CSS or Markdown files, but it's entirely optional. To enable linting for all standard JS and TS file extensions, consider using files: ['**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}']. See the Framework FAQs to use typescript-eslint on file types other than those.
  • '@eslint/js' / js.configs.recommended turns on eslint's recommended config.
  • tseslint.configs.recommended turns on our recommended config.
<details> <summary>Aside on file extensions</summary>

The .mjs extension makes the file use the ES modules (ESM) format. Node interprets .js files in the CommonJS (CJS) format by default, but if you have "type": "module" in your package.json, you can also use eslint.config.js.

</details>

Step 3: Running ESLint

Open a terminal to the root of your project and run the following command:

<Tabs groupId="npm2yarn"> <TabItem value="npm">
bash
npx eslint .
</TabItem> <TabItem value="Yarn">
bash
yarn eslint .
</TabItem> <TabItem value="pnpm">
bash
pnpm eslint .
</TabItem> </Tabs>

ESLint will lint all TypeScript compatible files within the current folder, and will output the results to your terminal.

Next Steps

If you're having problems getting this working, please have a look at our Troubleshooting & FAQs.

Additional Configs

We recommend you consider enabling the following two configs:

  • strict: a superset of recommended that includes more opinionated rules which may also catch bugs.
  • stylistic: additional rules that enforce consistent styling without significantly catching bugs or changing logic.
js
export default defineConfig({
  files: ['**/*.{js,ts}'],
  extends: [
    js.configs.recommended,
    // Remove this line
    tseslint.configs.recommended,
    // Add this line
    tseslint.configs.strict,
    // Add this line
    tseslint.configs.stylistic,
  ],
});

You can read more about these in our shared configurations docs.

Typed Linting

We also provide a plethora of powerful rules that utilize the power of TypeScript's type information. Visit the next page for a typed rules setup guide.

Documentation Resources