Map runtime errors to TypeScript
Debugging transpiled application code
TypeScript · Const
sourceMap enables source-map generation for emitted JavaScript. Debuggers can use the generated .js.map files to show the original TypeScript source while the JavaScript runs.
Set sourceMap to true in compiler options to emit a separate source-map file alongside each generated JavaScript file. The generated JavaScript includes a mapping comment, enabling debuggers and similar tools to relate emitted code to the original TypeScript.
{
"compilerOptions": {
"sourceMap": true
}
}| Name | Type | Description |
|---|---|---|
sourceMap | boolean | Whether to emit source-map files for generated JavaScript. |
When enabled, emits .js.map or .jsx.map files next to generated JavaScript and adds a source-map comment to the JavaScript.
sourceMap is a boolean compiler option that turns on source-map generation..js.map or .jsx.map suffix.false.sourceMap for JavaScript output and declarationMap separately when declaration-file navigation also needs mappings.// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"outDir": "dist",
"sourceMap": true
}
}
// greet.ts
export function greet(name: string) {
return `Hello, ${name}`;
}Compile with tsc -p tsconfig.json; TypeScript emits JavaScript plus greet.js.map, allowing a debugger to map generated code back to greet.ts.
Debugging transpiled application code
Publishing a TypeScript library with its source files
Investigating generated JavaScript in browser or debugger tooling
sourceMap option do?sourceMap is a boolean compiler option that causes TypeScript to emit source-map files for generated JavaScript. The maps let debuggers and other tools show the original TypeScript while executing the emitted code.
Follow-up: How can maps be embedded instead? Use inlineSourceMap, which puts the map content into the emitted JavaScript rather than writing a separate .js.map file.
sourceMap produce?With sourceMap: true, each emitted .js file has a neighboring .js.map or .jsx.map file. The JavaScript also contains a source-map comment that points external tools to that map.
Follow-up: What related option maps declaration files? declarationMap emits maps for .d.ts files so navigation can lead back to the original TypeScript source.
The option is useful during debugging because it maps generated JavaScript locations to the original TypeScript source. It does not itself alter the runtime JavaScript behavior; it supplies debugging metadata.
Follow-up: How can debugger source locations be customized? sourceRoot specifies a path or URL where the debugger should locate the TypeScript files.
inlineSourceMapinlineSourcessourceRootdeclarationMapmapRoot