DEV Community

Cover image for Why Use TypeScript For JavaScript Development Today
Dev By RayRay
Dev By RayRay

Posted on • Originally published at byrayray.dev on

Why Use TypeScript For JavaScript Development Today

TypeScript is an open-source programming language developed and maintained by Microsoft. It is a typed superset of JavaScript, which means that all JavaScript code is valid TypeScript code, but TypeScript adds some additional features that make it easier to write and maintain large and complex codebases.

One of the main reasons why TypeScript is essential is that it allows developers to add type annotations to their code. This means they can specify the data type of variables, function arguments, and return values, which can help prevent many common bugs and make the code easier to read and understand.

Originally published on ByRayRay.dev .

divider-byrayray.png

Function without types

For example, consider the following JavaScript code:

function add(x, y) {
    return x + y;
}

const result = add(1, "2");

Enter fullscreen mode Exit fullscreen mode

In this code, the add function is supposed to add two numbers, but because JavaScript is a dynamically typed language, it will silently try to convert the string "2" to a number and then add it to 1, producing the wrong result. This can be very difficult to debug, especially in large and complex codebases.

divider-byrayray.png

Function with types

With TypeScript, we can add type annotations to the add function to specify that it takes two numbers as arguments and returns a number:

function add(x: number, y: number): number {
    return x + y;
}

const result = add(1, "2"); // This will cause an error

Enter fullscreen mode Exit fullscreen mode

Now, if we try to pass a string as the second argument to the add function, the TypeScript compiler will give us an error, alerting us to the fact that there is a bug in our code. This can save us a lot of time and frustration, and it can help ensure that our code is correct and free of bugs.

Another reason why TypeScript is important is that it provides many features that are not available in vanilla JavaScript. For example, TypeScript supports classes, interfaces, and modules, which can help us organize our code more logically and maintainable way. It also supports type inference, which can automatically infer the types of variables and expressions based on the context in which they are used. This can save us a lot of time and effort, and it can make our code more concise and readable.

Overall, TypeScript is a valuable tool for writing great and readable JavaScript code. It allows us to add type annotations to our code, which can help prevent bugs and make the code easier to understand. It also provides additional features unavailable in vanilla JavaScript, which can help us write more modular and maintainable code. If you are working on a large or complex JavaScript project, it is worth considering using TypeScript.

If you want to start learning TypeScript today? I wrote a great post "TypeScript for beginners" where I teach how you can use all the essential parts of TypeScript. As an example I use 🍕😅

divider-byrayray.png

Thanks!

hashnode-footer.png

After reading this story, I hope you learned something new or are inspired to create something new! 🤗 If so, consider subscribing via email (scroll to the top of this page) or follow me here on Hashnode.

Did you know that you can create a Developer blog like this one, yourself? It's entirely for free. 👍💰🎉🥳🔥

If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a DM on Twitter @DevByRayRay when you want to keep it private. My DM's are always open 😁

Top comments (1)

Collapse
 
devto2k5 profile image
dev procedure

Are these best examples, or typical examples on the REASONS to use TypeScript in the REAL WORLD?

A function to "add numbers'? And I seen another example elsewhere to use a function to "divide a number"?

Does that really happen in the real world of programming?

The more I look at the "examples to use TypeScript," the more I see FAKE problems created to justify the use of TypeScript.