Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 701
Keith Avatar asked Nov 21 '12 15:11

Keith


People also ask

Can we use TypeScript in MVC?

This adds TypeScript support for jQuery. The jQuery library itself is already included in the MVC project template (look under wwwroot/lib in Solution Explorer). If you are using a different template, you may need to include the jquery npm package as well.

Is TypeScript like C#?

The biggest difference between C# and typescript is that typescript has a structural type system, as opposed to C#'s nominal one. In C#, if I define a method which takes a WidgetScrubber , then only instances of that particular WidgetScrubber class can be passed to the method.

What is TypeScript used for?

TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. It is designed for the development of large applications and transpiles to JavaScript.


2 Answers

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?

like image 197
Fenton Avatar answered Sep 30 '22 18:09

Fenton


You could create a server-control that compiles the code at runtime, and caches it:

[DefaultProperty("Text")]  
[ToolboxData("<{0}:TypeScript runat=server></{0}:TypeScript>")]  
public class TypeScript : WebControl  
{  
    public string Text { get; set; }

    protected override void RenderContents(HtmlTextWriter output)  
    {  
        output.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");  
        if (!string.IsNullOrEmpty(this.ID))
        {
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);  
        }
        output.RenderBeginTag("script");
        output.Write(CompileToJavaScript(Text));
        output.RenderEndTag();
    }

    private string CompileToJavaScript(string typeScript)
    {
        // TODO: Call tsc with the code, and return the result.
    }
} 

You might be able to compile tsc.js with JScript.NET, if you care to implement the IO-layer, and weed out some JScript.Net (not JavaScript) syntax errors.

like image 30
Markus Jarderot Avatar answered Sep 30 '22 20:09

Markus Jarderot