我一直在设置 paths
我的 tsconfig.json
中的选项文件。到目前为止一切正常。我可以运行我的测试,也可以像往常一样执行程序。我唯一的问题是,ESLint 没有找到使用 tsconfig.json
中定义的路径之一访问的模块。 .
以下是与该问题相关的所有文件:
tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"allowJs": true,
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./",
"strict": true,
"esModuleInterop": true,
"declaration": true,
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src/**/*", "test/**/*", "index.ts"]
}
tsconfig.eslint.json:
{
"extends": "./tsconfig.json",
"include": ["src/**/*", "test/**/*", "index.ts", ".eslintrc.js"]
}
.eslintrc.js:
{
root: true,
env: {
browser: true,
es6: true,
node: true
},
extends: ['plugin:@typescript-eslint/recommended', 'plugin:@typescript-eslint/recommended-requiring-type-checking'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: ['tsconfig.eslint.json'],
sourceType: 'module'
},
settings: {
'import/resolver': {
typescript: {}
}
},
plugins: ['@typescript-eslint', 'import', 'prefer-arrow'],
rules: {...}
}
我正在使用包eslint-import-resolver-typescript .
现在,如果我尝试使用路径“@/test”将文件“./src/test.ts”导入“./index.ts”,那么 ESLint 将无法解析该导入(尽管 TypeScript 可以解析它正好)。
我主要从 here 复制了我当前的解决方案,因为我认为提出该问题的人与我有同样的问题,但我的设置仍然无法正常工作。
顺便说一句,我在 NodeJS 环境中。
编辑:
我也试过使用包eslint-import-resolver-alias .这只是部分帮助。我可以摆脱“import/no-unresolved”错误,但是每当我调用导入的函数时,我都会得到“@typescript-eslint/no-unsafe-call”,因为显然 ESLint 找不到导入文件的类型因此给所有的东西类型
any
.
最佳答案
您可以尝试添加 tsconfigRootDir
给您的.eslintrc.js
?它对我有用。
parserOptions: {
tsconfigRootDir: __dirname,
}
关于node.js - tsconfig 的路径参数和 ESLint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62474451/