Source Maps: --sourceMap and --inlineSourceMap
What you'll learn: How to enable debugging of your TypeScript code in browsers and Node.js by creating source maps that link the compiled JavaScript back to your original TypeScript files.
The Problem: Debugging Compiled Code
When TypeScript compiles to JavaScript, the resulting .js files often look very different from your original .ts files. If an error occurs or you need to set a breakpoint, your debugger shows you the compiled JavaScript—not the TypeScript you actually wrote. This makes debugging confusing and frustrating.
What Are Source Maps?
Think of a source map like a translation dictionary. It's a special file (.js.map) that tells your browser or Node.js debugger: "Line 47 in the compiled JavaScript actually corresponds to line 23 in the original TypeScript file." This lets you debug using your actual TypeScript code, even though JavaScript is what's running.
The --sourceMap Flag
Add --sourceMap to your compilation command or tsconfig.json:
{
"compilerOptions": {
"sourceMap": true
}
}
This generates a separate .js.map file alongside each compiled .js file. Your debugger automatically loads these maps, showing you TypeScript line numbers and variable names.
The --inlineSourceMap Alternative
Instead of separate .map files, --inlineSourceMap embeds the source map directly inside the .js file as a base64-encoded comment. This keeps everything in one file but makes the JavaScript file larger.
{
"compilerOptions": {
"inlineSourceMap": true
}
}
Use --sourceMap for production builds (you can exclude .map files from deployment). Use --inlineSourceMap for simpler local development setups.
Key Takeaway: Enable source maps with --sourceMap or --inlineSourceMap to debug your original TypeScript code instead of the compiled JavaScript, making development and troubleshooting dramatically easier.