node.js - tsconfig 的路径参数和 ESLint

标签 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"]
}

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/

相关文章:

javascript - 从 getInitialProps() 获取请求数据

node.js - fs watch方法的区别

linux - 如何在运行 Node.js 的 Raspberry Pi (Debian) 上解决 LIBUSB_ERROR_BUSY

javascript - 迭代按钮点击 Angular 5 上的 api 数据

javascript - 如何用 reactjs 修复 "Expected to return a value in arrow function"?

node.js - 如何在 VS Code 中使用两个不同的 .eslintrc.json?

javascript - NodeJS 中的集群

javascript - 如何触发一次警报/swal 函数而不重复触发?

bnf - 是否有正式的(最好是 BNF) typescript js 语言语法(或只有 typescript 子集)?

javascript - 如何指示 ESLint 忽略项目中的规则集?