Typescript Getting Started
learn about Typescript , setup environment for ts and write HelloWorld.ts
What is Typescript
is an open source programming language developed by Microsoft
the very first question we may ask is
why learn another language ?
Let's try to answer this question in the following points ::
- Typescript adds types to JS , which makes your apps more robust and less prone to error
- Typescript performs many checks on the compile time itself , so you've less surprizes at runtime
- Typescript can be used with Node.js , Angular , React , Ionic means you learn one language and apply on mobile , web , desktop and servers
- Make use of other features of Typescript like Enums , Mixins , Generics , Interfaces etc
All these points accompanied by large companies and a huge community support is a good reason to invest time and energy in learning Typescript.
In this introductory Typescript tutorial , we're going to do the following ::
- Setup Typescript environment
- Write HelloWorld program in Typescript
can do that , let's get started.
In order to follow this Typescript tutorial , we need the following ::
ide , you can choose from Sublime Text , Visual Studio Code or Atom
Once you have these , you're ready to start developing with Typescript.
Install Typescript by executing the following command in your terminal window.
npm install -g typescript
Once you've Typescript installed , check its version with the following command
The output of the above command is the version of Typescript installed on your machine.
Writing Hello World in Typescript
OK , time to write our first Typescript program , create a file HelloWorld.ts in your IDE and enter the following single line of code into that.
document.write('Hello World in Typescript') ;
If you notice , it's the same as JS but it's perfectly valid because as we mentioned above ,
all valid JS is valid TS also.
tsc command invokes the Typescript transpiler and processes the file given as parameter and outputs a JS file with the same name in the same directory. So as the output of the above command , you
should see a new file HelloWorld.ts created in your directory. Now create an HTML file and refer that JS file in your html.
Now load this file in your browser
and you should see Hello World TypeScript
as the output.
In this introductory Typescript tutorial , we learned the following ::
- What and Whys of Typescript
- Setting up the Typescript development environment
- HelloWorld program with Typescript