admin 管理员组

文章数量: 1086019

When I have code:

const test = (testVar) => {
    console.log(testVar);
}

I get error in VS Code from TS and also while building TS, that Parameter 'testVar' implicitly has an 'any' type., so it's OK. But can I get similar error in ESLint with TypeScript?

Or ESLint serves different purpose in this case (like if I added testVar: any ESLint will throw error while TS would build ok)?

I'd assume that I could use ESLint to catch all TS errors as well, to have all errors in one place, and not running first ESLint and later test TS as well, to find any errors.

When I have code:

const test = (testVar) => {
    console.log(testVar);
}

I get error in VS Code from TS and also while building TS, that Parameter 'testVar' implicitly has an 'any' type., so it's OK. But can I get similar error in ESLint with TypeScript?

Or ESLint serves different purpose in this case (like if I added testVar: any ESLint will throw error while TS would build ok)?

I'd assume that I could use ESLint to catch all TS errors as well, to have all errors in one place, and not running first ESLint and later test TS as well, to find any errors.

Share Improve this question edited Mar 27 at 14:06 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 27 at 13:53 user3235231user3235231 1
Add a comment  | 

1 Answer 1

Reset to default 1

ESLint is primarily a linter for code style and patterns, not a type checker. However, with TypeScript support, it can catch some type-related issues.

If the question is "Can Eslint catch type error?" the answer is yes, but with limitations.

If you want ESLint to flag implicit any usage, you can enable the @typescript-eslint/no-implicit-any rule :

{
  "rules": {
    "@typescript-eslint/no-implicit-any": "error"
  }
}

But in fact should you rely on ESLint for type checking ?

No, and here's why:

  • TypeScript's type checker is optimized for this work. Running it through ESLint is slower.
  • ESLint won't catch all type errors that TypeScript would.
  • We typically uses TypeScript compiler (tsc) or ts-loader (Webpack) for type checking and ESLint for code style and patterns

While you can make ESLint catch some type-related issues, it's not a complete replacement for TypeScript's type checking. The most robust setup uses both tools for their primary purposes.

本文标签: typescriptCan ESLint do similar check as TS does with noImplicitAnyStack Overflow