You can easily install typescript with the help of NPM. For this first install Node Js on your machine. To download and install it you can visit to NodeJs official website and follow the instructions.After this you can use
npm -version to check if it is installed.Now to install typescript compiler use following command.
1. Traverse to your typescript workspace and create a file as hello.ts with below code
2. compile it using
tsc hello.ts. This will generate a hello.js file in the same location.
3. Run as
node hello.js. This will print Hello, Devglan in the console.
A variable in typescript can be boolean, string, any or number and any variable in typescript can be declared using keyword - var and let. But it is always recommended to use let keyword to define a variable in typescript as it provides type safety.Following are the different ways to define a variable in typescript.
The advantage of this different variable type is - a variable declared as
number can not hold a string value as typescript compiler will show you a compile error. Similarly, if you have a variable declared as
any can hold any data type. This is similar to
Typescript also provides support for enum similar to object oriented language to declare constant.Following is an exampple to declare enums in typescript.
Arrow functions are similar to lambda expression in java.We can execute inline functions using it. Suppose we have a code of block as follow:
This can be replaced using arrow function as
var greet = (person) => console.log(person);
Interface is used to define reusable custom data types. For example, if you want to create a reusable
User object then you can have name, age, gender as its types.
And this can be used as a reusable type as below:
Class is a logical entity that has variables and functions that are highly related to perform single operation. The difference between a class and an interface is that class can have implementation inside it whereas an interface does not have any implementation inside it. Following is a simple User class that has fields and methods:
Now we can instantiate this class and use it's method in following ways
Constructor is used to instantiate a class.The declaration of a constructor is similar to a method without any return type.Following is the declaration of constructor for class defined above.
Now doing so the line
let user = new User(); will show compiler error as there is no matching constructor in the class definition and typescript does not support constructor
overloading.To remove this compile error, you can make the constructor parameters optional as follow:
Access modifier is used to modify the access level of the variables and methods used inside any class.We have 3 different access modifiers in typescript - public, private and protected. By default all the members are public and tht's the reason we were able to initialise class variable from outside the class.Following is the syntax to use access modifiers.
In this article we learned about getting started with typescript with variable, class, interface, access-modifier. If you have anything that you want to add or share then please share it below in the comment section.
1. Rxjs Tutorial
2. Angular Material App
3. Install Nginx Php Mac
4. Nodejs Basic Tutorial
Contact us: firstname.lastname@example.org