Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


This is practice-first introduction into TypeScript 2.0 language brought to you by programming mentor. Each step continues on files created as result of previous step.

Step 01 - Prepare environment. Create 'Hello World'. Basic configuration and debugging

I. Install prerequisites

1. Install Node.js

Navigate to, download and install latest version of Node.JS (7.1.0)

2. Install Visual Studio Code

Navigate to, download and install latest version of Visual Studio Code (1.7.1)

3. Install http-server

npm install -g http-server

4. Install TypeScript

npm install -g typescript

II. Create Hello World application

Create new folder and index.html file

Open folder in Visual Studio Code and create index.html file

<!doctype html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Hello World in TypeScript</title>
    <script src="index.js"></script>

Note that we referenced index.js file that should be JavaScript file that we will received by compiling TypeScript into it.

Create index.ts file in TypeScript


let greetings: string = `Hello World!`;
document.body.innerHTML =  greetings;

Note that we written code very similar to JavaScript, the only difference from EcmaScript 2015 is that we provided data type string to the variable.

Run TypeScript compiler

Go to the terminal (Ctrl-` in Visual Studio Code) and run the following command:

console command:

tsc index.ts

If the code is syntactically correct this command will not produce any output to the screen but will generate index.js file:


var greetings = "Hello World!";
document.body.innerHTML = greetings;

As you can see, data type has been removed and also syntax converted to classical JavaScript because by default TypeScript compiler outputs to ES3 standard of the language.

Run http-server and open web page in a browser

In the terminal run the following command:


It will output addresses and port to connect to the local server:

Starting up http-server, serving ./
Available on:
Hit CTRL-C to stop the server

Open web browser and navigate to one of the listed addresses, it should print 'Hello World!' on the page.

Running TypeScript compiler in watch mode

If you want to modify transcript file you have re-run compiler manually but it has an option to watch for changes, so let's open new terminal window and run it in watching mode:

tsc -w index.ts

Let's make changes to index.ts and save it:


var greetings = "Hello World!";
document.body.innerHTML = greetings;

TypeScript compiler will output to the console that file changes detected and it starts incremental compilation. After refreshing browser we will see that the page is updated.

III. Basic debugging and configuration

If we go to developer tools in browser and look at source code we will see that there is only JavaScript file index.js and there are no original TypeScript source code. While it is possible to debug generated JavaScript too, it might be much better to see and debug original TypeScript code in browser. So let's configure TypeScript to allow it.

Configuration for TypeScript is stored in file tsconfig.json, let's create the file and tell compiler to generate source map files as well as run compiler in watch mode and provide source file:


    "compilerOptions": {
        "sourceMap": true,
        "watch": true
    "files": [

Let's restart compiler: stop it with Ctrl-C and run with the following command:


Now it will produce additional file that contains source code map. Let's refresh browser and look to the sources. As you can see, original source code is visible in the debugger, we can set breakpoints inside it and also see content of the variables when debugger is paused.

Next let's do a mistake in our source code and see what happens. Instead of assigning a string to greetings variable let's assign a number to it.


let greetings: string = 42;
document.body.innerHTML =  greetings;

IDE will warn us about the error and also compiler will output error to the console:

console output:

index.ts(1,5): error TS2322: Type 'number' is not assignable to type 'string'.

But compiler will not stop compilation on error and still produce JavaScript file:


var greetings = 42;
document.body.innerHTML = greetings;

As you can see, it is syntactically valid JavaScript file, but semantics of TypeScript is broken because we declared greetings to be string variable.

Let's make more mistakes - remove assignment operators from both lines and see what happens:


let greetings: string 42;
document.body.innerHTML greetings;

IDE and TypeScript compiler warn us about the error:

console output:

index.ts(1,23): error TS1005: '=' expected.
index.ts(2,25): error TS1005: ';' expected.

But compiler still produce JavaScript code that is completely broken this time:


var greetings = 42;

We also can't debug it using source code maps because erroneous code in TypeScript can't be correctly mapped to produced JavaScript code.

So we can conclude that default behavior of TypeScript compiler to produce output code even if source files have errors is not quite correct.

Let's fix it by adding option noEmitOnError to configuration of the compiler:


    "compilerOptions": {
        "sourceMap": true,
        "watch": true,
        "noEmitOnError": true
    "files": [

This configuration will stop compiler from producing output if there are errors in source code.

You can’t perform that action at this time.