Why Your Next Node Application Should Use TypeScript

Omer Morad
NI Tech Blog
Published in
5 min readApr 21, 2020

In my previous article, I was suggesting a strategy for a full transformation from JavaScript to TypeScript in a large-scale R&D operation.
In this article, I discuss the conclusions we’ve drawn as a company while writing microservices with TypeScript and explain why using it for your next Node application is an excellent idea.

Server-Side JavaScript

Nobody thought one day JavaScript would go over so many life cycles and end up as a full robust runtime environment that enables writing code not only for the browser but also for servers, desktop applications, and even mobile applications.

We can’t ignore the popularity of NodeJS (Node) and the domination it has over the technical landscape. The event-driven architecture and asynchronous I/O design enable Node to optimize throughput and scalability in web applications with many I/O operations, and this is how winners are born. But for me, writing server-side JavaScript was still a little bit weird.

Change the technical landscape — NodeJS

Let’s not forget that JavaScript was initially intended for the scope of a single page, and some could even say it’s main weaknesses are also its greatest strengths. Still, when it comes to server-side programming — yet it’s most significant strengths make it challenging for a server-side solution. Elements like classes, classical inheritance, contracts, and type-safety are essential if you want to write large-scale codebases and primarily when you work in teams where massive refactoring is inevitable.

Other Solutions

After conducting extensive research, we found that different companies like Google, Facebook, and Microsoft came up with different solutions trying to compensate for JavaScript weaknesses. While researching, we discovered Dart, JS++, Flow, and some other variants of type-safety solutions, but we ended up considering two options: TypeScript or ES6+Flow.
As the title of this article implies, we decided that TypeScript is the ultimate choice for our needs.

Motivation for TypeScript

The three main factors that influenced our decision were smooth refactoring, easy-to-manage codebase in teams, and a vast community.
We have noticed that other languages are not as popular as TypeScript is; there is no broad community and also the chance of finding good developers is not very high.

TypeScript makes refactoring incredibly easier to handle and to work in teams. It also makes it easier for other developers to move in and out of the codebase without unintentionally breaking things, especially when it comes to large-scale projects with many developers involved.

Long story short, TypeScript builds on the strengths of JavaScript while improving on its weaknesses.

Moving forward to TypeScript

What is TypeScript?

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

TypeScript compiles to clean, simple JavaScript code which runs on any browser, in Node.js, or in any JavaScript engine that supports ECMAScript 3 (or newer).

The goal of TypeScript is to help catch mistakes early through a type system and to make JavaScript development more efficient.

How TypeScript Helped Us To Achieve Our Goals

Support for modern JavaScript features

TypeScript allows for the use of many of the latest ECMAScript features. It translates them to older ECMAScript targets of your choosing; this means that you can safely use new features, like modules, lambda functions, classes, the spread operator, and destructuring.

Advanced Type System

TypeScript is a comprehensive, typed, and multi-feature superset of JavaScript. It’s a programming language of its own built to provide features missing in JavaScript like static types, generics, interfaces, enums, decorators, union/intersection types, access modifiers, and the list goes on. Type-safety can help catch potential errors earlier in the development cycle, and TypeScript supplies compile-time type validation, it means it will not allow the application to be appropriately compiled if the typing is incorrect.

Enhanced IDE Support

TypeScript has excellent IDE support. The development experience with TypeScript is a significant improvement over JavaScript. Thanks to the TypeScript compiler, the IDE can easily integrate with the language features, it’s informed in real-time about rich type information. That gives you the ability to do refactorings like renames across your entire codebase safely.

The majority of IDEs (Visual Studio Code, WebStorm, Atom, and Sublime) fully support TypeScript assistance tools like syntax highlighting, semantic highlighting, IntelliSense, JSDoc, methods signature help, auto imports, and code formatting. Through code completion, you can get inline help on whatever functions a library might offer — no more need to remember them or look them up in online references.

Contracts

One of TypeScript’s core principles is interfaces, sometimes called “duck typing” or “structural subtyping”. Interfaces fill the role of naming these types and are a powerful way of defining formal, precise, and valid contracts inside the project as well as outside of it.
When it comes to extensive codebase management — the larger it grows, the more necessary it is to have substantial contracts between different parts of the code.

JavaScript Interoperability

In case you need to use external JavaScript packages, you can use declaration files. Declaration files are just files that describe the shape of an existing JavaScript codebase to TypeScript. By using those files, you can avoid misusing third-party JavaScript libraries. There is an excellent Github repository that contains all the third-party libraries declaration files, which is called DefinitelyTyped. The project is community-driven but supported by the TypeScript team as well. You can read more about it in TypeScript’s blog.

Robust Frameworks

Ts.ED, LoopBack, and NestJS are just a few of the TypeScript frameworks for Node. They make use of all the language’s most robust features (like classes, interfaces, and decorators); thus, they enable high scalability and flexibility and get the best out of the language. They also combine Object-Oriented programming with a variety of design patterns.

Bonus: Our Choice — NestJS Framework

Scalable server-side applications with NestJS

Nest (NestJS) is a progressive Node framework used to build scalable server-side applications. Nest is heavily inspired by Angular architecture with the same concepts as Pipes, Interceptors, Providers, and Controllers. It created with and fully supports TypeScript and combines elements of OOP, Functional Programming, and Reactive Programming.
As a philosophy, Nest provides no logic of its own; it’s an architecture friendly thin wrapper over existing frameworks (like Express and Mongoose, for example). It provides a level of abstraction above those frameworks but also exposes their API directly, which allows the freedom to use the myriad of third-party modules that are available for the underlying them.
The ecosystem is excellent, too — there is growing community support, and the Nest team even suggests a suitable course with 60+ chapters walkthrough of all the fundamentals of a Nest application.

Final words

I don’t think all of you should embrace TypeScript right away and in any case. Sometimes Vanilla JavaScript can do great work too. However, if you know, there is a large project ahead, and even more so, if a team will develop it, the smart choice is TypeScript.

References

Lodewijk Bogaards — What is TypeScript and why would I use it in place of JavaScript

TypeScript vs. Flow by Milos Protic

--

--

Omer Morad
NI Tech Blog

Software Developer, Guitar Player. I’m a Technophile who and OpenSource (OSS) Enthusiast :)