ค้นหาเว็บไซต์

แก้ไขข้อผิดพลาด: typescript ที่มี interface ไม่ถูกต้องถูกโหลดเป็น resolver ของ ESLint


ฉันกำลังใช้ ESLint ในโปรเจกต์ของฉันและได้รับข้อผิดพลาด Resolve Error: typescript with invalid interface loaded as resolver : import-no-restricted-paths เมื่อฉันเพิ่มกฎ import/no-restricted-paths

tsconfig.json

{
    "include": [
        "./src/**/*",
        "./generated/**/*"
    ],
    "compilerOptions": {
        "resolveJsonModule": true,
        "target": "es5",
        "module": "esnext",
        "moduleResolution": "node",
        "lib": [
            "dom",
            "es6"
        ],
        "downlevelIteration": true,
        "jsx": "react",
        "declaration": false,
        "sourceMap": true,
        "baseUrl": "src",
        "outDir": "./dist/js",
        "paths": {
            "@generated/*": ["../generated/*"],
            "api": ["lib/api"],
            "api/*": ["lib/api/*"],
            "asset/*": ["lib/asset/*"],
            "base/*": ["lib/base/*"]
        },
        // Rules
        "noImplicitAny": false,
        "strictNullChecks": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "forceConsistentCasingInFileNames": true,
        "esModuleInterop": true,
        "isolatedModules": true
    }
}

.eslint.json

module.exports = {
    env: {
        browser: true
    },
    settings: {
        "import/parsers": {
            "@typescript-eslint/parser": [".ts", ".tsx"]
        },
        "import/resolver": {
            "typescript": {
                paths: "./tsconfig.json",
                alwaysTryTypes: true
            }
        },
    },
    parser: "@typescript-eslint/parser",
    parserOptions: {
        tsconfigRootDir: ".",
        sourceType: "module",
        project: "./tsconfig.json"
    },
    extends: [
        "plugin:import/recommended",
        "plugin:import/typescript"
    ],
    plugins: [
        "@typescript-eslint",
        "import"
    ],
    overrides: [
        {
            files: [
                "src/_migration/**"
            ],
            rules: {
                "import/no-restricted-paths": [
                    "error",
                    {
                        basePath: "./src",
                        zones: [
                            { target: "./_migration", from: "./", except: ['./_migration'] }
                        ],
                    },
                ]
            }
        },
    ],
    rules: {
        "import/no-unresolved": "off",
        "@typescript-eslint/typedef": [
            "error",
            {
                parameter: true
            }
        ]
    }
};

คำตอบ #1

สิ่งนี้ดูเหมือนว่าจะได้ผลสำหรับฉัน

npm install -D eslint-import-resolver-typescript

ฉันไม่อยากลบ package-lock.json เพราะฉันรู้จากประสบการณ์ว่าการทำเช่นนั้นจะทำให้เกิดปัญหากับโปรเจกต์ปัจจุบันของฉัน

คำตอบ #2

สำหรับฉัน มันก็เพียงพอแล้วที่จะรีสตาร์ทเซิร์ฟเวอร์ eslint ใน Visual Studio Code คุณสามารถทำได้โดยกด cmd+p แล้วเริ่มพิมพ์:

>eslint: restart eslint server

คำตอบ #3

เพิ่มสิ่งนี้ไปที่ .eslintrc แล้วมันก็แก้ปัญหาสำหรับฉันได้

"settings": {
  "import/resolver": {
    "node": {
      "extensions": [".js", ".jsx", ".ts", ".tsx"]
    }
  }
}

บทความที่เกี่ยวข้อง