DEV Community

Cover image for JavaScript to TypeScript, Why you should make the move!
Mehdi Raash
Mehdi Raash

Posted on

JavaScript to TypeScript, Why you should make the move!

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.


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

Enter fullscreen mode Exit fullscreen mode

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')"

let message;
console.log(message.toUpperCase()); 
Enter fullscreen mode Exit fullscreen mode

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

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.

Top comments (1)

Collapse
 
devto2k5 profile image
dev procedure

Do programmers actually create functions to "divide" in the real world?

Or does this example show how TypeScript tries to solve problems that don't exist in the real world?