# Debugging TypeScript in the browser

Source: https://tpiros.dev/blog/debugging-typescript-in-the-browser

TypeScript, being a superset of JavaScript, makes it convenient to write applications with support for data types, generics and more. But to actually run TypeScript, we need to transpile it to JavaScript. And once we've got that JavaScript, we can't see the original TypeScript source code in the browser.

There are ways around this. Here we'll look at how to debug TypeScript in web browsers using source maps.

## Source maps

Source maps are special files created for JavaScript or CSS files. The idea is simple. Imagine a bunch of JavaScript files (or SASS-generated CSS files) that get minified and concatenated into a single file. There's no way to debug using those files in the browser.

A source map lets us map our minified (or otherwise modified) files back to their sources, so we can view the original code in the browser.

On top of seeing the original source, we can also set breakpoints and step through our application.

## Generate source maps

To generate a source map, we'll use the `tsc` transpiler with the `--sourceMaps true` option. Let's write some TypeScript first:

```typescript
interface IWarrior {
  name: string;
  health: number;
  fight: Function;
}

let myWarrior: IWarrior = {
  name: 'John the Nomad',
  health: 100,
  fight: function () {
    return `"${this.name}" attack!`;
  },
};

console.log(myWarrior.fight());
```

Now transpile with `tsc --sourceMaps true`.

The result is a JavaScript file with this line appended at the end:

```typescript
//# sourceMappingURL=app.js.map
```

We also get a map file with the following content:

```json
{
  "version": 3,
  "file": "app.js",
  "sourceRoot": "",
  "sources": ["app.ts"],
  "names": [],
  "mappings": "AAMA,IAAI,SAAS,GAAa;IACxB,IAAI,EAAE,gBAAgB;IACtB,MAAM,EAAE,GAAG;IACX,KAAK,EAAE;QACL,MAAM,CAAC,OAAI,IAAI,CAAC,IAAI,eAAW,CAAC;IAClC,CAAC;CACF,CAAC;AAEF,OAAO,CAAC,GAAG,CACT,SAAS,CAAC,KAAK,EAAE,CAClB,CAAC"
}
```

### Understanding the source map

The source map is a JSON document with a few properties:

- **version** - the [source map standard](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#)'s version
- **file** - the transpiled filename
- **sourceRoot** - an optional source root, useful for relocating source files on a server or removing repeated values in the "sources" entry
- **sources** - the original TypeScript source file
- **names** - a list of symbol names used by the mappings entry
- **mappings** - encoded mapping data

So we've got the generated filename, the source, and the mapping between the two using Base 64 VLQ (Variable-Length Quantity) encoding. This encoding stores letters against numbers: for example `0 - A`, `1 - C` and so on. (There are some [online Base64 VLQ (de)coders](http://www.murzwin.com/base64vlq.html) available.)

### Interpret Base 64 VLQ

We can use a [source map visualisation tool](http://sokra.github.io/source-map-visualization/#base64,dmFyIG15V2FycmlvciA9IHsKICAgIG5hbWU6ICdKb2huIHRoZSBOb21hZCcsCiAgICBoZWFsdGg6IDEwMCwKICAgIGZpZ2h0OiBmdW5jdGlvbiAoKSB7CiAgICAgICAgcmV0dXJuICJcIiIgKyB0aGlzLm5hbWUgKyAiXCIgYXR0YWNrISI7CiAgICB9Cn07CmNvbnNvbGUubG9nKG15V2Fycmlvci5maWdodCgpKTsKLy8jIHNvdXJjZU1hcHBpbmdVUkw9YXBwLmpzLm1hcA==,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiYXBwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQU1BLElBQUksU0FBUyxHQUFhO0lBQ3hCLElBQUksRUFBRSxnQkFBZ0I7SUFDdEIsTUFBTSxFQUFFLEdBQUc7SUFDWCxLQUFLLEVBQUU7UUFDTCxNQUFNLENBQUMsT0FBSSxJQUFJLENBQUMsSUFBSSxlQUFXLENBQUM7SUFDbEMsQ0FBQztDQUNGLENBQUM7QUFFRixPQUFPLENBQUMsR0FBRyxDQUNULFNBQVMsQ0FBQyxLQUFLLEVBQUUsQ0FDbEIsQ0FBQyJ9,aW50ZXJmYWNlIElXYXJyaW9yIHsKICBuYW1lOiBzdHJpbmc7CiAgaGVhbHRoOiBudW1iZXI7CiAgZmlnaHQ6IEZ1bmN0aW9uOwp9CgpsZXQgbXlXYXJyaW9yOiBJV2FycmlvciA9IHsKICBuYW1lOiAnSm9obiB0aGUgTm9tYWQnLAogIGhlYWx0aDogMTAwLAogIGZpZ2h0OiBmdW5jdGlvbigpIHsKICAgIHJldHVybiBgIiR7dGhpcy5uYW1lfSIgYXR0YWNrIWA7CiAgfQp9OwoKY29uc29sZS5sb2coCiAgbXlXYXJyaW9yLmZpZ2h0KCkKKTsK) to explore how the mapping works. It's excellent at showing how the original source (app.ts) maps to the generated file (app.js) term by term.

## Use the maps for debugging

With a source map in place, we can open the application in a browser. Under the "Sources" tab (Chrome) or "Debug" tab (Firefox/Safari), the original `.ts` file appears:

We can set breakpoints and inspect variable values during runtime.
