Using TypeScript with an inline server-side `<script>` block and ASP.Net

asp.net, c#, server-side, typescript

Solution

The short answer is no.

You could write a TypeScript file and paste the compiled JavaScript in, but that's as close as you'll get. One practical problem would be that if the compiler transformed your TypeScript into JavaScript, you would lose your TypeScript code. This is why you have a TypeScript file and a JavaScript file.

What's the compelling reason to put the script inline rather than referencing an external script - maybe there is another solution to your problem that doesn't require inline scripts?

Problem

I want to be able to have inline TypeScript in an ASPX (or Razor) page that is converted to Javascript when the page compiles. So: ``` <script type="text/typescript" runat="server"> ... </script> ``` Becomes... ``` <script type="text/javascript"> ... </script> ``` It should happen at the same point that `@` or `<% %>` blocks are converted. This should be possible at run time with some kind of page post-processing, but that won't generate exceptions at compile time - I want to find errors in the script at the same time as any C# code. Ideally TypeScript intellisense and the like should work in the inline `<script>` block, which makes me think that this should be a VS2012 extension. Is there any way to do this?

Original source