eslint + typescript + tsconfig 中的路径:ESLint:无法解析模块“@components/some-module”的路径。(导入/无未解析)

Tim*_*rov 9 typescript reactjs eslint typescript-eslint

我为路径(别名)添加打字稿配置:

配置文件

{
  "compilerOptions": {
    "jsx": "react",
    "baseUrl": "src",
    "paths": {
      "@components/*": ["components/*"]
    }
  },
  "exclude": [
    "node_modules",
    "public"
  ]
}

Run Code Online (Sandbox Code Playgroud)

之后,我尝试导入这样的东西:

import Header from '@components/header';
Run Code Online (Sandbox Code Playgroud)

现在我在 eslint 中遇到错误: ESLint: Unable to resolve path to module '@components/header'.(import/no-unresolved)

Tim*_*rov 29

我找到了一个解决方案:eslint-import-resolver-typescript

npm i -D eslint-plugin-import @typescript-eslint/parser eslint-import-resolver-typescript

.eslintrc.js

module.exports = {
  'settings': {
    'import/resolver': {
      'typescript': {},
    },
  },
}
Run Code Online (Sandbox Code Playgroud)

  • 正确答案。最后,在为此浪费了 2 个小时并放弃禁用警告之后,我找到了这个简单的解决方案。谢谢 (2认同)