Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get from JavaScript to TypeScript in ASP.Net MVC?

For obvious reasons we want to start with TS instead of JS in our project.

The problem we occurred are the variables who are set in the MVC Views which are set by the Model of the given View.

E.g. tes.cshtml:

@model Testmodel
<script>
  var test = {};
  var test.myProp = @Model.Testproperty;
<script>

Now in my test.ts I got an error when I try to get the test-variable because my TypeScript file doesn't know it.

Do I have a architecture miss-conception here? Or is there a trick to do that?

To be honest we have around 100 variables set and / or created in RazorViews, most likely a lot of Ressource-Variables from our resx files we would need e.g. in a java-script alert!

like image 623
Rene Koch Avatar asked Oct 29 '22 20:10

Rene Koch


1 Answers

You can create definitions file and put all your global declarations there. For example:

declare interface SampleInterface{
    myProp:string;
    myFunc(someParameter:string):void;
}
declare var test:SampleInterface;
declare var someFunc: () => number;

More info on writing declaration files here.

like image 191
Aleksey L. Avatar answered Nov 14 '22 10:11

Aleksey L.