10

I have ESLint extension installed in VSC and it works fine for .js files. Now I've introduced Typescript to my React project, ESLint is no longer working on the fly in VSC.

I have a .eslintrc file in the root of my project like so:

{
  "parser": "@typescript-eslint/parser",
  "extends": [
    "airbnb",
    "plugin:@typescript-eslint/recommended"
  ],
  "env": {
    "browser": true,
    "node": true,
    "serviceworker": true
  },
  "parserOptions": {
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "settings": {
    "react": {
      "version": "detect"
    },
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  },
  "rules": {
    "@typescript-eslint/indent": ["error", 2],
    "indent": "off",
    "jsx-a11y/anchor-is-valid": ["error", {
      "aspects": ["invalidHref", "preferButton"]
    }],
    "no-console": ["error", {
      "allow": ["error", "info"]
    }],
    "react/no-danger": 0
  }
}

I have an npm script that looks like this:

"scripts": {
  "eslint": "eslint . --ext .js,.jsx,.ts,.tsx",
},

If I run npm run eslint, the whole project gets linted correctly. What do I need to do for the project to lint on the fly with red squiggly lines in VSC?

1 Answer 1

39

Assuming you've got eslint extension installed on Visual Studio Code, you should add the following to your settings.

    "eslint.validate": [
        "javascript",
        "javascriptreact",
        "typescript",
        "typescriptreact",
    ],

This will make eslint look at typescript files as well as javascript files.

5
  • this is great. I was spending too much time with my ESLint config and the documentation and this fixed it! Commented Jul 8, 2021 at 5:08
  • 1
    @Siva-Dev-Wizard I'm not 100% sure this answer is "the correct way" anymore. I think out of the box the eslint plugin in vscode should now scan for TypeScript/ReactTS files. I'll update this answer once I've researched it later though to keep this as correct as possible.
    – N.J.Dawson
    Commented Jul 8, 2021 at 12:27
  • makes sense. I will check on my end too. Commented Jul 12, 2021 at 7:20
  • 6
    still not working after adding the above line.
    – npkp
    Commented Sep 6, 2021 at 11:18
  • "not working" isn't debuggable. What happens? Does JS work? Etc.
    – N.J.Dawson
    Commented Sep 15, 2021 at 8:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.