最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - eslint throwing importno-unresolved error on declaration files - Stack Overflow

programmeradmin0浏览0评论

Eslint is throwing an import error: import/no-unresolved when a typescript declaration files is imported. I am using lerna for monorepo. Relevant declaration file import. Below imported file is app.d.ts file.

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.

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

My .eslintrc.js config file:

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,
      }
    }
  ]
};

Eslint is throwing an import error: import/no-unresolved when a typescript declaration files is imported. I am using lerna for monorepo. Relevant declaration file import. Below imported file is app.d.ts file.

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.

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

My .eslintrc.js config file:

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,
      }
    }
  ]
};
Share Improve this question asked Dec 1, 2020 at 16:50 Shivangi KhandelwalShivangi Khandelwal 1231 gold badge2 silver badges14 bronze badges 3
  • What's sprinklr? – Lucas Vazquez Commented Mar 17, 2022 at 23:50
  • it is my company's name. :). we use our custom configs/plugins for eslint – Shivangi Khandelwal Commented Mar 21, 2022 at 10:18
  • Ahh, okay!! thanks for answering. Just asking out of curiosity, because I searched for that word as an eslint plugin and found nothing. – Lucas Vazquez Commented Mar 21, 2022 at 18:29
Add a comment  | 

2 Answers 2

Reset to default 16

You need more configs for 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

...,
// 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

发布评论

评论列表(0)

  1. 暂无评论