【发布时间】: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;
}
}
如果我运行 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 代码报告与我的 tsc 和 eslint 相同的错误 -- 不多也不少?
【问题讨论】:
-
这是因为它不知道它所期望的类型。尝试在您的捕获中添加类型,看看是否有帮助!
-
@3xGuy 谢谢你的帮助。我知道这会解决它。
标签: typescript visual-studio-code eslint