Why should we use Typescript instead of vanilla Javascript?

Wandy Santana
NicaSource
Published in
4 min readJul 25, 2022

--

If you’re in the front-end web development business, and unless you’ve been living under a rock for the past few years, you’ve probably heard of TypeScript, the programming language that builds on JavaScript.

TypeScript is a superset that provides several essential features that makes it easier for us to write JavaScript code. It’s more straightforward, consistent, test friendly, and error-free. Since its creation at Microsoft in 2012, TypeScript’s adoption has increased, and it peaked further when Google established it as Angular’s default language.

The Origin of TypeScript.

In 1995, Brendan Eich invented JavaScript; two years later, in 1997, JavaScript became an ECMA standard. ECMAScript is a JavaScript standard to ensure web pages’ interoperability across different browsers.

In 2009, ECMAScript 5 (ES5) was the first significant revision released to JavaScript, where we started seeing arrays, strings, and other powerful features we love using. Years later, ECMAScript 6 (ES6) added new features, such as promises, asynchronous programming, and tokens, and it also introduced a more explicit syntax for object-oriented programming. Both releases leveled up TypeScript’s game to overcome JavaScript’s limitations.

Remember that all the code written in vanilla JavaScript is valid for TypeScript, but not necessarily the other way around. Browsers can’t understand TypeScript natively, so it must be transpile to JavaScript before using it in a browser.

And what are the differences between vanilla JavaScript and TypeScript?

Here are some of the most prominent characteristics we can observe and compare between JavaScript and TypeScript and the main differences.

TypeScript vs JavaScript.Now that we have seen the benefits of TypeScript, I will explain why you should use it instead of vanilla JavaScript.

JavaScript is the language that allows you to do everything as it pleases you. Flexibility makes JavaScript unique in any scenario where it is put to the test. But, like anything too good to be true, it also has some downsides. This long-term flexibility can get overwhelming and lead us into trouble when we continue to scan our project or when we need to refactor some parts of the code that have not been edited for a long time. That’s vanilla JavaScript, the pure will of the code.

One of the things that I like the most when developing with TypeScript is that I feel self-assured when creating a new feature for my app. I know that a given function or variable will work exactly as I expect it from the beginning. I have everything under control, and nothing that was not previously defined will be executed. If you think of it, it can be a real-time saver in development, as errors and possible bugs can be avoided by controlling what goes in and out of the application.

Using TypeScript instead of vanilla JavaScript has many benefits, but one thing that works against it is the learning curve. It won’t be easy if we start at zero or do not have experience with typed languages, this will also result in taking us a little longer to write and implement pieces of code. We must comply with the typing and not miss it if we wish to have a clean, scalable, and, as much possible, safe code. All the benefits mentioned above are great, but we shouldn’t forget that it provides excellent support for tools for development.

I consider tooling is the most important thing to know about TypeScript because it integrates the TypeScript standalone server (aka tsserver), which is a homonymous protocol that wraps the compiler and other language services and exposes them as a service to code editors. Tsserver gives us a powerful resource to validate our code and provides a lot of contextual help, called Intellisense, while we are programming.

Conclusion

TypeScript has excellent benefits for development, but it is crucial to keep in mind that its use is not the best option for all circumstances. For instance, applying it if we are working on a simple, non-scalable project is not recommended. For something small and straightforward, my recommendation is that you should use vanilla JavaScript. If, on the contrary, the project is expected to expand over time, not only in the number of code lines but also in the number of users, TypeScript would be a more suitable choice to build a robust codebase where any developer can jump in and start to collaborate.

--

--