Getting Started with TypeScript – Part 3

Hi Friends,

In this section, we will continue from the last place and explore more into TypeScript. Here, we will be starting with basics of TypeScript, means very much around the syntax areas. Let us look at few samples around the same.

Now, if I hover on the x variable, it will show of type number as shown below.

44th

Now, similar thing can be achieved by using annotation as shown below in the snippet. Here, we are explicitly making the stuff as number.

45th

Now, with annotations, we can force static type checking. Let’s see the below example for instance.

However, while calling the Add function here, intellisense prompts me at every stage as shown below.It says you need to pass two input parameters of type number.

46th

However, when I build the same, it will produce the following error.

47th

Therefore, this is the magic of using TypeScript on top of JavaScript.This way we are actually writing very type safe and robust code. Now, let me fix the same and produce the output.

With the above change in place, it will produce the following output.

48th

I hope you would have liked this discussion. In coming section, we will delve further into TypeScript. Till then stay tuned and Happy Coding.

Download Code Link:- https://github.com/rahulsahay19/TypeScript-Demo

Thanks,
Rahul Sahay
Happy Coding

1,961 total views, 1 views today