Whenever we search for above question – the answer we get most of the time is, Typescript is superset of Javascript,but what exactly that means?

Let’s go deep into above question together,and we will try to deduce a simplistic explanation of above statement.


Look at the above image.

Now,without complicating things much,Typescript is everything what Javascript is along with some additional features.

So,if you are a Javascript developer,even in Typescript you are writing Javascript only along with additional features which Typescript provides.

Typescript Features???

We have been talking about Typescript features a lot,but what exactly are those.

Before coming to that,one question for Javascript developers out there.Just look at below snippet and think,how often have we encountred the below issue,even tough our application is compiled successfully.

function sub(a,b){ return a - b;
} sub(2,3); // Output -> -1
sub(2,"JS");	// Output -> NaN

Nothing wrong with above function and above function calling and is correct if we see it from Javascript perspective,and moreover our code will work correctly, irrespective of invalid type arguments. Issue is with the output we are getting.

And to save from this,we have the feature we have been talking about(Trust me, this is the last time,I am using that word ✌).

Issue is the type mismatch and that is what is Typescript all about –> the TYPE
Whenever we use Typescript,type of the variables are defined either implicitly or explicitly by developers and thus,will help in overcoming the issue during compile time only instead of runtime.

So, rewriting the above snippet Typescript way:

 function sub(a:number,b:number):number{ return a - b;
} sub(2,3); // Output -> -1
sub(2,"JS");	// Error during Compile time only and won't allow compilation at all

Don’t go into the fancy syntax,basically what this below code is saying

function sub(a:number,b:number):number{ return a - b;

I have a function sub which takes 2 arguments and both should be number,and return type of this function is number.

Anything which violates this contract either through arguments or through return type will give me a compilation error.

So, basically this is what it means when we say,Typescript is superset of Javascript.You are free to use Javascript but at the same time can leverage TYPE binding provided by typescript.

Source: https://www.codementor.io/avinashdev21987/what-is-typescript-1izm2jljle