Airbnb ESLint Config for TypeScript
eslint-config-airbnb-typescript extends the popular Airbnb ESLint configuration to provide full support for TypeScript projects. It ensures consistent code style and identifies potential issues across JavaScript and TypeScript files. Currently stable at version 18.0.0, this configuration typically updates in lockstep with major releases of `@typescript-eslint/eslint-plugin`, `@typescript-eslint/parser`, and `eslint`, as well as updates to `eslint-config-airbnb` or `eslint-config-airbnb-base`. The project has a moderately active release cadence, issuing updates to align with its core dependencies and address bug fixes. Its key differentiator is providing a TypeScript-compatible wrapper around the widely adopted and opinionated Airbnb style guide, making it a go-to choice for teams wanting to enforce a strict, well-defined coding standard in their TypeScript codebase. It requires manual peer dependency installation, a common pattern for shareable ESLint configurations.
Common errors
-
The file must be included in at least one of the projects provided.
cause ESLint is attempting to lint a file that is not covered by the `include` array in the `tsconfig.json` referenced by `parserOptions.project`.fixCreate a `tsconfig.eslint.json` that extends your main `tsconfig.json` and explicitly includes all files you intend to lint (e.g., `"include": ["src/**/*.ts", "src/**/*.js", "test/**/*.ts"]`). Then update your `.eslintrc` to `parserOptions: { project: './tsconfig.eslint.json' }`. -
ESLint: Cannot find module '@typescript-eslint/eslint-plugin/package.json' or its corresponding type declarations.
cause One or more of the required peer dependencies for `eslint-config-airbnb-typescript` (especially `@typescript-eslint/eslint-plugin` or `@typescript-eslint/parser`) are either not installed or are of an incompatible version.fixEnsure all peer dependencies are correctly installed with compatible versions. For v18.0.0, this means `npm install --save-dev @typescript-eslint/eslint-plugin@^7.0.0 @typescript-eslint/parser@^7.0.0 eslint@^8.56.0`. -
ESLint: Cannot read config file: ./.eslintrc.js
cause Incorrect ESLint configuration file format or path, or missing required base config like `eslint-config-airbnb`.fixVerify that your `.eslintrc` file is valid JSON or CommonJS, correctly extends `airbnb` (or `airbnb-base`) before `airbnb-typescript`, and that the base Airbnb config is installed as a dependency.
Warnings
- breaking Version 18.0.0 introduced a breaking change by upgrading peer dependencies. You must update `@typescript-eslint/eslint-plugin` and `@typescript-eslint/parser` to `^7.0.0` or above, and `eslint` to `^8.56.0` or above.
- breaking Upgrading to version 17.0.0 requires updating `@typescript-eslint/eslint-plugin` to `^5.13.0` due to a peer dependency bump related to the `space-before-blocks` rule.
- breaking Version 16.0.0 introduced breaking changes by updating to ESLint v8 and requiring `eslint-config-airbnb` dependency to `^19.0.0` (or `eslint-config-airbnb-base` to `^15.0.0`).
- gotcha This package relies heavily on its peer dependencies. Failure to install the correct versions of `eslint`, `@typescript-eslint/eslint-plugin`, and `@typescript-eslint/parser` will result in errors or incorrect linting behavior.
- gotcha When using this config, you must set `parserOptions.project` in your ESLint configuration to the path of your `tsconfig.json` file to enable type-aware linting rules.
Install
-
npm install eslint-config-airbnb-typescript -
yarn add eslint-config-airbnb-typescript -
pnpm add eslint-config-airbnb-typescript
Imports
- airbnb-typescript
require('eslint-config-airbnb-typescript')extends: ['airbnb-typescript']
- airbnb-typescript/base
extends: ['airbnb-typescript/base.js']
extends: ['airbnb-typescript/base']
- parserOptions.project
parserOptions: { tsconfigRootDir: __dirname, project: ['./tsconfig.json'] }parserOptions: { project: './tsconfig.json' }
Quickstart
npm install --save-dev eslint-config-airbnb-typescript \
@typescript-eslint/eslint-plugin@^7.0.0 \
@typescript-eslint/parser@^7.0.0 \
eslint@^8.56.0 \
eslint-config-airbnb # or eslint-config-airbnb-base
// .eslintrc.cjs (or .js)
module.exports = {
extends: [
'airbnb', // or 'airbnb-base' if no React
'airbnb-typescript'
],
parserOptions: {
project: './tsconfig.json' // Path to your TypeScript config
},
root: true,
env: {
node: true,
browser: true,
es2021: true
}
};
// tsconfig.json
{
"compilerOptions": {
"target": "es2021",
"module": "esnext",
"lib": ["dom", "es2021"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
// ... other compiler options
},
"include": ["src/**/*", ".eslintrc.cjs"]
}
// package.json script for linting
// "lint": "eslint . --ext .js,.jsx,.ts,.tsx"