简体   繁体   English

eslint 在声明文件上抛出 import/no-unresolved 错误

[英]eslint throwing import/no-unresolved error on declaration files

Eslint is throwing an import error: import/no-unresolved when a typescript declaration files is imported. Eslint 抛出一个导入错误:import/no-unresolved when a typescript 声明文件被导入。 I am using lerna for monorepo.我正在将 lerna 用于 monorepo。 Relevant declaration file import.相关声明文件导入。 Below imported file is app.d.ts file.下面导入的文件是 app.d.ts 文件。

I can add "import/ignore": [".d.ts"] in the below config, which solves the error but I am looking for a solution other than this.我可以在下面的配置中添加 "import/ignore": [".d.ts"] ,这可以解决错误,但我正在寻找除此之外的解决方案。

// types
import { Theme } from '@typings/app';

My.eslintrc.js config file: My.eslintrc.js 配置文件:

module.exports = {
  parser: "@typescript-eslint/parser",
  extends: [
    "airbnb",
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/react"
  ],
  plugins: ["react", "jsx-a11y", "react-hooks", "sprinklr"],
  env: {
    browser: true,
    node: true,
    es6: true
  },
  globals: {
    define: true,
    require: true
  },
  rules: {
    strict: [2, "never"],
    "no-multi-spaces": 0,
    "spaced-comment": 0,
    "no-multi-assign": 0,
    //Import rules
    "import/extensions": [
      2,
      "ignorePackages",
      {
        js: "never",
        jsx: "never",
        mjs: "never",
        ts: "never",
        tsx: "never"
      }
    ],
    "import/no-unresolved": 2,
    "import/no-extraneous-dependencies": 0,
    "import/no-named-as-default-member": 0,
    "import/prefer-default-export": 0,
    //Keeping below till idea supports these codestyles
    "object-curly-spacing": 0,

    //ts related rules
    "indent": 0,
    "@typescript-eslint/indent": 0,
    "@typescript-eslint/explicit-member-accessibility": 0,
    "@typescript-eslint/camelcase": 0,
    "import/no-webpack-loader-syntax": 0,
    "@typescript-eslint/prefer-interface": 0,
    "import/no-cycle": 1,
    "react/prop-types": 0,
    "@typescript-eslint/ban-ts-ignore": 1, //Remove after ts adoption increases
    "@typescript-eslint/explicit-function-return-type": [1, {
      "allowTypedFunctionExpressions": true
    }],
    "@typescript-eslint/no-this-alias": 0,
    "sprinklr/no-logic-i18n-template-literal": 2,
  },
  parserOptions: {
    sourceType: "module",
    ecmaVersion: 6,
    ecmaFeatures: {
      globalReturn: true,
      jsx: true
    },
    allowImportExportEverywhere: true
  },
  settings: {
    "import/extensions": [".js", ".jsx", ".mjs", ".ts", ".tsx"],
    "import/resolver": {
      webpack: {
        config: {
          resolve: {
            extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
          },
        },
      },
    },
    "react": {
      version: "detect",
    },
  },
  overrides: [
    {
      files: ["**/*.spec.*", "**/*.test.*", "**/*.d.ts"],
      env: {
        jest: true
      },
      globals: {
        // Enzyme globals
        mount: true,
        shallow: true,
        render: true
      },
      rules: {
        "@typescript-eslint/no-var-requires": 0,
        "@typescript-eslint/no-empty-function": 0,
      }
    }
  ]
};

You need more configs for typescript. typescript 需要更多配置。 the solution is as shown.解决方案如图所示。

extends: [
    "airbnb",
    "plugin:import/typescript", // this is needed because airbnb uses eslint-plugin-import
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/@typescript-eslint", // not related to this problem but it helps
    "prettier/react"
]

to improve your .eslintrc改进你的.eslintrc

...,
// parser: "@typescript-eslint/parser", -- not needed, "plugin:@typescript-eslint/recommended" has added
extends: [
  "airbnb",
  "plugin:import/typescript", // added
  "plugin:@typescript-eslint/recommended",
  "prettier",
  "prettier/@typescript-eslint" // added
  "prettier/react"
],
plugins: [
  // "react", -- not needed, airbnb has covered
  // "jsx-a11y", -- not needed, airbnb has covered
  // "react-hooks", -- not needed, airbnb has covered
  "sprinklr"
],
env: {
  browser: true,
  // node: true, -- not needed, airbnb has covered
  // es6: true, -- not needed, airbnb has covered
  amd: true // add define and require
},
globals: {
  // define: true, -- not needed, use amd env
  // require: true -- not needed, use amd env
},
...,
parserOptions: {
  // sourceType: "module", -- not needed, airbnb has covered
  // ecmaVersion: 6, -- not needed, airbnb has covered
  // ecmaFeatures: { -- not needed, airbnb has covered
  //   globalReturn: true, -- not needed, airbnb has covered
  //   jsx: true -- not needed, airbnb has covered
  // },
  allowImportExportEverywhere: true
},
...,
settings: {
  ...,
  // "react": { -- not needed, airbnb has covered
  //   version: "detect", -- not needed, airbnb has covered
  // },
  ...,
},

in addition, you may need to look what "plugin:import/typescript" has set here: https://github.com/benmosher/eslint-plugin-import/blob/master/config/typescript.js此外,您可能需要查看“plugin:import/typescript”在此处设置的内容: https://github.com/benmosher/eslint-plugin-import/blob/master/config/typescript.js

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

相关问题 如何在 ESLint 中为导入文件解析“import/no-unresolved”! 对于 javascript 和 nodejs - How to resolve "import/no-unresolved" in ESLint for import files! for javascript and nodejs 如何在Webpack 4中使用eslint的安装来解决导入/无法解决的问题 - how to resolve import/no-unresolved with setup of eslint in webpack 4 如何从导入“react”中删除未解决的 ESlint 错误 - How to remove ESlint error no-unresolved from importing 'react' 带有 Eslint 的 Vite.js 上的 Vue 3 — 无法解析模块 eslint 的路径(导入/未解决) - Vue 3 on Vite.js with Eslint — Unable to resolve path to module eslint(import/no-unresolved) 规则 'import/no-unresolved' 的生成的故事条目.js 错误 - generated-stories-entry.js error for rule 'import/no-unresolved' html 和模板文件中的 ESLint 抛出错误 - ESLint throwing error in html and template files @babel/eslint-parser 在 vue 文件上抛出错误 - @babel/eslint-parser throwing error on vue files 为 VsCode 配置 eslint 以支持类型声明文件? - Configuring eslint for VsCode to support type declaration files? 类声明ECMAScript 6后分号的ESLint错误 - ESLint error on semicolon after class declaration ECMAScript 6 样式化组件导入和 eslint 出错 - Error with styled component import and eslint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM