Why You Should Choose TypeScript Over JavaScript

Why You Should Choose TypeScript Over 
                                             JavaScript

What is TypeScript?

In short, TypeScript is just a superset of JavaScript with static typing and compiles to plain JavaScript. In plain words, TypeScript is technically JavaScript with static typing whenever you want to have it.

why-use-typescript-pirate-meme_.png

But wait, What is Static Typing?

Static Typing is when the compiler enforces that values use the same type. Here's an example. This is valid in JavaScript:

let greeting="hello"
greeting = 5

Here, the type of greeting changes from a string to a number. In TypeScript, this is forbidden.

let greeting:string = "hello"
greeting = 5

We would encounter an error that would say error:'Type' number is not assignable to 'type' string.

So in simple words, Static Typing is the type of variable that must be known at compile time. If a variable is declared, it should be known by the compiler if it will be a number, a string, or a boolean. Now, let's find the reasons for adding static typing to JavaScript?

I can list at least three:

  • Orienting yourself in complex, large-scale systems is not as dreadful anymore.
  • It is easier to refactor code without breaking it significantly.
  • You can avoid hidden errors like the classic 'undefined' is not a function.

    What are types, and how do they work in TS?

why-use-typescript-typescript-meme_.png Types are a way to correct programs from incorrect before we run them by describing our code and how we plan to use our data. It can vary from simple types like Number and String to complex structures perfectly modeled for our problem domain.

Basic Types Typescript has several basic types that are pre-defined. For e.g., Number, String, boolean, etc.

let name:string = "Kunal"
let isCorrect:boolean = true
let age:number = 21

and if we want to assign a number to name, let's see what happens

LavOYAOCj.jpg

As we can see that an error arises if you want to assign a number to a string.

For Objects

type Employee={
name: string,
gender: string,
phoneNumber: number
}

const emp1:Employee={
name: "Arpit",
gender: "Male",
phoneNumber: 1234
}

Union Type

A union type is formed when two or more other types representing values that may be any one of those types. We refer to each of these types as the union's members.

function printEmpId(id:string | number){
console.log("Emp id is" +id)
}

printEmpId(101)
printEmpId("2")
printEmpId({id:"20"}) //wrong

any can cover well anything that you wish but is generally used when you don’t want a particular value to cause type-checking errors.

let emp:any={age:22,gender:"Male"}

Reasons to choose TypeScript over JavaScript

  • TypeScript is more reliable

In contrast to JavaScript, TypeScript code is more reliable and easier to refactor. This enables developers to avoid errors and do rewrites much easier. Types invalidate most of the common errors that can see in JavaScript codebases and create a quick fix to all the little mistakes when writing new code and refactoring.

  • TypeScript is more explicit

Making types explicit focuses our attention on how our system is built and how different parts interact with each other. In large-scale systems, it is important to abstract away the rest of the system while keeping the context in mind. Types enable us to do that.

TypeScript and JavaScript are practically interchangeable, so why not? Since TypeScript is a superset of JavaScript, you can use all JavaScript libraries and code that you want in your TypeScript code. This means that you can slowly adapt TypeScript in your JavaScript codebase, adding types to individual modules and then expanding your codebase to consume the known universe, I guess.

Conclusion

That's it, guys. I have covered some basic things about TypeScript and the advantages of it over JavaScript.

Overall, TypeScript is a great weapon to have in your arsenal, even if you don't use it to its full capacity. TypeScript is quiet, inclining, and inviting to beginners, so there is no need to be afraid.