📎 Webclip
Debugging TypeScript in the browser
TypeScript has to be transpiled to JavaScript before it can run in the browser, which means the browser normally shows the generated JavaScript instead of the original source. The article presents source maps as the way to connect the modified file back to the TypeScript file and make browser debugging practical.
Reading notes#
- Source maps are special files created for JavaScript or CSS that map minified or transformed output back to its source.
- With a source map, the browser can show the original source code instead of only the generated file.
- Source maps also make it easier to add breakpoints while debugging.
- The example uses
tsc --sourceMaps trueto generate both the JavaScript output and a.mapfile. - The generated JavaScript includes
//# sourceMappingURL=app.js.mapat the end. - The source map JSON includes
version,file,sourceRoot,sources,names, andmappings. - The mappings use Base 64 VLQ encoding to relate positions in the generated file to positions in the original source.
- A source map visualization tool can be used to inspect how
app.tsmaps toapp.js. - In browser developer tools, the original
.tsfile appears under the Sources tab in Chrome or the Debug panel in Firefox and Safari. - With the map loaded, breakpoints and runtime variable values are visible in the original TypeScript source.
