Debugging
With source maps
Debugging starts with maps enabled. The debugger then shows TypeScript lines while the runtime executes the emitted JavaScript behind the scenes.
Build once with maps before attaching the debugger. Without that step breakpoints land on generated code and variables look unfamiliar.
tsc --sourceMap
The compiler prints nothing on a clean build, and the folder now holds app.js with app.js.map beside it.
Launch config sketch
A launch config tells the editor how to start the program and where to find the maps. Keep the sketch minimal: runtime, program entry, and map support on.
Press start, set a breakpoint on a TypeScript line, and step through. The call stack shows your functions, not generated helpers.
{ "type": "node", "request": "launch", "name": "Debug app", "program": "app.js", "sourceMaps": true }
Strategic logging
When the debugger is overkill, log with purpose. Print a label plus the value at each boundary, then remove the lines once the fix lands.
Log inputs and outputs, not every step inside. Two well placed lines usually reveal which side of a boundary breaks the contract.
function discount(price: number, rate: number): number { console.log("price:", price, "rate:", rate); return price - price * rate; } console.log(discount(100, 0.2));
price: 100 rate: 0.2
80
Halve the code
For stubborn bugs, halve the suspect area. Disable half the logic, rerun, and keep the half that still fails. Repeat until one function remains.
The method feels slow but converges fast. Each step removes half the mystery with one run, so even large files shrink to a culprit in minutes.
const ids: number[] = [1, 2, 3, 4]; const even = ids.filter((n) => n % 2 === 0); console.log(even);
[ 2, 4 ]
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why build with source maps before debugging?
Answer
So breakpoints and variables map back to the TypeScript lines
Without maps the debugger shows generated JavaScript and unfamiliar names.
-
What does a launch config tell the editor?
Answer
How to start the program and where the maps are
The config names the runtime, the entry file, and the map support for the session.
-
What should strategic logging print?
Answer
A label and the values at boundaries, not every internal step
Two well placed lines show which side of a boundary breaks the contract.
-
How does halving the code find a stubborn bug?
Answer
Disable half, rerun, and keep the half that still fails
Each run removes half the mystery until one function remains.
-
What does this program print?
function discount(price: number, rate: number): number { console.log("price:", price, "rate:", rate); return price - price * rate; } console.log(discount(100, 0.2));Answer
price: 100 rate: 0.2 / 80
The inner log runs first, and the discount of 20 leaves 80.
-
What does this program print?
const ids: number[] = [1, 2, 3, 4]; const even = ids.filter((n) => n % 2 === 0); console.log(even);Answer
[ 2, 4 ]
The filter keeps the numbers whose remainder is zero, namely 2 and 4.
-
What does this program print?
console.log(10 - 10 * 0.2);Answer
8
The multiplication runs first: 10 times 0.2 is 2, and 10 minus 2 is 8.
-
What does this program print?
console.log([1, 2, 3, 4].filter((n) => n % 2 === 0).length);Answer
2
Two of the four numbers are even, so the filtered array has length 2.
-
What does the compiler say about this program?
function discount(price: number, rate: number): number { return price - price * rate; } console.log(discount("100", 0.2));Answer
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
The first parameter is a number, and the string argument does not fit it.
-
What does the compiler say about this program?
const ids: number[] = [1, 2]; ids.push("3"); console.log(ids);Answer
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
The array holds numbers, so push accepts only a number.
Article author: Arthur Isaev