Why Typescript is a Must-Have for Any JavaScript Developer

Why Typescript is a Must-Have for Any JavaScript Developer

Insights from ‘You Don’t Know JavaScript’

·

5 min read

Table of contents

No heading

No headings in the article.

I’m excited to share my thoughts on a topic that has always fascinated me and has had a significant impact on my career and improving my workflow as a developer: TypeScript.

Typescript is a superset of JavaScript that adds optional static typing and other features to the language. It offers several benefits for JavaScript developers, including:

  • Improved error detection: Static typing enables you to catch errors at compile-time rather than runtime, reducing the risk of runtime errors.

For example, consider the following code written in JavaScript:

function add(a, b) {
  return a + b;
}

console.log(add(1, 2)); // 3
console.log(add("1", 2)); // "12"

In the code above, the add function is supposed to take in two numbers and return their sum. However, if we pass in a string for the a parameter, the function will still run and return a concatenated string, "12", rather than throw an error. This can be problematic because it can lead to unexpected behavior in the program.

Now consider the same code written in TypeScript:

function add(a: number, b: number): number {
  return a + b;
}

console.log(add(1, 2)); // 3
console.log(add("1", 2)); // Error: Argument of type ‘string’ is not assignable to parameter of type 'number'.

In this version of the code, we have specified the types of the a and b parameters as number and the return type as number. As a result, when we attempt to pass in a string for the a parameter, the TypeScript compiler will throw an error because it is not a valid type for the a parameter.

This can help improve error detection because it allows developers to catch type errors before the code is even run, which can save time and effort in debugging.

  • Enhanced code readability and maintainability: Explicit type annotations make it easier to understand the purpose and behavior of your code, which can be especially useful when working on large or complex projects.

Let's consider the following code written in JavaScript:

function getUser(id) {
  return fetch(`/users/${id}`)
    .then(response => response.json())
    .then(user => {
      return {
        name: user.name,
        email: user.email
      };
    });
}

getUser(123).then(user => console.log(user.name));

In this code, it is not immediately clear what the type of the user object is or what properties it has. This can make it difficult for developers to understand how the code is intended to be used and may lead to errors if the object is used unexpectedly.

Now let's translate the same code written in TypeScript:

interface User {
  name: string;
  email: string;
}

function getUser(id: number): Promise<User> {
  return fetch(`/users/${id}`)
    .then(response => response.json())
    .then(user => {
      return {
        name: user.name,
        email: user.email
      };
    });
}

getUser(123).then(user => console.log(user.name));

In this version of the code, we have defined an User interface that specifies the shape of the user object, including the name and email properties. We have also annotated the id parameter as a number and the return type of the getUser function as a Promise of a User object. This makes it clear to developers what the types and structures of the user object should be, which can improve code readability and maintainability.

  • Improved learning tool: Typescript’s stricter syntax and helpful error messages can be helpful for beginners learning JavaScript, helping them to understand the underlying principles of the language and avoid common mistakes.

As JavaScript developers, we’re no stranger to the fast-paced and constantly evolving world of front-end development. With new frameworks and libraries popping up all the time, it can be tough to keep up. This one tool that has gained a lot of traction in recent years is Typescript, a tool that adds optional static typing and other features to the language.

But why should you care about Typescript? After all, JavaScript has been around for decades and has proven to be a versatile and powerful language.

In his book “You Don’t Know JavaScript”, author Kyle Simpson argues that Typescript is a game-changer for JavaScript developers, offering a number of benefits that can improve the quality and reliability of your code.

One of the main advantages of Typescript is that it enables you to catch errors at compile-time rather than runtime. With static typing, you can define the expected data types for variables, function arguments, and return values. This helps you catch mistakes early on and reduces the risk of runtime errors. As Simpson puts it, “TypeScript forces you to be more mindful of your code, which ultimately leads to better quality.”

Typescript also offers improved code readability and maintainability. With explicit type annotations, it’s easier for you and other developers to understand the purpose and behavior of your code. This can save time and frustration when working on large or complex projects.

But Typescript isn’t just for experienced developers — it can also be a helpful learning tool for beginners. By enforcing a stricter syntax and providing helpful error messages, Typescript can help you understand the underlying principles of JavaScript and avoid common pitfalls.

So, should you start using Typescript in all of your JavaScript projects? Not necessarily. Simpson advises that you should “use the right tool for the job.” But he also argues that Typescript is a valuable tool to have in your toolbox, and that every JavaScript developer should at least be familiar with it.

In conclusion, Typescript is a powerful and useful addition to the JavaScript ecosystem that offers a number of benefits for developers. Whether you’re a seasoned pro or just starting out, it’s worth considering adding Typescript to your workflow.

As Simpson writes, “TypeScript is the ‘professional’ way to write JavaScript.”

Thank you for reading!

Did you find this article valuable?

Support Egi Mata by becoming a sponsor. Any amount is appreciated!