Environment Setup for Development in TypeScript
What is TypeScript
TypeScript is a programming language developed and maintained by Microsoft. It is super set of JavaScript which provides optional static typing to language. TypeScript may be used to develop JavaScript applications for client-side or server-side (Node.js) execution.
TypeScript compiles to JavaScript so any existing framework of JavaScript can execute it i.e. browsers. Every valid JavaScript program is also a valid TypeScript program but not every TypeScript program is JavaScript which is clear from following figure.
For development in TypeScript, one needs to know the following:
A Text Editor
The text editor helps you to write your source code. Examples of a few editors include Windows Notepad, Notepad++, sublime & atom, etc. Editors used may vary with Operating Systems.
The source files are typically named with the extension .ts.
The TypeScript Compiler
The TypeScript compiler is itself a .ts file compiled down to JavaScript (.js) file. The TSC (TypeScript Compiler) is a source-to-source compiler (transcompiler / transpiler).
The TSC command generates a JavaScript version of the .ts file passed to it. Bascially, the TSC produces an equivalent JavaScript source code from the Typescript file given as an input to it. This process is termed as transpilation.
However, the compiler tends to rejects any raw JavaScript file passed to it. The compiler only deals with .ts or .d.ts files.
For installing TypeScript, we need to install NodeJs and npm.
Installation on Windows
Download the LTS version of node from https://nodejs.org/en/download/ and install it.
This package would also include the latest LTS version of npm (In my case node: v8.9.0 and npm: v5.5.1). you can verify the installation of node and npm with executing following commands in command prompt.
Installation on Ubuntu (16.04 LTS)
Installation of node in Ubuntu is much simple, you just need to open the terminal and execute following commands:
First you need to update apt packages list using following command.
After updating apt packages list execute following commands.
Now to check version of installed node and npm execute following commands:
To install TypeScript, we just need to execute the following command in either command prompt (for windows) or terminal (for Ubuntu/Linux)
This will include ‘tsc’ environment variable for transpilation of TypeScript, for verification of installation of TypeScript execute the following command:
Editor
The most common and powerful IDE for development of TypeScript is Visual Studio Code. For installation of Visual Studio Code visit this website and download the respective installation package i.e. for windows or Ubuntu/Linux. https://code.visualstudio.com/
After it in windows open the installer follow the on screen instructions,
And in Ubuntu run the following command in terminal
Hello World Program
Open Visual Studio and write your first program:
Let us see how to compile and execute a TypeScript program using Visual Studio Code. Follow the steps given below.
Step 1 − Save the file with .ts extension. We saved the file as Test.ts. The code editor marks errors in the code, if any, while you save it.
Step 2 − Right-click the TypeScript file under the Working Files option in VS Code’s Explore Pane. Select Open in Command Prompt/terminal option.
Step 3 − To compile the file use the following command on the terminal window. Then use node to execute JavaScript file.
The successful transpilation of TypeScript Code and execution of transpiled JavaScript code indicates the correct installation of node, npm, visual studio code and TypeScript for development.