HTML5 is becoming a popular platform among game developers to deliver their games on web. Several game engines and tools have been developed to make the game development process smoother. But the basic limitation of HTML5 is that you get stuck with JavaScript. Unlike desktop games where you can use any language to develop games, same is not possible with HTML5 even in android there are different languages available e.g. Java, C++ and lua.



JavaScript is a nice language but it does not offer static type checking and class based object oriented programming. There are ways to simulate class based object oriented behavior through prototypes but currently there is no direct way. Hence things gets really complicated while developing large scale applications in JavaScript.

To deal with this situation, developers have come up with new languages which offer more functionality and features but they still get compiled into JavaScript only. So, you can develop applications and games in languages other than JavaScript but they will still run on web browsers as the compiled output will be a JavaScript only.

Several languages have been built upon JavaScript including some famous ones like coffee script, type script, Dart, etc. In this blog, we will talk about TypeScript.

As per Wikipedia: TypeScript is a free and open source programming language developed by Microsoft. It is a strict superset of JavaScript, and essentially adds optional static typing and class-based object oriented programming to the language. Anders Hejlsberg, lead architect of C#, has worked on development of TypeScript.

TypeScript extends JavaScript syntax, so any existing JavaScript programs work with TypeScript without any changes. TypeScript is designed for development of large applications and when compiled it produces JavaScript to ensure compatibility.

TypeScript is a language extension that adds features to JavaScript.

Type annotations and compile-time type checking

Classes

Interfaces

Modules

Abbreviated “arrow” syntax for anonymous functions

Getting Started with TypeScript

To use TypeScript you will need its compiler which is based on node.js. Hence, you need to first install node.js (Note: node.js is only required to compile the typescript to javascript)

Once you have installed node.js, install typescript

> npm install -g typescript

to compile any typescript file use tsc command

> tsc your.js

Let’s Say Hello to TypeScript

function greet() { console.log(“Hello! World”); } greet();

As you can observe that above code is plain JavaScript which shows that typescript is nothing but a super set of JavaScript, hence your existing JavaScript project is already compatible with TypeScript.

Let’s write the above program using classes.

class Greeting { private name:String; constructor(n:String) { this.name = n; } sayHi():String { return “Hello! ” + this.name; } doHi() { console.log(this.sayHi()); } } var greet = new Greeting(“Suyash”); greet.doHi();

The corresponding JavaScript generated is

var Greeting = (function () { function Greeting(n) { this.name = n; } Greeting.prototype.sayHi = function () { return “Hello! ” + this.name; }; Greeting.prototype.doHi = function () { console.log(this.sayHi()); }; return Greeting; })(); var greet = new Greeting(“Suyash”); greet.doHi();

You might think that if you have to write more code, then why to choose TypeScript. Because as your project scales, code gets complicated and a proper structuring of your code will be needed. TypeScript is developed keeping in mind the large-scale applications.

For game developers, TypeScript can be very useful as it supports class based object oriented programming and you can easily represent different game objects and people in your game through classes which will make your code easily represent the real world scenario.

If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com.