JavaScript to TypeScript, Why you should make the move!
Tags: typescript, javascript, programming, productivity
Published: 2023-05-21 06:28:32


Strict-typed programming languages have been introduced way back in old programming days, although TypeScript is just typed version of JavaScript, so the question is why TypeScript's getting more and more popularity day by day, and most importantly why we should switch gear for it!

In almost most of strict-typed languages, you need to accept the fact that `types` is a part of the process of coding, while in TypeScript, using of types is optional, JavaScript codes are welcome in `.ts` files, with that being said, this fact leads JS/TS programmers to compare the two experiences,   

> You'll get the benefit of using TypeScript from day one you use it. Anders Hejlsberg - core developer of TypeScript

### TypeScript; the manual of your application

Every system - a television, a microwave etc, that interacts with the user must provide a book of instructions details on how it can be used. So called a manual.

If you're from Dynamic programming background, TypeScript led you to make the manual of your own code. So you, as programmer, are placed as the manual writer of your own code. Similar to rules and laws in a country, types are rules of your application's code. 

### Edge-cases, here and there

By the course of time, TS led you think about edge cases more and more.
By resolving TS errors, you're actually coding for edge-cases(You probably are). Consequently, resolving the TS errors, are actually bug fixes of your code that you're making, while _coding_!

For example, in the code snippet below, you're guaranteed the caller of the `divideNumbers` function must only provide numbers as inputs. Which means you dictated the way the functions should be used, thus `divideNumbers("10", 2)` or `divideNumbers("10", nulk)` are not allowed. Simply the code is made runtime-safe.

```ts

function divideNumbers(a: number, b: number): number {
  return a / b;
}

```

> Tired of perplexing TS errors? look at TS like a personal trainer in a gym who doesn't let you cut corners when lifting weights properly. Good news is: you're being improved.

### JavaScript does type checking, too

In JavaScript, type checking is performed at runtime during execution, so it's dynamic. That's exactly why below code will throw error "caught TypeError: Cannot read properties of undefined (reading 'toUpperCase')"

```js
let message;
console.log(message.toUpperCase()); 
```

Unfortunately, You'll get to know about the errors right when running the code. While in TS, you get warning(TS error) about it, before your code is shipped! 

> Please note that the code snippet provided is written in JavaScript, but TypeScript's inference mechanism allows for automatic type inference.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imgieqxov80pdmrertps.png)

### Typeless vs typed programming language

I do not intend to directly compare these two types here, as this is an extensive discussion involving individuals on both sides of the debate.
Both languages possess their unique underlying philosophy.

As a conclusion, TypeScript made the JavaScript more robust.