Getting Started with TypeScript – Part 8

Hi Friends,

In this section, we are going to see more of types. Basically, we’ll see how to extend the types. If you are coming from from C# or Java background, you must be familiar with extending or inheriting. Although, JavaScript, by default doesn’t support inheritance, but by using Prototypes, we can implement inheritance in JavaScript. I have already have one section dedicated to Prototype(Advanced) JavaScript, you can refer that as well. So, here we are going to see the same, means writing a base class and inheriting the same in the child class. Hence, without wasting time, let’s jump into demos.

Below is the sample snippet with extends keyword in TS format.

And, its equivalent JS file as shown below.

Now, let me go ahead and few properties to child class as well. As you can see in the below screenshot, intellisense is telling, there is some problem with constructor here. Let’s fix that first. In order to invoke the child class constructor, we need to make sure that, we will be invoking super class constructor here as well, which is Employee here. Below snippet is the modified snippet.

Let me go ahead and put the finished code here,

And, equivalent JS code,

With the above change in place, when I go ahead and run the app, it will produce the desired output as shown below. 65th I hope, you would have liked this discussion. In coming section, we will delve more into it.Till then, stay tuned and Happy Coding. Thanks, Rahul Sahay Happy Coding

760 total views, 2 views today