Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript within .cshtml Razor Files

I am starting a new project using ASP.NET-MVC framework. I'd like to use TypeScript in this project in place of JavaScript. TypeScript is easily supported by Visual Studio but doesn't seem to be (fully) compatible with the .cshtml razor files. I'm able to create my classes within the .ts file and call those classes within my .cshtml file, the issue is when I pass in parameters to the object in the .cshtml file TypeSafety is ignored and the function is run as if a type were never defined.

.ts file

    export class SomeClass {

    name: number;

    constructor(public tName: number) {
        this.name = tName;
    }

    public sayName() {
        alert(this.name);
    }
}

.cshtml file

var instance = new SomeClass("Timmy");
instance.sayName();

As you can see, I am passing a string to the constructor even though I clearly defined the parameter to only accept numbers, but the TypeSafely is ignored and the TypeScript/JavaScript executes as if there is no issue.

Both file types were invented by Microsoft so I'm slightly surprised they aren't a little more friendly with each other. This isn't the end of the world, at least I am still able to use Object Oriented Programming, I'm just curious if anyone else has experienced this and can maybe give me a brief explanation.

like image 497
Colin Avatar asked Nov 04 '16 18:11

Colin


People also ask

Can you use TypeScript with C#?

TypeScript is a popular choice for programmers accustomed to other languages with static typing, such as C# and Java. TypeScript's type system offers many of the same benefits, such as better code completion, earlier detection of errors, and clearer communication between parts of your program.

How do I use TypeScript in .NET core?

Create an ASP.NET Core project. Add the NuGet package for TypeScript support. Add some TypeScript code. Run the app.

Can we use TypeScript in MVC?

In your MVC solution you will need to create a folder where all of your TypeScript files will reside. In this example I am going to call the folder “TSScripts” (You can name this folder anything you would like). I created the “TSScripts” folder at the root directory of my “TypeScriptProject” project.

What is difference between .cshtml and .HTML file?

html is strictly processed by the client, typically a browser. cshtml is the file extension that refers to the razor view engine. In addition to straight html, these files also contain C# code that is compiled on the server prior to the pages being server up to the browser..


1 Answers

TypeScript transpiler only checks and transpiles files that only contain :

  • Javascript
  • Javascript with some syntaxique sugar code added by TypeScript (static typing, generic class etc...)

CSHTML files are basically created to contain Razor/C# code and of course HTML/JavaScript/CSS.

Some developers are attempted to add a Javascript code and CSS stylesheet directly to the Cshtml files and by this is not a good practice.

JavaScript code and also CSS style should be in its own file. Then reference the file by using script (Javascript) or style (CSS) tag in your CSHTML .

Putting Javascript code directly into your view (CSHTML or just HTML) are not recommended because it break the following principe of Unobtrusive JavaScript:

Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation (source Wikipedia)

Some ASP.Net MVC developers still continue to put their Javascript code directly into their Razor views because they need to pass some data that are from the view model directly to the JavaScript code. When the Javascript code is already in the view, it is easy to pass the data without any complications. But I already said this is not good ;-).

This kind of things that breaks the Unobntrusive JavaScript principes can be avoid. All data that need to be read by JavaScript code should be stored by using data attribute in your HTML elements e.g.

<span id="mySpan" data-t-name="123456">Hello World</span>

Then in your TypeScript code just use jQuery (or vanilla javascript) to get the data you set in your CSHTML view like this:

let tName: number = int.Parse($("#mySpan").data("t-name"));
var instance = new SomeClass(tName);
instance.sayName();

After that, reference the generated js file from TypeScript into your CSHTML.

Hope it helps.

like image 162
CodeNotFound Avatar answered Sep 28 '22 05:09

CodeNotFound