Basic TypeScript Compiler Options

Understand the core options for the TypeScript compiler (tsconfig.json) and how to configure them for your project.


TypeScript Source Maps Explained (For Beginners)

'sourceMap': Enabling Source Map Generation

When you write TypeScript code, you usually don't directly run that code in a browser. Instead, you compile it into JavaScript. Browsers understand JavaScript, not TypeScript. This compilation process can make debugging tricky.

Imagine your TypeScript code has an error on line 10. When compiled to JavaScript, that same error might appear on line 50. It's hard to trace the error back to the original TypeScript code. That's where source maps come in.

A source map is a special file that creates a mapping between your compiled JavaScript code and your original TypeScript code. It tells the browser: "This line of JavaScript comes from this line in this TypeScript file."

The 'sourceMap' option in your TypeScript configuration file (tsconfig.json) tells the TypeScript compiler whether or not to create these source map files during the compilation process. Setting it to true enables source map generation.

// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",       // Specify ECMAScript target version
    "module": "commonjs",   // Specify module code generation
    "sourceMap": true,      // Enable sourcemap generation
    "outDir": "./dist"      // Redirect output structure to the directory
  },
  "include": [
    "src/**/*"            // Specify source files
  ]
} 

In the example tsconfig.json above, the crucial line is: "sourceMap": true. This is what tells the TypeScript compiler to generate source maps.

Exploring the 'sourceMap' Option and Generating Source Maps

Let's break down how to use the sourceMap option and what actually happens.

  1. Create a tsconfig.json File: If you don't have one already, create a file named tsconfig.json in your project's root directory. This file configures the TypeScript compiler. Copy the example above as a starting point.
  2. Set "sourceMap": true: Make sure the "sourceMap" option inside the compilerOptions section of your tsconfig.json file is set to true.
  3. Write TypeScript Code: Create a TypeScript file (e.g., src/index.ts) with some code. For example:
  4. // src/index.ts
    function greet(name: string): string {
      return "Hello, " + name + "!";
    }
    
    const myName: string = "TypeScript Beginner";
    const greetingMessage: string = greet(myName);
    
    console.log(greetingMessage); 
  5. Compile Your TypeScript: Open your terminal, navigate to your project's root directory (the same directory as tsconfig.json), and run the TypeScript compiler:
  6. tsc

    If you have TypeScript installed globally, you can run tsc directly. If not, you might need to use npx tsc if you have TypeScript installed as a project dependency (using npm or yarn). If that doesn't work, double check that TypeScript is installed correctly. The command tsc -v should show you the TypeScript version.

  7. Examine the Output: After compilation, you'll likely see two files in your output directory (specified by outDir in your tsconfig.json - in our example, ./dist):
    • dist/index.js: The compiled JavaScript code.
    • dist/index.js.map: The source map file. This file contains the mapping information.
  8. Debugging in the Browser: Open the HTML file that uses the compiled JavaScript file in your browser (see example below). When you open the browser's developer tools (usually by pressing F12), the browser will automatically detect the source map file and use it. This means you'll be able to set breakpoints and step through your original TypeScript code, even though the browser is actually running the JavaScript code. You can directly see the TypeScript code and debug it as if it were running directly in the browser.
  9. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>TypeScript Example</title>
    </head>
    <body>
        <script src="dist/index.js"></script>
    </body>
    </html> 

Important Note: Source maps are primarily useful during development. You generally don't want to include them in your production code because they expose your original source code. You can disable source map generation (set "sourceMap": false) for production builds.

By using source maps, you can significantly improve your TypeScript debugging workflow and make it much easier to find and fix errors in your code.