[英]How to setup VSCode to show typescript error inline
我正在尝试将 ES6 项目迁移到 typescript。这是我第一次尝试在 NodeJS 中编写 typescript 模块。
到目前为止,它似乎在 Angular-CLI 中工作得更好。
我已经使用命令行tsc
进行了编译,但我不确定如何在代码编辑器中显示错误和智能感知?
在一个目录中,我有如下所示的两个文件。 当它编译它时,正如预期的那样抛出一个编译错误: Supplied parameters do not match any signature of cal l target.
. 这可以。
但是 VSCode 没有在编辑器中显示任何错误,即使我故意犯了一个语法错误,比如去掉一个括号,或者像这样输入错误。 如何让 VSCode 在 .ts 文件的编辑器中显示内联语法或编译器错误?
验证错误.ts
/**
* Wrapper for a particular validation error.
*/
export class ValidationError extends Error {
private type: string;
constructor(error: any) {
super(error);
Error.captureStackTrace(this, this.constructor);
this.message = error.message;
this.type = 'ValidationError';
}
}
然后我尝试编写简单的规范来测试工作流程:
验证错误.spec.ts
import { ValidationError } from './validation-error';
import * as should from 'should';
describe('Validation Error', () => {
it('should create a new instance', () => {
const instance = new ValidationError();
should.exist(instance);
});
});
我仍在努力解决这个问题——我已经将tsc
设置为在tasks.json
中自动运行这项工作:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"taskName": "tsc",
"command": "tsc",
"isShellCommand": true,
"args": ["-w", "-p", "."],
"problemMatcher": "$tsc-watch",
"echoCommand": true
}
我怀疑如果使用$tsc-watch
正确报告了错误,该错误也可能会显示在编辑器中。 当我运行任务时,我得到 output,例如:
running command$ tsc -w -p .
src/hello.ts(15,1): error TS2346: Supplied parameters do not match any signature of call target.
4:24:40 PM - Compilation complete. Watching for file changes.
但是Problems
视图中没有报告任何问题——尽管显然存在编译器问题。
从此文档页面获得$tsc-watch
设置: https://code.visualstudio.com/docs/editor/tasks
我刚刚关闭并重新启动了我的 VS Code,它开始像以前一样显示打字稿错误。
如果这不起作用,我建议重新启动您的计算机系统。
我遇到了这个问题,设法通过向 .vscode 目录中的本地 settings.json 文件添加"typescript.validate.enable": true
来解决它。 我的设置告诉我打字稿验证器已为 VS 代码全局启用,但在我添加本地设置文件之前,我无法看到内联 TS 错误。
其他答案(大多数在 2020 年之前)对我不起作用。
根据 Microsoft VS Code 存储库中的此功能请求,执行以下步骤:
"typescript.validate.enable": true,
"typescript.tsserver.experimental.enableProjectDiagnostics": true,
"include": [
"src",
"global.d.ts",
],
"exclude": [
"node_modules",
"plugins",
"public"
]
稍等片刻(或重新启动 VS Code),以便在打开的文件中看到错误列表并添加到问题面板中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.