在 VSCode 中开发 TypeScript
安装 Node.js
安装 ts-node 和 Typescript
-
安装Typescript
npm install -g typescript
-
安装 ts-node. 安装后能直接运行
.ts
文件, 不需要通过tsc
编译.npm install -g ts-node
-
打印版本信息测试安装.
tsc --version ts-node --version
创建测试工程
- 创建测试目录并使用
code
打开mkdir HelloWorld cd HelloWorld code .
- 创建测试文件
helloworld.ts
, 并在文件中添加如下内容let message: string = 'Hello World'; console.log(message);
运行和调试
- 点击左侧活动面板中的"运行和调试面板", 选择"create a launch.json file", 选择 Node.js “环境”. 或通过菜单"Run | Open Configurations" 同样可以完成此步骤.
- 打开
.vscode/launch.json
, 将文件内容替换如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
// 运行当前文件. 如果是固定某个文件, 请替换掉${file}
"command": "ts-node ${file}",
"name": "Launch Program",
"request": "launch",
"type": "node-terminal"
}
]
}
- 编辑器切换到
helloworld.ts
文件, 然后点击 “运行与调试面板” 上的 启动调试 按钮, 或使用F5
快捷键启动调试.
- 启动调试执行完成后, 在终端窗口(TERMINAL)显示如下内容, 同时在调试控制台(DEBUG CONSOLE)中打印 “hello world”.