sourceMap: Debugging Compiled Code
What you'll learn: How to enable source maps so you can debug your original TypeScript code instead of the compiled JavaScript.
The Problem with Compiled Code
When TypeScript compiles your .ts files into .js files, the browser or debugger only sees the JavaScript output. If an error occurs or you set a breakpoint, you're looking at compiled code—not the TypeScript you actually wrote. This makes debugging confusing and frustrating.
What Source Maps Do
A source map is a special file (.js.map) that acts like a translator between your compiled JavaScript and your original TypeScript source. Think of it like subtitles for a foreign film: the browser "speaks" JavaScript, but the source map lets it show you what's happening in the TypeScript "language" you understand.
When source maps are enabled, your browser's developer tools or your IDE's debugger will display your original TypeScript code, line numbers, and variable names—even though the browser is actually running JavaScript behind the scenes.
Enabling Source Maps
In your tsconfig.json, add or set this option:
{
"compilerOptions": {
"sourceMap": true,
"outDir": "./dist"
}
}
Now when you run tsc, TypeScript will generate both .js files and .js.map files in your output directory. Your debugger automatically uses these map files to connect the dots between compiled and source code.
When to Use Source Maps
Enable source maps during development and debugging. Some teams disable them in production builds to reduce file size and protect source code, while others keep them for better error tracking.
Key Takeaway: Setting "sourceMap": true generates .map files that let you debug your original TypeScript code instead of wrestling with compiled JavaScript output.