0

Right now we're using server-side code blocks in .aspx pages to generate JavaScript variables to be used by client side scripting;

Page.aspx

<html>
   <head>
      <script type="text/javascript">
          <%=GenerateJavascriptVars()%>
          // session variables, database values, etc.
          // use the variables
      </script>
   </head>
   <body>
      <form></form>
   </body>
</html>

This is fine, but I'm looking into TypeScript and there doesn't seem to be a good way to go about mimicking this setup, since TypeScript needs to know the type of these variables at compile time (so that the JavaScript files can be generated). I know about .d.ts files, but it doesn't seem like there is any benefit in defining things in two places, at that point why not just use native JavaScript?

I guess what I'm asking is, is TypeScript viable with our current setup? I'm beginning to think that the refactor may outweigh the benefits of strongly typed JavaScript. Is there something that I'm missing?

6
  • No, your approach doesn't lend itself to Typescript which as you point out needs to be transpiled from *.ts files, so you can't execute server-side script there unless you get the server side to generate the entire .ts file. I don't really like this approach anyway. You should create a web service which provides your Session Variables/DB Values/etc. and use a framework to ensure the data is loaded as a requirement (e.g. Angular) Commented Nov 20, 2014 at 13:16
  • @RGraham I'm not a fan of this either, unfortunately, every page in our application is formatted this way. With the web service, the .aspx page is served up from IIS, then javascript makes a call from the client to the webservice to get the variables? Commented Nov 20, 2014 at 13:22
  • 1
    Exactly. But tbh, sounds to me like adding TypeScript to this isn't going to be a huge benefit. Use it for the next project. Commented Nov 20, 2014 at 13:23
  • I think you might be right. Thanks for the second look. Commented Nov 20, 2014 at 13:25
  • Possible duplicate without useful answer: stackoverflow.com/questions/13496911/… Commented Nov 20, 2014 at 15:59

1 Answer 1

1

I guess what I'm asking is, is TypeScript viable with our current setup? I'm beginning to think that the refactor may outweigh the benefits of strongly typed JavaScript. Is there something that I'm missing?

The benefits are there. As you know about .d.ts files are quite simple:

declare var sessionVarOne{}
declare var sessionVarTwo{}

But a more recommended less duplicate effort approach is to use codegen to generate these for you e.g. assume you will be exporting a C# variable AwesomeDTO Something you would codegen:

interface AwesomeDTO{
   awesomeDTOMember : string;
}

declare var Something:AwesomeDTO;

For codegen look at something like http://type.litesolutions.net/ (we have our own solution highly customized for us e.g. generates Angular HTTP Services as well).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.