Debugging TypeScript Code: Turning Bugs into Tasks
\n\nHave you ever spent an hour searching for an error that turned out to be a simple typo in a variable name? Or caught yourself thinking that console.log is the only tool you use? Welcome to the club. Debugging is not just "catching bugs," but a whole art of understanding how your code actually works.
TypeScript adds a special charm to this story. On one hand, static typing eliminates a whole class of errors at the compilation stage. On the other hand, when the code is already compiled into JavaScript, debugging it "on the fly" becomes more difficult: you write in TS, but see compiled JS in the browser. How not to get lost in this maze?
\n\nToday I will show you real techniques for debugging TypeScript code that I use myself. No fluff — only practical tools, examples, and life hacks that will save you nerves and hours of work time.
\n\nWhy is Debugging TypeScript Different from Debugging JavaScript?
\n\nThe main feature of TypeScript is transpilation. You write code with types, generics, and interfaces, while the browser or Node.js receives pure JavaScript. This means that during debugging, you are looking at compiled code that can differ significantly from the original: variable names may be changed, generics erased, and interfaces gone entirely.
\n\nThe solution — Source Maps. These are mapping files between the original TS and the compiled JS. Without them, debugging TypeScript turns into reading tea leaves.
\n\nSetting Up Source Maps: The First Step to Transparency
\n\nTo make the browser or debugger understand which TS line corresponds to a JS line, you need to enable source map generation in tsconfig.json:
{\n "compilerOptions": {\n "sourceMap": true,\n "outDir": "./dist",\n "target": "ES2020",\n "module": "commonjs"\n },\n "include": ["src/**/*"]\n}\n\nNow, during compilation, a .map file will appear next to each JS file. The browser (Chrome, Firefox) will automatically load it, and you will be able to set breakpoints directly in the TypeScript code.
Important: in production, it's better to disable source maps or use hidden endpoints to avoid exposing source code to users. Configure this via environment variables:
\n\n// tsconfig.prod.json\n{\n "extends": "./tsconfig.json",\n "compilerOptions": {\n "sourceMap": false\n }\n}\n\nTypeScript Debugging Tools: Your Arsenal
\n\n1. VS Code — Built-in Debugger
\n\nVisual Studio Code is arguably the best environment for debugging TypeScript. The built-in debugger can read source maps and set breakpoints directly in .ts files. Here is a minimal launch.json configuration:
{\n "version": "0.2.0",\n "configurations": [\n {\n "type": "node",\n "request": "launch",\n "name": "Debug TS via ts-node",\n "runtimeArgs": ["-r", "ts-node/register"],\n "args": ["${workspaceFolder}/src/index.ts"],\n "sourceMaps": true,\n "cwd": "${workspaceFolder}",\n "protocol": "inspector"\n }\n ]\n}\n\nNow you can press F5 and immediately enter the debugger. Place red dots to the left of line numbers — they will work even if you are debugging TypeScript code with generics and decorators.
\n\nLife hack: use debugger; directly in the code — it is equivalent to setting a breakpoint but doesn't require a mouse.
2. Chrome DevTools — Frontend Debugging
\n\nFor frontend projects in TypeScript (React, Angular, Vue), Chrome DevTools is a must-have. Open the Sources tab, find your file in the webpack:// or src folder — if source maps are configured correctly, you will see the original TypeScript.
Useful features:
\n- \n
- Conditional breakpoints