I am glad to announce that, as of Q3 2014, the Telerik UI for ASP.NET AJAX suite comes with TypeScript definitions for its client-side objects. In fact, their first version is available in the Q3 2014 BETA, so you can get it and give them a spin.
Now, let’s start from the basics and I will walk you through the initial setup for using TypeScript so you can get started.
You can consider TypeScript as a superset of JavaScript. It provides types and casting to JavaScript which make writing JavaScript similar to writing C# (including providing advanced
TypeScript infers the type of the object in order to provide you with
To do this for you, however, Visual Studio needs to obtain this information from somewhere. The files that contain it are called Definition files and we prepared one for our objects to aid you. Since we are based on the MS AJAX framework, we also bring you the MS AJAX definitions, so you don’t have to look for them yourself.
Install the UI for ASP.NET AJAX suite and go to the TypeScriptDefinitions folder. It is available both in the manual installation (zip archive) and in the automated installer (
Your other option is to use the Telerik WebApplication Creation Wizard or the Telerik Upgrade Wizard as they can both copy those definitions for you. Of course, make sure to upgrade the Telerik Visual Studio Extensions to their latest version so you can get this new feature.
Let’s first note that TypeScript compiles to JavaScript. The keyword here is “compiles”, so you can only add it to WebApplication types of projects.
Now, let’s make this a nice walkthrough:
<
Import
Project
=
"$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets"
Condition
=
"Exists('$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets')"
/>
That’s it. Visual Studio (especially VS2013 Update 2 and later) will do most of the work for you.
Now, you simply write code and build your project. When you save a TypeScript file the Import directive you added a few steps back will have it compiled to JavaScript. Without it, you need to build the entire project.
You can download a project that has all of the above in place. Make sure to update the definitions with the ones from the latest version, of course :) If you are on an earlier version than VS 2013 Update 2, you will still need to install the TypeScript Tools.
Here follows a snapshot of how
Figure 4: TypeScript
Figure 5: TypeScript
Figure 6: TypeScript
TypeScript only infers the object type. This means that it does not know that for sure, nor does it perform “real” casting like C# or VB.
If you cast an object, you cannot check if it is null to detect its type. TypeScript will assume you know what you are doing and will not correct you. So, you need to check for flags like the presence of the method you wish to call rather than for its object type.
Here is a sample RadTileList OnClientTileSelecting handler. Let’s assume we wish to execute some logic for RadLiveTiles when they are about to be selected, but not for other
Example 1: TypeScript code that shows casting does not guarantee correct type and method availability.
function
tileListSelectingHandler(tileList: Telerik.Web.UI.RadTileList, args: Telerik.Web.UI.RadTileListCancelEventArgs) {
var
tile: Telerik.Web.UI.RadBaseTile = args.get_tile();
//this cast tells TypeScript that the liveTile object is of type RadLiveTile, so TypeScript trusts you
var
liveTile: Telerik.Web.UI.RadLiveTile = <Telerik.Web.UI.RadLiveTile>args.get_tile();
//this is useless in TS, types are inferred and the above line only explicitly instructs the IDE about this type, it does not check for types like C#
if
(liveTile !=
null
) {
//you need to check for methods and fields presence, rather than types and if casts are successful
if
(liveTile.get_clientTemplate) {
alert(liveTile.get_clientTemplate());
}
}
//here is some sample logic that prevents selection of specific tiles
if
(tile.get_name() ==
"someTile"
) {
args.set_cancel(
true
);
}
}
To illustrate the concept, here is how this will translate into JavaScript:
Example 2: JavaScript generated from the TypeScript in Example 1 that shows casting is not translated at all.
function
tileListSelectingHandler(tileList, args) {
var
tile = args.get_tile();
//this cast tells TypeScript that the liveTile object is of type RadLiveTile, so TypeScript trusts you
var
liveTile = args.get_tile();
//this is useless in TS, types are inferred and the above line only explicitly instructs the IDE about this type, it does not check for types like C#
if
(liveTile !=
null
) {
//you need to check for methods and fields presence, rather than types and if casts are successful
if
(liveTile.get_clientTemplate) {
alert(liveTile.get_clientTemplate());
}
}
//here is some sample logic that prevents selection of specific tiles
if
(tile.get_name() ==
"someTile"
) {
args.set_cancel(
true
);
}
}
Obviously, whether the
There are rumors that VS2014 will use TypeScript as its primary source of JavaScript
We will also continually improve our definitions so they become better at helping you write client-side code with ease. We are already working on an easier way to show you what type of event arguments the event handlers receive, so when that’s available you will no longer need to wonder what’s available in the event you are using.
If you use some tools to help with your TypeScript experience, you have a success story or a tip for your fellow developers, please share it in the comments.
Marin Bratanov was a Principal Technical Support Engineer in the Blazor division.