使用 TypeScript 为 ESLint 创建 9.x.x 配置时,我开始收到错误:
Error: Error while loading rule '@typescript-eslint/dot-notation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser. Parser: typescript-eslint/parser`
同时,
eslint.config.js
看起来像这样:
export default tseslint.config(
{ ignores: ["dist", "assets", "build", "node_modules", "public", "vite.config.ts"] },
{
extends: [
js.configs.recommended,
...tseslint.configs.recommended,
eslintConfigPrettier
],
settings: {
"import/resolver": {
typescript: {
project: "./tsconfig.json",
},
},
},
files: ["**/*.{ts,tsx}"],
languageOptions: {
sourceType: "module",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: "latest",
project: ["./tsconfig.json", "tsconfig.node.json", "tsconfig.app.json"], // <- Here it is
},
globals: globals.browser,
},
plugins: {
"react-hooks": reactHooks,
},
rules: {
...reactHooks.configs.recommended.rules,
"@typescript-eslint/dot-notation": "error",
}
}
);
parserOptions.project
属性已被注册,但错误仍然存在。
原来不是
parseOptions.project
,而是没有tsconfigRootDir: __dirname
添加后
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// ...
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: "latest",
project: ["./tsconfig.json", "tsconfig.node.json", "tsconfig.app.json"],
tsconfigRootDir: __dirname,
}
// ...
一切顺利