Is there a way to debug TypeScript source in Visual Studio (instead of debugging the generated javascript)?
From the TypeScript language specifications:
TypeScript optionally provides source maps, enabling source-level debugging.
I was therefore expecting to be able to place breakpoints in ts code and be able to debug it, but it doesn't work. I didn't find any other mentions of debugging in the specs. Is there anything I should do to make this work? Perhaps the word "optionally" hints that I need to do something for it to work… Any suggestions?
Best Answer
Current Answer for VS2017 and later
Debugging Typescript directly in Visual Studio has been possible since VS2017. From the documentation:
There are also additional resources on Debugging Typescript / Asp.NET Core in Visual Studio.
It is also possible to debug typescript in Visual Studio Code:
Original Answer for prior versions of VS:
You may not be able to debug in VS, but you can in some browsers. Aaron Powell has blogged about getting breakpoints working in Chrome Canary just today: https://www.aaron-powell.com/posts/2012-10-03-typescript-source-maps/.
To summarise (very briefly) what Aaron says, you use the
-sourcemap
switch on the compiler to generate a*.js.map
file in the same directory as your source. In browsers which support source maps (Chrome Canary, and presumably recent Firefox builds, since they are a Mozilla idea), you can then debug your.ts
source just as you would normal.js
files.The blog finishes with "Hopefully either the Visual Studio or IE (or both) team also pick up Source Maps and add support for them too." - which suggests it hasn't happened yet.
Update:
With the release of TypeScript 0.8.1, Source Map debugging is now also available in Visual Studio:
https://blogs.msdn.com/b/typescript/archive/2012/11/15/announcing-typescript-0-8-1.aspx
From the announcement:
Update:
WebStorm has also added support for debugging via SourceMaps: http://blog.jetbrains.com/webide/2013/03/webstorm-6-0-released-adds-typescript-debugging-with-source-maps-fresh-ui-and-much-more/