Why Typescript ??

Vijay Tembugade
4 min readJul 3, 2022

--

Most of the time as developers, when we work on a project, we always have to decide whether we should go ahead with typescript or just javascript would be sufficient. As a good developer, we prefer typescript every time. But, the question arises is why typescript?

If you google “Why we should use typescript ?”, the answer would be something like the following.

In terms of software development, TypeScript offers many advantages over JavaScript. Like, Optional static typing: JavaScript is a dynamically typed language, which means that types are checked, and data type errors are only detected at runtime. This can be very dangerous and can create errors during production.

But, what is static typing? what is typing? What is dynamic typing? what is type check? Let’s get inside this and understand this concept clearly.

Every time we hear about javascript,

“Javascript is dynamically typed weak language.”

Let’s understand this analogy.

So, programming languages are divided into four parts with their intersection. i.e. Dynamic vs Statically typed language and Strong vs weak language.

What is “dynamically typed language” ?

Every programming language has variables. And every variable has some type. It means the variable could be Integer, String, Array, or Boolean. But, In javascript, we don’t explicitly define the type of the variable. For example, if we have a variable a which has a value of 100, here 100 is a number. But, we don't define it, which means we don't tell javascript that a is a Number, javascript takes it as it depends on how we wrote the syntax.

We see this kind of behavior in languages like Ruby and Python.

Note: In dynamically typed language type checking is done during the run time!

What is “Statically Typed Language”?

Statically typed languages are those where we have to define what a type of variable is! For example, in the C programming language, we need to define int, float, char, etc. types explicitly.

int a; 
a = 100;

So, in the above example, we tell our code that a is an Integer and we write it int explicitly.

Now, let’s understand what is Strongly typed and weakly typed Programming languages.

What is “Weakly-Typed Language”?

If you are familiar with javascript you would have known type coercion in javascript,

Type coercion is the process of converting a value from one type to another. This includes examples like string to a number, object to boolean, and number to a string.

In the above example, we see a is converted to a string and concatenated with b.

Implicit vs. explicit coercion

When developers express the intention to convert types by writing code as required is called Explicit type conversion. For example,

Number(num)

Since javascript is a weakly typed language, it converts types automatically without the developer mentioning it.

NOTE: One operator that does not trigger implicit type coercion is ===, which is called the strict equality operator. The loose equality operator ==on the other hand, does both comparison and type coercion if needed.

Read this freecodecamp blog to understand type coercion in a better way.

What is “Strongly typed language”?

In simple terms, strongly typed languages are which where developers are restricted to mixing different types of data types. There is no implicit type of conversion that happens in these languages. For example, Python and Ruby are strongly-typed languages.

number = "some" 
number + 10 // this gives an error in python.

In the above example, we can see that we cannot add a type string with a type number or integer, and this is what is called as strongly typed language.

Why Typescript ??

So, after understanding the above things, here is the first main reason comes for why typescript?

TypeScript makes JavaScript a static and strongly typed language.

Typescript is a superset of Javascript, which means, it has all functionalities of javascript as well as some of its own functionalities which makes javascript better.

There are some other main reasons why we should use TypeScript?

  1. Readability: Typescript provides readability towards the code and types so that at a time many developers can work on the same projects.
  2. The power of object-orientation: Typescript supports OOP concepts such as classes, interfaces, and inheritance, whereas Javascript is a prototyped-based language.
  3. IDE support: Developer free environment is created with typescript which gives auto-correct and suggestions like features in IDE too.

General Function in Typescript looks like below. Where, x and y are the parameter which are type of number, and we also define the return type of a function which is also number.

There are several types in TypeScript, like number, string, array, object, undefined and null.


// string data type in typescript
let myName: string = "Vijay";
// number data type in typescript
let myAge : number = 23
// Array of number and strings in typescript
let values: (string | number)[] = ['Apple', 2, 'Orange', 3, 4 ];

Advantages of using typescript

  • Typescript always points out compilation errors at the time of development.
  • TypeScript supports static and strong typing. This means that type correctness can be checked at compile time. This feature is not available in JavaScript.
  • Developers using JavaScript from other languages often complain about the lack of strong static characters, but that’s where TypeScript comes in to fill that gap.

“A language that doesn’t affect the way you think about programming is not worth knowing.”
Alan J. Perlis

--

--