Why do we need to learn Typescript?

Why do we need to learn Typescript?

  • Hello Techies, let's see do we really need to learn Typescript? Is JavaScript not Sufficient?

  • Does Typescript provide more functionality to the code?

  • Why TypeScript is used when at the end of the day the final Code will get compiled into Javascript?

Techies, just Stop your confusion and let's dive deep into Typescript and understand why we need Typescript.

TypeScript: A Superset of JavaScript

  • In its essence, TypeScript is a statically typed superset of JavaScript

  • In Simple terms, all the features that are available in Javascript are also available in TypeScript, just like the below image

  • The Yellow Box is JavaScript

    Think of JavaScript as a small yellow ball and TypeScript as a big blue ball containing all the features of its smaller counterpart

  • TypeScript does not provide any extra Callbacks, loops, or Arrow Functions

  • Typescript just allows one to write Javascript in a more precious manner and Error-free code.

Type Safety & Error Prevention

  • Let's understand this with an Example

  • console.log(2 + "2") output : 22

  • console.log(null + 2 ) output: 2

  • Here Javascript is trying to add a Number with a String, which is not an ideal way

  • There should be some feature in my code that could stop me from doing this

  • TypeScript intervenes here by providing type safety, preventing such implicit type coercion

  • This enhancement in type safety helps catch errors at compile-time rather than runtime, leading to more reliable and maintainable code.

  • Typescript is all about Type-Safety

Static Checking and Developer-Friendly

  • Typescript performs Static Checking

  • What is Static Checking?

    • When we are writing the code, the parser of the language is constantly being analyzed by the IDE, and through the error upfront
  • Eg: I am trying to access my object property, which is not present, if there is something in my code that can analyze and let me know that the object property does not exist, then it will be much easier

  • string + number, if someone could tell me string + number addition is not possible, Then it would be a great help and allow me to write less error-prone code

  • Typescript analyzes the code and helps you understand that you might be running into a potential error

  • This capability also guides developers in understanding which operations are permissible, such as clarifying that adding a string to a number is invalid.

Scalability and Development Efficiency

  • TypeScript operates as a development tool layer over JavaScript, fostering better code quality and scalability.

  • In larger projects with multiple contributors, TypeScript shines by enhancing code readability and maintainability.

  • The robustness of TypeScript becomes apparent as projects scale, enabling smoother collaboration and reducing the likelihood of errors in complex codebases.

Summary

  • TypeScript is a Development Tool

  • Typescript is just a layer over JS

  • In summary, TypeScript isn't merely about transforming JavaScript; it’s about enhancing its capabilities.

  • Its static typing, error prevention, and developer-friendly features make it an indispensable tool for modern development.

  • By embracing TypeScript, developers can write more robust, error-resistant, and scalable code, ultimately streamlining the development process.

Did you find this article valuable?

Support Ankush Thakur by becoming a sponsor. Any amount is appreciated!