Hello World in TypeScript

Create a workspace to store all your TypeScript files in it, and open up VS Code editor and open the folder in workspace using (File –> Open Folder or Ctrl+K and Ctrl+O)
Create new file with name hello-world.ts , ts here indicates TypeScript file. Write the below code inside it.

var fn = function () { return 'response'; };

Open up the terminal and now compile the hello-world.ts file using command below (Terminal can be opened up using CTRL+` in VS Code)

--one time compile
tsc hello-world.ts
--Watch hello-world.ts file for changes and keep compiling
tsc hello-world.ts -w

After compilation is done a hello-world.js file will be created parallel to hello-world.ts file.This file is the compiled JavaScript code equivalent to that of TypeScript code.The code inside hello-world.js look like below

var fn = function () { return 'response'; };

To execute the js file run the below command

node hello-world.js

Defining Primitive types in TypeScript

You can indicate a var as a number, boolean or String using the below command.

var a : number;
var b : boolean;
var c : String;

a = 10;
b = true;
c= "Hello World";

If you assign a incorrect type to a variable then TypeScript compilation will fail
Example : Below

var a : number;
var b : boolean;
var c : String;

a = 10;
b = true;
c= 20;

Compilation of above code will give you error :

Leave a Comment