带TypeScript解析器/插件的ESLint:如何包括从tsconfig.json中排除的.spec.ts?

带TypeScript解析器/插件的ESLint:如何包括从tsconfig.json中排除的.spec.ts?,typescript,eslint,tsconfig,eslintrc,typescript-eslint,Typescript,Eslint,Tsconfig,Eslintrc,Typescript Eslint,我和ESLint一起使用了真棒 问题描述:TypeScript ESLint解析器抱怨src/module.spec.ts不是项目的一部分,这是正确的。我从TypeScripttsconfig.json文件中排除了所有spec.ts文件,因为它们不需要传输 如何使src/module.spec.ts不传输,但仍针对ESLint进行检查 我的项目\src\module.spec.ts 0:0错误解析错误:@typescript eslint/parser已设置“parserOptions.proj

我和ESLint一起使用了真棒

问题描述:TypeScript ESLint解析器抱怨
src/module.spec.ts
不是项目的一部分,这是正确的。我从TypeScript
tsconfig.json
文件中排除了所有
spec.ts
文件,因为它们不需要传输

如何使
src/module.spec.ts
不传输,但仍针对ESLint进行检查

我的项目\src\module.spec.ts 0:0错误解析错误:@typescript eslint/parser已设置“parserOptions.project”。 该文件与您的项目配置不匹配:src\module.spec.ts。 该文件必须包含在提供的至少一个项目中

My
.eslint.json
(精简版):

My
tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": false,
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "strict": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "./dist",
    "**/*.spec.ts"
  ]
}
"project": "test.tsconfig.json"

在包含
tsconfig.json
的项目文件夹中,创建另一个包含以下内容的json文件:

{
  "exclude": [
    "./dist"
  ],
  "extends": "./tsconfig.json"
}
确切的文件名与此无关。请注意,这是一个非常有效的TypeScript配置文件,其中所有设置都继承自
/tsconfig.json
,execept for
extends
,其中已删除模式
“***.spec.ts”

从这个新的JSON文件读取其设置的工具(与默认的
tsconfig.JSON
)不会忽略
src/module.spec.ts

然后在
parserOptions
下的
.eslint.json
中,将新json文件的名称设置为
project
,例如,如果文件名为
test.tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": false,
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "strict": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "./dist",
    "**/*.spec.ts"
  ]
}
"project": "test.tsconfig.json"
然后运行ESLint,看看它还抱怨什么


这基本上就是我在一个遇到类似问题的项目中所做的。可能还有其他几种方法可以达到同样的效果。

但是test.tsconfig.json会超越项目tsconfig.json,对吗?或者我们可以两者兼得?没错。TypeScript默认使用
tsconfig.json
,而TypeScript eslint使用设置为
project
的任何文件。聪明。顺便问一下,为什么要重复排除键?将其命名为tsconfig.eslint.conf不是更好吗?是否有命名约定?请参阅我的更新答案。我找不到任何关于命名约定的信息,VSCode和其他IDE只将
tsconfig.json
识别为TS配置文件。无论如何,提供了两个可选名称的示例:
configs/base.json
tsconfig.nostrictnull.json
(虽然都是非常规的)。所以在monorepo中,我必须为每个项目创建2个tsconfig,这样我就可以进行测试了?