Ben
Ben

Reputation: 681

How to run typescript-eslint on .ts files and eslint on .js files in the same project in VSCode

I have a mixed codebase with javascript and typescript files. I'd like to have eslint run on .js files and typescript-eslint run on .ts files in VSCode.

I've managed to configure my .eslintrc.json file to get typescript-eslint to run on .ts files. The problem is that it also ends up running typescript-eslint on .js files, instead of plain old eslint.

This seems like it should be simple and fairly common, but I haven't been able to find a solution despite searching all over the internet.

Upvotes: 65

Views: 41898

Answers (4)

commonpike
commonpike

Reputation: 11185

In flat config - same answer as @rpm above, there is a similar rule to disable type checking for a set of files

https://typescript-eslint.io/getting-started/typed-linting/#how-can-i-disable-type-aware-linting-for-a-subset-of-files

{
    files: ['**/*.js'],
    ...tseslint.configs.disableTypeChecked,
  },

Upvotes: 1

You need to override the configuration to use separate parsers for js and ts files. you can configure .eslintrc.js as below

module.exports = {
    root: true,    
    extends: [
      'eslint:recommended'
    ],
    "overrides": [
      {
        "files": ["**/*.ts", "**/*.tsx"],
        "env": { "browser": true, "es6": true, "node": true },
        "extends": [
          "eslint:recommended",
          "plugin:@typescript-eslint/eslint-recommended",
          "plugin:@typescript-eslint/recommended"
        ],
        "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly" },
        "parser": "@typescript-eslint/parser",
        "parserOptions": {
          "ecmaFeatures": { "jsx": true },
          "ecmaVersion": 2018,
          "sourceType": "module",
          "project": "./tsconfig.json"
        },
        "plugins": ["@typescript-eslint"],
        "rules": {
          "indent": ["error", 2, { "SwitchCase": 1 }],
          "linebreak-style": ["error", "unix"],
          "quotes": ["error", "single"],
          "comma-dangle": ["error", "always-multiline"],
          "@typescript-eslint/no-explicit-any": 0
        }
      }
    ]
  };

Upvotes: 72

rpm
rpm

Reputation: 1362

typescript-eslint has added a super simple config (permalink here) to disable type-aware linting for all js files. Add the following to your .eslintrc.js:

module.exports = {
  ...
  overrides: [
    {
      files: ['*.js'],
      extends: ['plugin:@typescript-eslint/disable-type-checked'],
    },
  ],
};

Note: This plugin works by disabling all eslint-typescript type-aware lint rules for js files. As the eslint-typescript docs state:

If you use type-aware rules from other plugins, you will need to manually disable these rules or use a premade config they provide to disable them.

Upvotes: 5

xy2_
xy2_

Reputation: 7162

Use the overrides prop to have typescript-eslint's parser and related TS configuration only on .ts/.tsx files. For example (React, TypeScript, ES2021):

module.exports = {
  root: true,
  extends: ['eslint:recommended'],
  env: {
    browser: true,
    es2021: true,
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  overrides: [
    {
      files: ['**/*.ts', '**/*.tsx'],
      plugins: [
        '@typescript-eslint',
      ],
      extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
      parser: '@typescript-eslint/parser',
      parserOptions: {
        project: ['./tsconfig.json'],
      },
    },
  ],
};

Upvotes: 18

Related Questions