Hello world program in typescript

Hello world program

Today I am going to show you how to write simple hello world program in typescript, it is very simple to write helloword program and run it. Also we will talk about how to write a class in typescript and constructor in typescript.

Open the vs code and type the following single line of code and save it in your system's folder and follow the given steps.

console.log("Hello Word");

Step to compile and run the typescript program.

1.Save it with name helloword.ts
2.Open NodeJs Command Prompt , go to the directory where your helloword.ts file is saved.
3.In my case file available in E:\typescriptprtcl\New folder
4.So I am trying to go to in E:\typescriptprtcl\New folder using command.
     
Hello world program in typescript




5.Now do the following steps

1. tsc helloword.ts  when you type this command and hit enter the typescript program will be compile and one more file created with the name helloword.js  .See in your folder,here tsc means typescript compiler.
2. node helloword.js whenever you hit this command your helloword.js result show in picture.


Note:

tsc is compiler which convert the typescript file into javascript file so that we can run javacript, we can’t run directly typescript first we need to convert it.


Hello world program in typescript



How to write class in typescript and create object of class

class office
{
   empcode:any  ="Helloword";
}
   let obj=new office();
   console.log(obj.empcode);

In above example any is a variable which is able to store any datatype in typescript. Let key use to create an object of a class so that we can call variable of the class. console.log  is same as Console.Write function in c#.

Constructor In typescript:

Constructor is special type of function which name same as class name in other language.  But in typescript if we want to create constructor , we need to write constructor keyword. See Example



class office
{
   empcode:any;
   constructor(code:number)
   {
     this.empcode=code;
   }   
}
   let obj=new office(101);
   console.log(obj.empcode);



Previous
Next Post »