Source maps
What a .map is
A map file records which line of emitted JavaScript came from which line of TypeScript. The browser reads it and shows the original sources in the debugger.
Without maps you debug generated code. With maps you set breakpoints and read variables in the code you actually wrote.
function total(items: number[]): number { return items.reduce((a, b) => a + b, 0); } console.log(total([1, 2, 3]));
6
sourceMap:true
One setting enables map output for the whole project. Each emitted JavaScript file gets a matching map file beside it.
The command below builds once with maps, which is ideal while wiring up the debugger for the first time.
tsc --sourceMap
The compiler prints nothing on a clean build, and the folder now holds app.js with app.js.map beside it.
Debug TS directly
Open the browser developer tools and look for the TypeScript sources under the sources panel. Breakpoints set there pause on the original lines.
Step over, step into, and hover values exactly like plain JavaScript. The map layer translates every frame back to TypeScript for you.
{ "compilerOptions": { "sourceMap": true, "target": "ES2020" } }
Do not ship maps
Maps are a development aid, not a production artifact. Published maps expose original sources to every visitor and double the deployed files.
Keep maps on for local debugging and off for release builds. The release config overrides the flag without touching shared settings.
{ "compilerOptions": { "sourceMap": false, "target": "ES2020" } }
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a
.mapfile record?Answer
Which line of emitted JavaScript came from which source line
The browser reads the map and shows the original TypeScript in the debugger.
-
What does
"sourceMap": truedo?Answer
It writes a map file beside every emitted JavaScript file
Each emitted file gets a matching map, and the file ends with a pointer to it.
-
Where does a browser show the TypeScript of a mapped build?
Answer
In the Sources panel, under the original files
The map lets the debugger list the sources and set breakpoints on their lines.
-
Why keep maps out of a release build?
Answer
They expose the original sources and double the deployed files
A map is a development aid, and shipping it publishes the code it maps.
-
What does this program print?
function total(items: number[]): number { return items.reduce((a, b) => a + b, 0); } console.log(total([1, 2, 3]));Answer
6
The reduce adds the three numbers with a zero start and returns 6.
-
What does this program print?
function total(items: number[]): number { return items.reduce((a, b) => a + b, 0); } console.log(total([]));Answer
0
The initial value 0 is returned when the array is empty.
-
What does this program print?
console.log([1, 2].map((n) => n * 2).join(","));Answer
2,4
The map doubles every element, and join puts a comma between them.
-
What does this program print?
console.log("app.ts".length);Answer
6
The string has six characters: a, p, p, the dot, t, and s.
-
What does the compiler say about this program?
function total(items: number[]): number { return items.reduce((a, b) => a + b, 0); } console.log(total(["a"]));Answer
error TS2322: Type 'string' is not assignable to type 'number'.
The array is checked element by element against number, and a string does not fit.
-
What does the compiler say about this program?
function total(items: number[]): number { return items.reduce((a, b) => a + b, 0); } console.log(total());Answer
error TS2554: Expected 1 arguments, but got 0.
The function needs one argument, and the call gives none.
Article author: Arthur Isaev