Search code examples
typescriptvisual-studio-codeprettiereslint-config-airbnbprettier-eslint

Configure parserOptions or parser package for ESLint and Airbnb typescript setup


In my Gatsby (React) project I am using typescript. I need to setup ESLint and have to use eslint-config-airbnb-typescript

I am also using Prettier: prettier (comes with Gatsby), and I installed the following packages:

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

If I look at the documentation of eslint-config-airbnb-typescript, I don't see anything about the @typescript-eslint/parser package?

They showing an example like:

module.exports = {
  extends: ['airbnb-typescript'],
  parserOptions: {
    project: './tsconfig.json',
  }
};

What's the correct way? And in their example, you don't have to use the @typescript-eslint/parser package?

My ESLint config .eslintrc.js:

module.exports = {
  globals: {
    __PATH_PREFIX__: true,
  },
  env: {
    browser: true,
    es6: true,
  },
  plugins: ['react', '@typescript-eslint', 'prettier'],
  extends: [
    'airbnb-typescript',
    'airbnb/hooks',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'prettier/react',
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './tsconfig.json',
  },
  rules: {
    'prettier/prettier': [
      'error',
      {
        endOfLine: 'auto',
      },
    ],
  },
};

My tsconfig.js:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "esnext",
    "jsx": "preserve",
    "lib": ["dom", "es2015", "es2017"],
    "strict": true,
    "noEmit": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "baseUrl": "src"
  },
  "include": ["src/**/*"]
}

Solution

  • This is the way I set it up in eslint-config-auto, which automatically configures eslint based on your package.json config.

    overrides: [
      {
        files: ['**.ts', '**.tsx'],
        parser: '@typescript-eslint/parser',
        parserOptions: {
          project: './tsconfig.json',
        },
      },
    ],