ESLint is primarily designed for JavaScript. While it has support for TypeScript, it doesn’t have built - in type checking capabilities like the TypeScript compiler. TypeScript errors are related to type safety, such as incorrect type assignments, using non - existent properties on an object, etc. For ESLint to show TypeScript errors, it needs to be configured properly with TypeScript - specific plugins and parsers.
@typescript-eslint/parser
is used. It allows ESLint to understand TypeScript syntax.@typescript-eslint/eslint-plugin
provides a set of rules specifically for TypeScript.First, you need to install ESLint and the necessary TypeScript - related packages. In your project directory, run the following command:
npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
Initialize ESLint in your project by running:
npx eslint --init
During the initialization process, you’ll be asked a series of questions. Make sure to select options that are appropriate for TypeScript, such as choosing the TypeScript support.
Open the .eslintrc.js
(or other configuration file format) and make the following changes:
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module'
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
rules: {
// You can add custom rules here
}
};
package.json
Add the following script to your package.json
to easily run ESLint:
{
"scripts": {
"lint": "eslint src --ext .ts,.tsx"
}
}
Now you can run npm run lint
to check your TypeScript code for errors.
--fix
OptionESLint has a --fix
option that can automatically fix many common errors. You can update the lint
script in package.json
to:
{
"scripts": {
"lint": "eslint src --ext .ts,.tsx --fix"
}
}
Create an .eslintignore
file in your project root to specify files and directories that ESLint should ignore. For example:
node_modules
dist
Regularly update ESLint, @typescript-eslint/parser
, and @typescript-eslint/eslint-plugin
to the latest versions. Newer versions often come with bug fixes and improved TypeScript support.
If the built - in rules don’t meet your project’s specific requirements, you can write custom rules. You can use the @typescript-eslint
framework to create rules that are aware of TypeScript types.
Integrate ESLint with your IDE (e.g., Visual Studio Code). Most IDEs have ESLint extensions that can show errors in real - time as you code. In Visual Studio Code, you can install the ESLint extension and configure it to use your project’s ESLint configuration.
ESLint not showing TypeScript errors can be a challenging issue, but by understanding the fundamental concepts, setting up ESLint correctly, following common practices, and implementing best practices, you can ensure that ESLint effectively catches and reports TypeScript errors. This will help you maintain high - quality TypeScript code and improve your development workflow.