【问题标题】:Visual Studio complains about typescript errors that tsc and eslint don'tVisual Studio 抱怨 tsc 和 eslint 没有的 typescript 错误
【发布时间】:2021-11-07 04:11:08
【问题描述】:

今天早上,我重新启动了我的电脑并打开了 Visual Studio 代码,并得到了这个我以前从未遇到过的错误:

我没有更改项目中的任何代码(即,git status 为空)。我不确定这是否从今天开始,或者我只是从未注意到这些文件并且已经发生了一段时间。但我确定这些错误在 5 天前没有出现,而且错误代码的存在时间比这更长。这是代码:

        } catch (e) {
            if (typeof e === "string") {
                throw new Error(
                    `...: ${e}`
                );
            } else {
                e.message = `... ${e.message}`;
                throw e;
            }
        }

如果我运行 tsceslint,则不会抱怨此错误。我希望 vscode 报告 tsc/eslint 会做什么,而不是决定它自己的类型检查规则。如何摆脱这些错误?

我不知道我不知道什么。我想我会附上我的设置:

用户

{
    "files.autoSave": "afterDelay",
    "explorer.confirmDelete": false,
    "security.workspace.trust.untrustedFiles": "open",
    "explorer.confirmDragAndDrop": false,
    "docker.showStartPage": false,
    "editor.fontSize": 14,
    "editor.renderWhitespace": "all",
    "[json]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "typescript.tsserver.experimental.enableProjectDiagnostics": true
}

我切换了typescript.tsserver.experimental.enableProjectDiagnostics,它对这个错误消息没有影响。

工作区

{
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter-notebook"
    },
    "notebook.cellToolbarLocation": {
        "default": "right",
        "jupyter-notebook": "left"
    },
    "python.formatting.provider": "black",
    "eslint.workingDirectories": [
    "./firebase/functions",
    ],
    "eslint.format.enable": false,
    "prettier.enable": true,
    "[typescript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "typescript.format.semicolons": "insert",
    "editor.detectIndentation": false,
    "prettier.configPath": "firebase/functions/.prettierrc.json",
}

这是我的 package.json 的审查版本:

{
  "name": "...",
  "scripts": {
    "lint": "eslint .",
    "build": "tsc --build .",
    "test": "npm run lint && npm run build && ...",
    ...
  },
  "engines": {
    "node": "12"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@firebase/testing": "^0.20.8",
    "@types/child-process-promise": "^2.2.1",
    "@types/follow-redirects": "^1.13.0",
    "@types/node-fetch": "^2.5.7",
    "@types/progress": "^2.0.5",
    "@types/request": "^2.48.7",
    "@types/uuid": "^8.3.0",
    ...
  },
  "devDependencies": {
    "@types/mocha": "^9.0.0",
    "@types/node": "^14.10.2",
    "@typescript-eslint/eslint-plugin": "^4.28.5",
    "@typescript-eslint/parser": "^4.28.5",
    "eslint": "^7.31.0",
    "firebase-functions-test": "^0.2.2",
    "lodash": "^4.17.21",
    "mocha": "^8.1.3",
    "prettier": "^2.3.2",
    "typescript": "^3.9.7"
    ...
  },
  "private": true
}

(此文件中有很多与此问题无关的怪异之处。抱歉;这是遗留问题,我正在努力改进它)。

这是我的 tsconfig.json:

{
    "compilerOptions": {
        "rootDir": ".",
    },
    "files": [],
    "references": [
        {
            "path": "./src"
        },
        {
            "path": "./test"
        },
    ]
}

注意:此文件位于./firebase/functions,而不是项目根目录。

这是./firebase/functions/src/tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "../lib",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "sourceMap": true,
    "strict": true,
    "lib": ["es2020", "dom"],
    "target": "es2019",
    "composite": true,
    "allowSyntheticDefaultImports": true
  },
  "compileOnSave": true,
  "types": ["node", ...],
  "include": ["**/*.ts"]
}

这是./firebase/functions/test/tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "../libtest",
    "strict": false,
    "composite": true,
    "sourceMap": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  },
  "include": [
    "**/*.ts"
  ]
}

如何让 Visual Studio 代码报告与我的 tsc 和 eslint 相同的错误 -- 不多也不少?

【问题讨论】:

  • 这是因为它不知道它所期望的类型。尝试在您的捕获中添加类型,看看是否有帮助!
  • @3xGuy 谢谢你的帮助。我知道这会解决它。

标签: typescript visual-studio-code eslint


【解决方案1】:

您的 vs 代码显然使用了比您的 package.json 更新的 typescript 版本,因此它使用了一个新选项,即 catch 块中的错误被视为 unknown 而不是 any。要解决此问题,请查看 vs 代码窗口的右下角,您应该会看到 typescript 版本,如下所示:

点击版本号,屏幕顶部会出现一个下拉菜单

单击“选择 Typescript 版本”进行更改。

通常,vs code 能够找出您工作区的版本,并将其列为可能性。如果由于某种原因它不能,或者如果你想使用不同的版本,你可以告诉 typescript 如何处理“typescript.tsdk”设置。有关如何设置的信息,请参阅此页面:https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript

【讨论】:

  • 谢谢你,解决了。如果您设置了typescript.tsdk 工作区设置,您能否提及如何仅找到其他版本?
猜你喜欢
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 2021-01-16
相关资源
最近更新 更多