TypeScript compiler is developed in NodeJS, so we need first install it. Let’s start with NodeJS

First of all, go to official NodeJS Web and download installing packages according with your Operating System.

Then, we can install TSC, compiler TypeScript to JavaScript. We can do it in two different ways:

  • Using npm:
> npm install -g typescript
  • Or if you use Visual Studio Code, you can add the plugin.

Once we have installed, it could be checked running:

> tcs -v

Now we are ready to use TypeScript in our system, all we need is create new file with “.ts” extension.

Build our first TypeScript file

Using your favourite text editor, save new file with “.ts” extension. For example: “first.tsc

function saludo() {
let s:string= "Hello World! ";
return s;
}
console.log( saludo() );

When we run tsc command, it shows compilation errors and by the way, it creates the “.js” file. In comand line terminal, go to folder in wich you have the new file and run the compiler:

> tsc first.tsc 

A JavaScript file will be generated with “.js” extension and with same name “first.js”. It has content translated into JS language:

function saludo() {
var s = "Hello World! ";
return s;
}
console.log ( saludo() );

As we can see both files are similar, but if we use some concepts in TypeScript that not exists in JavaScript, the compiler translate it into JS language and we don’t have to do anything extra.

Also, we can run code in our JavaScript file using node and we can check the same result in the console. In our terminal, go to the file folder and run:

> node first.js
Hello World!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store