Register     Login


Using TypeScript in LightSwitch

Oct 20

Written by:
Sunday, October 20, 2013 8:55 PM  RssIcon

As someone who is brand new to Javascript, my main problem with having to use it in the HTML Client is that it isn't checked for correctness at compile-time. Being new to the language, it's more important than ever that there be compile-time checking to aid with syntax etc. Again and again, I didn't find that there was an error until the app was being run. It's frustrating to say the least!

As some of you will know, I recently discovered TypeScript, & that it "starts with Javascript and ends with Javascript". I wondered if it was possible to use it in LightSwitch. It turns out, that the answer is yes. If I add a Typescript file in the same folder as the screen's code file (in the UserCode folder in VS 2012, and in the Screens folder in VS 2013), with a .ts extension, I can copy any code that was in the screen's .js file into the .ts file, and when I save the TypeScript file it ovewrites the screen's Javascript file. From now on, I can add code to the TypeScript file, and when save it, the translated code gets stored in the screen's .js file.

Here's an example of using TypeScript files in VS 2013:

So now I have better intellisense, compile-time checking, and strongly-types objects. I can also create classes and modules, and it all gets translated into pure Javascript. If you do decide to use TypeScript files in the way I've just described, be careful if you use the Write Code drop-down in the screen editor. Any code that gets added to the .js file will be overwritten when the TypeScript file is saved. So, either copy the code from the .js file to the .ts file, or add any screen code manually into the .ts file only. The experience isn't as smooth or automatic as it could be, so you have to weigh the extra steps against the benefits that TypeScript can provide.

Here's a link to the Introduction to Introducing TypeScript video that I watched, where Anders Hejlsberg explains how TypeScript is a typed superset of JavaScript that compiles to normal JavaScript, and can dramatically improve your productivity by enabling rich tooling experiences, all while maintaining your existing code and continuing to use the same JavaScript libraries you already love.

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Home   |   Blogs   |   Books   |   Extensions   |   Donations   |   About LightSwitch   |   About Me
Privacy Statement    |   Terms Of Use

Copyright 2011-2013 by Yann Duran