The Good and the Bad of TypeScript
Popular as it is, JS doesn’t come without drawbacks. The major concern is that this language wasn’t initially meant for building enterprise-level applications. And that’s where TypeScript steps in. Kin to JS in terms of syntax and semantics, it brings a number of additional tools that increase productivity and facilitate creating large codebases.
For this reason, Google chose TS to build Angular, targeted at large-scale applications, and made it the primary language for developing Angular apps. Microsoft’s solution is also compatible with all JS libraries and frameworks and can be used to create software with React, Vue, or any other technology within the JS ecosystem. This versatility along with great tooling makes TS one of the most loved languages, according to the 2019 Developer Survey by Stack Overflow.
The 2019 list of most loved languages. Source: Stackoverflow.
There are two quick ways to get started with TS, suggested by its official website:
- via NPM — Node.js package manager.
- by installing plugins for Visual Studio, a full-fledged IDE from Microsoft.
The following tutorials will help you dive deeper into TypeScript and learn how to use it:
Overview — descriptions of all existing TS releases (up to the 3.7 version).
React & Webpack — the short introduction to teaming up TS with React and webpack.fun
Handbook — tutorials covering all essential concepts of TS, including types, variable declarations, classes, functions, generic, enums, etc.
Declaration files — a comprehensive guide to writing a high-quality TypeScript Declaration File.
Project Configuration — manuals that give answers on questions about compiling and integrating with build tools like Babel, Browserify, Grunt, and others.
With all the above in mind, let’s inspect the advantages offered by TypeScript in more detail.
TypeScript pros: what makes TypeScript a good fit for large projects
Optional static typing
TypeScript introduces optional strong static typing: once declared, a variable doesn’t change its type and can take only certain values. The compiler alerts developers to type-related mistakes, so they have no chances to hit the production phase. This results in less error-prone code and better performance during execution.
But static typing is not only about catching bugs. It also gives the code more structure, makes it self-documenting and more readable, speeds up debugging and refactoring, and, all totaled, increases productivity across a large team.
It’s important to note that TS doesn’t force declaring types everywhere. Developers are free to change the level of type strictness in different parts of the project. This approach distinguishes TS from other statically typed languages and allows you to find the right balance between flexibility and correctness.
Early spotted bugs
Researchers found that TypeScript detects 15 percent of common bugs at the compile stage. Far from a 100 percent result, this amount is still significant enough to save developers time and let them focus on correcting mistakes in the logic — rather than catching common bugs. Pushing the code through a compiler also decreases the volume of quality assurance and testing activities.
With TypeScript, everything stays the way it was initially defined. If a variable is declared as a string, it will always be a string and won’t turn into a boolean. This enhances the likelihood of functions working the way initially intended.
Due to adding strict types and other elements that make the code more self-expressive (so-called ‘syntactic sugar’), you can see the design intent of developers who originally wrote the code. It’s especially important for distributed teams working on the same project. A code that speaks for itself can offset the lack of direct communication between team members.
Rich IDE support
Information about types makes editors and Integrated development environments (IDE) much more helpful. They can offer features like code navigation and autocompletion, providing accurate suggestions. You also get feedback while typing: the editor flags errors, including type-related as soon as they occur. All this helps you write maintainable code and results in a significant productivity boost.
With Microsoft Visual Studio as the most popular and natural environment for running TypeScript, it is also supported by many other EDIs including:
- Eclipse, an integrated IDE offering a plugin for TS development,
- Visual Studio Code, a lightweight cross-platform editor by Microsoft,
- Atom, a cross-platform text editor,
- CATS, an open source TypeScript development environment.
Refactoring or updating the app without changing its behavior is needed to keep the codebase robust and maintainable. TypeScript makes this important process less painful. With IDEs knowing much about your code, you are equipped with navigation tools like “find all references” or “go to definition”.
Besides, a lot of mistakes are spotted automatically. For example, if you rename a function and later forget to change the name somewhere, TS will alert you to the issue. This simplifies and accelerates refactoring, which is especially beneficial when you deal with large portions of the codebase.
Power of OOP
TypeScript supports concepts from class-based object-oriented programming (OOP) like classes, interfaces, inheritance and more. The OOP paradigm facilitates building a well-organized scalable code, and this advantage becomes more obvious as your project grows in size and complexity.
Cross-platform and cross-browser compatibility
Huge talent pool
The number of active software developers detected in 2019. Source: Developer Economics.
Support from the tech world
TypeScript is an open-source language with a steadily growing community. The very fact that it is loved by millions of developers and backed by Microsoft gives confidence that the technology won’t be abandoned and you always can seek (and get!) assistance from the wide TypeScript community. Many popular software products — such as Asana and Slack — switched to TypeScript to manage and maintain their large codebases.
TypeScript cons: what problems it creates
There is nothing perfect in this world, and TypeScript is not an exception. For the most part, its weaknesses originated from its own strengths.
Not true static typing
In the previous section we admitted that it would be relatively easy for JS developers to learn TS. However, despite this similarity, they still need to invest time and effort in learning types and some TS-specific constructs. So if your company plans to move to TypeScript and have JS devs with no previous experience with TS, they won’t hit the 100% productivity immediately.
Adding extra step — transpiling
It’s getting better all the time
TypeScript continues to evolve, with its 3.8 version coming soon. At the time of writing, the release candidate was already available, and the production release will come with the same set of upgrades plus some critical bug fixes. So what major improvements does the fresh version bring?
New syntax for type-only imports and exports. This update is supposed to give developers more control over how the compiler imports and elides (omits) some elements.
By default, ES classes are public, which means they can be accessed and modified outside the class. The implementation of private fields — data slots, available from inside the class only — enables programmers to create a kind of a protective cover, secluding internal details and preventing unwelcome interventions. This feature is already supported in Node.js 12, Chrome 74, and Babel.
“Fast and Loose” incremental checking. The new compiling option reveals if the file needs rechecking or rebuilding, based on changes made to it. The update is supposed to reduce build time and thus favor large projects.