我的项目结构
我的 tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": "./src",
"declaration": true,
"allowJs": true
},
"include": [
"src/**/*.ts",
],
"exclude": [
"eslint.config.mjs",
"dist/**"
]
}
我的eslint.config.mjs:
// @ts-check
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import tsParser from '@typescript-eslint/parser'
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
export default tseslint.config(
{
files: [
"src/**/*.ts"
],
ignores: [
'dist/**/*.ts',
'dist/**',
"**/*.mjs",
"eslint.config.mjs",
"**/*.js"
],
rules: {
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
},
},
...tseslint.configs.strictTypeChecked,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: "./tsconfig.json",
projectService: true,
tsconfigRootDir: __dirname,
projectFolderIgnoreList: [
"**dist**",
],
},
},
},
);
我希望 tsc 将编译后的输出放在 dist 文件夹中(我是 TypeScript 新手,所以我不知道这是否是 TS 项目的正确方法)。
我忽略了 dist 文件夹下的文件,但仍然遇到这些错误:
/Users/kainzhong/VSCode/eslintTmp/dist/src/index.d.ts
0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/index.d.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject
/Users/kainzhong/VSCode/eslintTmp/dist/src/index.js
0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/index.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject
/Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.d.ts
0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.d.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject
/Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.js
0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject
/Users/kainzhong/VSCode/eslintTmp/eslint.config.mjs
0:0 error Parsing error: /Users/kainzhong/VSCode/eslintTmp/eslint.config.mjs was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject
我不是已经忽略了这些文件吗?为什么 eslint 仍然抱怨他们?
此配置实际上并没有忽略这些文件 - 这就是问题所在。
ignores
有两种行为方式:
files
的配置块中时,它会停止 特定配置块 影响那些被忽略的文件files
时,它 告诉 ESLint 忽略这些文件将
ignores
移动到开始时自己的块:
export default tseslint.config(
ignores: [
'dist/**/*.ts',
'dist/**',
"**/*.mjs",
"eslint.config.mjs",
"**/*.js"
],
},
{
files: [
"src/**/*.ts"
],
// ...
来自 https://eslint.org/docs/latest/use/configure/configuration-files#exclusion-files-with-ignores。