我正在尝试将一个ES6项目迁移到typescript。这是我第一次在NodeJS中编写typescript模块。
到目前为止,在Angular-CLI中似乎工作得更好。
我已经使用命令行tsc
进行了编译,但我不确定如何在代码编辑器中显示错误和智能感知?
在一个目录中,我有下面展示的两个文件。当它进行编译时,按预期会抛出一个编译错误:提供的参数与任何调用目标的签名都不匹配。
这很好。
但是,即使我故意制造语法错误(比如删掉括号)或类型错误,VSCode也不会在编辑器中显示任何错误。如何让VSCode在.ts文件中显示内联语法或编译器错误?
validation-error.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';
}
}
接下来我尝试编写一个简单的规范以测试工作流程:
validation-error.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);
});
});
这里有修改:
我还在继续努力 - 我已经在tasks.json
中设置了tsc
自动运行的任务:
{
// 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
正确报告错误,错误可能也会显示在编辑器中。当我运行任务时,输出如下:
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.
但是在“问题”视图中没有报告任何问题 - 尽管显然存在编译器问题。
从文档的这个页面获取了$tsc-watch
设置:https://code.visualstudio.com/docs/editor/tasks
tsconfig.json
文件? - known-as-bmf