简体   繁体   English

带有子文件夹的 typescript 项目中的 eslint 配置

[英]eslint configuration in a typescript project with subfolders

I am using vscode, I have a project with a directory structure:我正在使用 vscode,我有一个目录结构的项目:

'App' directory --> contains 2 subfolders 'frontend' and 'backend', both have eslint configs. 'App' 目录 --> 包含 2 个子文件夹 'frontend' 和 'backend',两者都有 eslint 配置。

In the frontend folder I have:在前端文件夹中,我有:

eslintrc.json : eslintrc.json

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "project": "./tsconfig.json"
  },
  "plugins": ["@typescript-eslint", "unused-imports"],
  // rules...
}

tsconfig.json : tsconfig.json

 {
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/components/*": ["src/components/*"],
      "@/config/*": ["src/config/*"],
      "@/hooks/*": ["src/hooks/*"],
      "@/utils/*": ["src/utils/*"],
      "@/store/*": ["src/store/*"],
      "@/styles/*": ["src/styles/*"]
    },
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "noErrorTruncation": true
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"],
  "exclude": ["node_modules"]
}

whenever I import using aliases for example "@/hooks/useToggle"每当我使用别名导入时,例如"@/hooks/useToggle"

I get an eslint error我收到一个eslint错误

Unable to resolve path to module '@/hooks/useToggle'

the error goes away when I open the frontend folder alone in vscode.当我在 vscode 中单独打开frontend文件夹时,错误消失了。

Could you please help?能否请你帮忙?

This problem can be solved by adding eslint.workingDirectories config in vscode workspace settings to point to all top-level subfolders that required linting.这个问题可以通过在 vscode 工作空间设置中添加eslint.workingDirectories配置来指向所有需要 linting 的顶级子文件夹来解决。

In your case, try adding this to .vscode/settings.json在您的情况下,请尝试将其添加到.vscode/settings.json

{
  ...

  "eslint.workingDirectories": ["frontend","backend"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM