今天早上,我重新启动了计算机并打开了 Visual Studio Code,并收到了这个我以前从未遇到过的错误:
我没有更改项目中的任何代码(即 git status
为空)。我不确定这是否从今天开始,或者我只是从未注意到这些文件并且它已经发生了一段时间。但我确信这些错误在 5 天前并未显示,而且错误代码出现的时间比这还要长。这是代码:
} catch (e) {
if (typeof e === "string") {
throw new Error(
`...: ${e}`
);
} else {
e.message = `... ${e.message}`;
throw e;
}
}
如果我运行tsc
或eslint
,都不会提示这个错误。我希望 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 Code 报告与我的 tsc 和 eslint 相同的错误 - 不多也不少?
最佳答案
您的 vs code 显然使用了比 package.json 更新版本的 typescript,因此它使用了一个新选项,即 catch block 中的错误被视为 unknown
而不是任何
。要解决此问题,请查看 VS Code 窗口的右下角,您应该会看到 TypeScript 版本,如下所示:
点击版本号,屏幕顶部会出现一个下拉列表
点击“选择 Typescript 版本”进行更改。
通常,vs code 能够确定您的工作空间的版本,并将其列为可能的版本。如果由于某种原因不能,或者您想使用不同的版本,您可以告诉 typescript 如何处理“typescript.tsdk”设置。有关如何设置的信息,请参阅此页面:https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript
关于typescript - Visual Studio 会提示 typescript 错误,而 tsc 和 eslint 则不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69136051/