解决错误:作为解析器 ESLint 加载的带有无效接口的打字稿

Posted

技术标签:

【中文标题】解决错误:作为解析器 ESLint 加载的带有无效接口的打字稿【英文标题】:Resolve Error: typescript with invalid interface loaded as resolver ESLint 【发布时间】:2021-11-25 11:34:08 【问题描述】:

我的项目中有 ESLint,当我添加规则 import/no-restricted-paths 时出现错误 Resolve Error: typescript with invalid interface loaded as resolver : 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
            
        ]
    
;

【问题讨论】:

我通过删除 lockfile 和 node_modules 并重新安装所有内容来解决此问题。 重启 vscode 对我有帮助???? 【参考方案1】:

这似乎对我有用。

npm install -D eslint-import-resolver-typescript

我不想删除package-lock.json,因为我从经验中知道这样做会给我当前的项目带来问题。

【讨论】:

以上是关于解决错误:作为解析器 ESLint 加载的带有无效接口的打字稿的主要内容,如果未能解决你的问题,请参考以下文章

带有 TypeScript 解析器/插件的 ESLint:如何包含从 tsconfig.json 中排除的 .spec.ts?

eslint 解析错误:带有异步的意外令牌函数

带有 ESLint 的 TypeScript:解析错误:关键字“枚举”是保留的 eslint

更漂亮,eslint - classProperties 解析器插件错误

错误 CGImageCreate:无效的图像大小:0 x 0 带有(选择器)窗口旋转

IOS 应用程序加载器显示捆绑错误,带有 armv7s 和 armv7