Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes it possible to debug typescript in chrome?

What makes it possible to debug typescript in chrome when browser understands only Javascript? I always debug my typescript files in my angular project created using angular CLI from chrome developers tool, however I don't know the reason how we are able to debug typescript files. Can someone explain it to me?

like image 806
Jyoti Prasad Pal Avatar asked Aug 20 '17 16:08

Jyoti Prasad Pal


People also ask

Is it possible to debug any TypeScript file?

TypeScript is great for writing client-side code as well as Node. js applications and you can debug client-side source code with the built-in Edge and Chrome debugger.

Can Chrome run TypeScript?

You cannot use Typescript in Chrome. Typescript is superset of JS and you must first compile . TS file to . JS file.

Can you debug in Chrome?

The Chrome Web Inspector and Debugger are conveniently built-in with Chrome. You can launch it by hitting F12 while in your browser or by right clicking on a web page and selecting the Inspect menu item. The images below show a few different views that you'll see in the Chrome DevTools browser.


1 Answers

Angular CLI uses webpack. When webpack transpiles your TS to JS, it can be be configured (and is by default) to generate source maps as well. This is how Chrome is able to tie the javascript code back to typescript for debugging.

Example tsconfig.json generated by angular CLI:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true, <--- this right here
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}
like image 86
AJ X. Avatar answered Sep 30 '22 20:09

AJ X.