【问题标题】:tsconfig not used by typescript-eslinttypescript-eslint 未使用 tsconfig
【发布时间】:2019-12-06 07:04:42
【问题描述】:

在一个新项目中,我安装了typescript、eslint、@typescript-eslint/parser、@typescipt-eslint/eslint-plugin。我还添加了以下.eslintrc 文件:

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"]
}

以及以下tsconfig.json 文件:

{
  "compilerOptions": {
    "strict": true
  }
}

问题是当我运行命令eslint 时,来自tsconfig.json 的选项未应用。不过,它使用命令 tsc 可以正常工作。

例如,文件index.ts 包含:

function sum(a, b) {}

如果我运行npx eslint index.js,我没有错误,如果我运行tsc --noEmit,我有两个:

  • 错误 TS7006:参数“a”隐式具有“任何”类型。
  • 错误 TS7006:参数“b”隐式具有“任何”类型。

我希望eslint 命令返回与tsc 命令相同的错误。 有什么想法吗?

编辑我尝试在.eslintrc 中使用和不使用以下内容:

"parserOptions": {
  "project": "./tsconfig.json"
}

【问题讨论】:

    标签: typescript eslint typescript-eslint


    【解决方案1】:

    typescript-eslint 不报告编译器警告。它只报告由自己的验证规则生成的警告。另外,在 TypeScript 中启用 strict 选项对 typescript-eslint 执行的代码分析没有影响,它不依赖于项目设置。

    some discussions 关于创建新的 @typescript-eslint/no-undef 规则(以 ESLint no-undef 规则为蓝本)将捕获至少一些由 tsc 编译器生成的警告,并启用严格的类型检查。

    目前最好的方法可能是将 tsc --noEmit 的执行集成到 lint 进程中。

    【讨论】:

    • 能否提供一个在 lint 进程中集成 tsc --noEmit 执行的示例?
    • @olefrank 我手头没有运行 tsc 可执行文件的示例,但在我的一个项目中,我使用 TypeScript 编译器 API 和最小的 options 集(包括noEmit) 来做这种 linting。关键是 typescript 模块提供的函数createProgram,它会在编译时引发错误 - 从而停止构建。感兴趣的代码是here。我看看能不能想出一个简化的例子。
    • @GOTO0 对该简化示例有任何更新吗?也许 eslint-typescript 对此有一些更新?
    猜你喜欢
    • 2020-11-30
    • 2020-05-06
    • 2019-05-15
    • 2016-06-08
    • 1970-01-01
    • 2021-01-19
    • 2021-12-08
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多