How to debug TypeScript code
· Category: TypeScript
Short answer
Enable sourceMap: true in tsconfig.json, generate .js.map files, and configure your debugger to map breakpoints back to original .ts sources.
Steps
- Enable source maps in tsconfig.json:
"sourceMap": true - Compile your project with
tsc. - In VS Code, create a launch.json with
preLaunchTaskset to your build task. - Set breakpoints directly in
.tsfiles. - For Node.js, use
ts-node --inspector compile first and debug the output with source maps.
Tips
- Inline source maps (
inlineSourceMap) reduce file clutter but increase output size. - Ensure
outDirandrootDirare correctly mapped in the debugger configuration.
Common issues
- Missing or misaligned source maps cause breakpoints to bind to compiled JavaScript instead of TypeScript.
- Debugging transpiled async/await may step through helper code unless targeting modern ES versions.