5 Reasons why you should switch to TypeScript

·

3 min read

Untitled

If you switch to JavaScript from languages like Java or C++, you will notice the stark difference between the former and the latter. One of the key distinctions between Java and JavaScript is the lack of need for allocating types to your variables or objects in JavaScript. Even though this makes coding slightly painless and faster, it might lead to significant bugs during production.

Example: We want to add two numbers in the below code, but we have strings as our input. It will not throw an error on runtime, but we have a logical mistake in our code, leading to considerable problems in our Web Application development.

function add(num1, num2) {
    return num1 + num2;
}

console.log(add('2','3'));
//23

Large codebases are more susceptible to these types of errors.

Noticing this issue, Microsoft released a syntactical superset of JavaScript called TypeScript. Code written in TypeScript compiles to JavaScript workarounds using the TSC compiler.

Untitled

A function to add two numbers in TypeScript would look like this:

function add(num1 : number, num2 : number) {
    return num1 + num 2;
}

console.log(add('2','3'));
//throws compile-time error

Here we explicitly specify the type of data we are expecting to be passed to the function. We get a compile-time error if we try to pass strings to the function add.

TypeScript has three primitive types:

  • string
  • number
  • boolean

And it also supports Arrays, Tuple, Enum, Unions, etc.

Other features that TypeScript has introduced include interfaces, ES6 classes, and great tooling support with IntelliSense.

Advantages of using TypeScript over JavaScript

Optional Static Typing

JavaScript, being dynamically typed, will not show any type differences up until runtime. Whereas in TypeScript, once a variable is statically typed , the compiler alerts developers of any type-related errors.

Early Bug Detection

It is a notable advantage as warnings and errors are caught or us during development, decreasing the chances of bugs and unexpected behavior at runtime. It also helps refactoring code quickly with confidence

Improved Readability

TypeScript static reading and interface increases code optimization. Interface in typescript can be used to define a type and also to implement it in a class, resulting in a more informative codebase. It increases both the readability and stability of the code.

Code Optimization

TypeScript is thought to have better code organization and object-arranged programming procedures and is capable of increasing development speed via Type Annotation, Generics, API Documentation, and IntelliSense support.

Conclusion

JavaScript is a great choice when you want your code to be short and flexible. However, if you are dealing with a large codebase, TypeScript is your best bet. As your code grows and becomes more complex to handle, there is more potential for errors, and TypeScript catches these errors for you during compile time.