TypeScript is a programming language which was developed and is maintained by Microsoft. It is an open source programming language. The language is a superset of JavaScript since it has added some extra features to it, such as support for object oriented programming and optional static typing. The language was developed for the purpose of creating large applications, and it always compiles into JavaScript.
One can use TypeScript to develop JavaScript applications for both the client side and server side. Since we have said that TypeScript is a superset of javaScript, any program written in JavaScript is also viable to be considered a TypeScript program. The TypeScript compiler was also written in TypeScript. The aim of TypeScript is to provide developers with an easy mechanism by which to develop large applications. It comes with concepts such as classes, inheritance, interfaces, modules, and others, and these make it easy for developers to create large applications with much ease.
Chapter 2- Installing TypeScript
There are two ways that we can install TypeScript into our system.
These include the following:
- By use of the npm (Node Package Manager) tool.
- By installation of Visual studio plugins for TypeScript.
For Windows users, you can use the second option if it will be more convenient for you. Just install the Visual Studio, and then download and install the necessary plugin for programming in TypeScript. This option has been found to be a bit superior compared to the use of other code editors. For those using platforms other than Windows, just ensure that you have a text editor, a browser, and the npm package for TypeScript so as to be able to program in TypeScript. The following steps can be followed for this to be done:
- Begin by installing the Node Package Manager (npm).
Just open the terminal, and execute the commands given below: $ curl http://npmjs.org/install.sh | sh $ npm --version 1.1.70
- In the next step, install the TypeScript npm package, and this should be done globally on the command line. The following commands can be used for doing this:
$ npm install -g typescript $ npm view typescript version npm http GET https://registry.npmjs.org/typescript npm http 304 https://registry.npmjs.org/typescript 0.8.1-1 Make sure that you use the latest version of your browser, such as Chrome.
Any text editor is appropriate, and an example of this is the Sublime text editor.
Hello World Example Now that we have set up the environment ready for programming, we can begin to program. We will begin by creating the Hello world example. Remember that TypeScript is a superset of ES5, and it has more features for ES6. TypeScript is also compiled into JavaScript. We want to create our index file and then reference it to an external file.
Here is the code for the file index.html : As shown in the above code, we have referenced the code in the file to the file hello.ts. This has been done in the line as the script source. The extension .ts in the file name means that it is a TypeScript file. Now, create a new file and give it the name hello.ts. The following line of code should then be added to the file: alert('hello world, this is TypeScript!'); After that, just launch the command line, and then navigate to the folder with the file hello.ts. Compile the file by executing the following command: tsc hello.ts The tsc command in TypeScript represents the TypeScript compiler, and it will generate a new file with the name hello.js.
From the extension in the file, we will have transformed the TypeScript to a JavaScript file, which is what normally happens in TypeScript.
Chapter 3- Type Annotations
This feature is optional in TypeScript, and it allows us to express and implement indentation in our TypeScript programs. We need to demonstrate this by creating a program which will calculate the area of a rectangle. Just open your text editor and create a new file with the name area.ts. Add the following code to the file: function area(shape: string, width: number, height: number) { var area = width * height; return "The shape is " + shape + " having an area of " + area + " cm squared."; } document.body.innerHTML = area("rectangle", 20, 15); Next, open the file index.html and change the script source from type.ts to area.ts. Open your terminal, and then execute the following command so as to compile the whole thing: tsc area.ts The message together with the area of the rectangle will be displayed.
As you must have noticed, the type annotations are passed to the parameters of the function, and they are used for indicating the kind of parameters which are supported by the function. In the above example, the parameter shape is a string, while the parameters length and width are numbers. You have to know that both Type annotations and other TypeScript features are enforced during compile time. In case one passes other types of values to these parameters, a compile-time error will be displayed by the compiler. This is a very good idea when it comes to working with large applications. We need to demonstrate how errors will be detected in our case.
Consider the code given below: function area(shape: string, width: number, height: number) { var area = width * height; return "The shape is " + shape + " having an area of " + area + " cm squared."; } document.body.innerHTML = area("rectangle", "width", 15); // wrong type of width In the above example, the width has been passed in as a string, which is wrong. The compiler expects that the width should be a number. After compiling the program given above, you will get the following as the error: $ tsc type.ts type.ts(6,26): Supplied parameters do not match any signature of call target That is the kind of error you get. However, you must have noticed that although there was a warning due to type mismatch, this did not stop the TypeScript file from compiling and creating a JavaScript file. As shown in the above examples, it is very clear that annotations are used for the purpose of recording the function which a function or a variable is intended to perform. Suppose that you have a function which expects that only a single parameter should be passed to it, and the parameter should be of the string type.