DEV Community

Cover image for How Typescript differ from Javascript
Sankalpcreat
Sankalpcreat

Posted on

How Typescript differ from Javascript

In this small section we will be exploring typescript advantages over javascript how it can be used for the better development purpose.
Types Mostly Typescript contains some special keyword like Number,String,Boolean but some are very import as they might cause an lot of bug in code like Any and Never these two keyword makes hell if you don't use them carefully many developer face problem with any and never keyword as Typescript is Type defined language so to be precises we have to used these data carefully what they are return value etc.

Javascript :
let value=32134;
Typescript 
let value:number=32134;
Enter fullscreen mode Exit fullscreen mode

Disclaimer not to use any
let us take an example

let hero 

function gethero(){
return "gfg";
hero=gethero()
Enter fullscreen mode Exit fullscreen mode

In above example typescript will automatically detect the return type and it will show us any this means no type is define so by default it will take any as type so this value can be changed if any keyword is define it can be boolean ,number ,string any thing this may create a havoc in your code so be careful while using any keyword in your code.

In the next section we will learn about how to use typescript in your defined function lets say

function data(email:string){
 email.toUpperCase();
Enter fullscreen mode Exit fullscreen mode

Let us take the above example in that we have taken a function which will take parameter as email and that to is defined as string that is new thing that we have introduced in Typescript,which was not in javascript
If you want to return type of function

function data(email:string):boolean{
return true;
Enter fullscreen mode Exit fullscreen mode

Here we have defined the return type of function data which has return type of boolean so it will return the true and false value

In this section we will be learning Interface how to use? where to use? when to use ?

interface User{
name:string;
id:number;
above18:boolean
Enter fullscreen mode Exit fullscreen mode

this is an interface defining to use the and define various data and the object inside it with single name if User which can be further used

const value:User{
name:"bet",
id:52,
above18;true,
Enter fullscreen mode Exit fullscreen mode

This is the way how to use interface with the object value by use of previous data

There is lot of discussion in the Tech industry that where to use interface vs type
We can use both of them but there is problem if you want to add more data in type it will give error but on the other side interface it will not give you error.That means interface can be extended

One more major Advantage of type over interface is that it can be used as union,tuples and primitive this section will be covered in next blog

Now here comes the important points where to use interface and where to use types
Interface

  • if new object need to bed defined Type
  • When your need is to define tuples,primitive and union
  • if you are using map type function to take the value

some important topics like Generics,tuples,primitive usage of typescript in abstract class

Top comments (0)