tsconfig 的路径参数和 ESLint

Leh*_*hks 8 node.js typescript eslint

我一直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"]
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.eslint.json:

{
    "extends": "./tsconfig.json",
    "include": ["src/**/*", "test/**/*", "index.ts", ".eslintrc.js"]
}
Run Code Online (Sandbox Code Playgroud)

.eslintrc.js:

{
    "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"]
}
Run Code Online (Sandbox Code Playgroud)

我正在使用包eslint-import-resolver-typescript

现在,如果我尝试使用路径 '@/test' 将文件 './src/test.ts' 导入到 './index.ts' 中,那么 ESLint 将无法解析该导入(尽管 TypeScript 可以解析它)正好)。

我主要是从这里复制我当前的解决方案,因为我认为提出这个问题的人和我有同样的问题,但我的设置仍然不起作用。

顺便说一下,我在 NodeJS 环境中。

编辑:我也试过使用包eslint-import-resolver-alias。这只是部分帮助。我可以摆脱 'import/no-unresolved' 错误,但是每当我调用一个导入的函数时,我都会得到 '@typescript-eslint/no-unsafe-call' 因为显然,ESLint 没有找到导入文件的类型从而为所有内容提供 type any

kor*_*ral 9

您可以尝试将其添加tsconfigRootDir到您的.eslintrc.js吗?它对我有用。

parserOptions: {
    tsconfigRootDir: __dirname,
}
Run Code Online (Sandbox Code Playgroud)


Ian*_*ink 9

对于那些使用 Angular 和 的人.eslintrc.json,我使用这个(注意“Project”中的 2 颗星

        "parserOptions": {
            "project": ["**/tsconfig.json"],
            "createDefaultProgram": true
        },
Run Code Online (Sandbox Code Playgroud)