typescript :2.5.2
vscode:1.16.0node
源码:githubwebpack
npm install typescript --save-dev
主要是将 sourceMap
设置为true
。git
{ "compilerOptions": { "module": "commonjs", "target": "es5", "noImplicitAny": true, "outDir": "./dist", "sourceMap": true }, "include": [ "src/**/*" ] }
利用 vscode 的 tasks 自动将 ts 编译为 js。也能够使用别的方式编译,如:gulp,webpack 等。
添加文件: /.vscode/tasks.json
github
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for thedocumentation about the tasks.json format "version": "0.1.0", "command": "tsc", "isShellCommand": true, //-p 指定目录;-w watch,检测文件改变自动编译 "args": ["-p", ".","-w"], "showOutput": "always", "problemMatcher": "$tsc" }
使用快捷键 Ctrl + Shift + B
开启自动编译。web
调试时,须要配置 vscode 的 launch.json
文件。这个文件记录启动选项。
添加或编辑文件 /.vscode/launch.json
。typescript
{ "version": "0.2.0", "configurations": [ { "name": "launch", "type": "node", "request": "launch", "program": "${workspaceRoot}/dist/main.js", "args": [], "cwd": "${workspaceRoot}", "protocol": "inspector" } ] }
注意 : program
需设置为你要调试的 ts 生成的对应的 js。
假如须要调试 /src/main.ts
,则此处为 ${workspaceRoot}/dist/main.js
。npm
打开 main.ts
,在左侧添加断点,进行调试。json
源码:github
来自:Debugging TypeScript in VS Code without compiling, using ts-nodets-node
调试 ts 文件时,不会显式生成 js。假如你不想编译为 js 后 再调试,能够考虑这种方式。gulp
npm install typescript --save-dev npm install ts-node --save-dev
主要是将 sourceMap
设置为true
。es5
{ "compilerOptions": { "module": "commonjs", "target": "es5", "noImplicitAny": true, "outDir": "./dist", "sourceMap": true }, "include": [ "src/**/*" ] }
打开 DEBUG 界面,添加 配置
或者编辑 /.vscode/launch.json
。
{ "version": "0.2.0", "configurations": [ { "name": "Current TS File", "type": "node", "request": "launch", "program": "${workspaceRoot}/node_modules/ts-node/dist/_bin.js", "args": [ "${relativeFile}" ], "cwd": "${workspaceRoot}", "protocol": "inspector" } ] }
debugger
。DEBUG
后 选择 launch.json 中对应的配置,此处为Current TS File
。