在Visual Studio Code中运行TypeScript文件,并且console.log(hello),有打印输出信息。
一. vscode安装Code Runner(.run)插件

二. 在文件的根目录下安装ts-node
执行命令: npm install -g ts-node typescript
bash
npm install -g ts-node typescript
三. 运行原理
TypeScript -> JavaScript
四. 配置Visual Studio Code的settings.json新增/修改如下的配置
javascript
{
// 所有文件都在终端运行
"code-runner.runInTerminal": true,
// 运行前自动清屏,避免旧日志干扰
"code-runner.clearPreviousOutput": true,
// 运行前自动保存文件,避免用旧代码
"code-runner.saveFileBeforeRun": true,
// 指定解释器
"code-runner.executorMap": {
"typescript": "tsc $fileName && node $fileNameWithoutExt.js"
}
}
五.测试
// hello.ts文件
// 右击文件,选择Run Code 运行
TypeScript
const hello : string = "hello world"
console.log(hello);
// 输出结果
bash
[Running] tsc hello.ts && node hello.js
hello world
[Done] exited with code=0 in 0.435 seconds