Recently, I embarked on a new project utilizing the ASP.NET-MVC framework. For this particular project, I decided to opt for TypeScript over JavaScript. While Visual Studio provides excellent support for TypeScript, I encountered some compatibility issues when integrating it with .cshtml razor files. Although I could define and utilize classes in my .ts file and call them within the .cshtml file, I faced challenges when passing parameters to objects in the .cshtml file where TypeSafety seemed to be disregarded.
.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();
In the above code snippet, I unintentionally passed a string to the constructor despite specifying that only numbers should be accepted as parameters. This caused TypeSafety to be overlooked, leading to the execution of TypeScript/JavaScript without error prompts.
Considering both file types are developed by Microsoft, I found it slightly surprising that they do not seamlessly integrate with each other. Nevertheless, while this issue is not insurmountable and allows me to continue leveraging Object Oriented Programming principles, I am interested to hear from others who may have encountered similar challenges and seek input or insights on this matter.