From JavaScript to TypeScript: With Great Precision Comes Great Power

04 Sep 2024

How important is precision in coding? Well, imprecision in coding could cost somewhere in the ballpark of hundreds of thousands of dollars, and in one of the worst cases hundreds of millions of dollars, just as we saw with the Crowdstrike incident for Microsoft this year. While precision in coding is undeniably crucial, clarity is essential across all professions, not just for software developers. Would we want to ride any vehicle whose maintenance was done by someone known to cut corners? The only caveat of this is, of course, the sometimes tedious nature of what we need to do in order to ensure that clarity, and the responsibility of creating quality work as a professional. Enter TypeScript: a powerful extension to JavaScript that has significantly improved the development of countless websites we use today with exactly the goal of being more precise in mind. In my journey of learning TypeScript, it has transformed my approach to coding by enhancing my attention to detail, improving code reliability, and encouraged a newfound appreciation in me for complexity within coding languages.

Enhancing Attention to Detail through TypeScript’s Typing System

One of the key ways TypeScript enhances a developer’s attention to detail is through its strict typing system. In JavaScript, it’s easy to overlook the types of variables, leading to potential runtime errors. TypeScript, however, enforces type annotations, making it clear what types of values a variable can hold. For instance, consider the following example:

In this simple function, TypeScript requires us to explicitly declare that ‘price’ and ‘quantity’ are both numbers and that the function returns a number. If we accidentally try to pass a string or any other type, TypeScript will throw a compile-time error, alerting us to the mistake before it can cause issues in production. This attention to detail not only prevents errors but also encourages developers to think more critically about their code, leading to cleaner and more reliable software.

Ensuring Code Reliability through Explicit Declarations

In addition to enhancing one’s attention through its syntax, TypeScript also functions to ensure code reliability. A program that has hundreds of thousands to millions of lines of code requires that other developers who look at that code can compartmentalize it, and that is precisely because of its explicit declarations that allow others to see its syntax with clarity. We might confuse ‘quantity’ between other data types than a ‘number,’ as it could very well be an array, for instance. Here, there is no confusion as to what the data type could possibly be. That said, the only caveat to this precision is that one has to type more. The tediousness of this is only jarring to new developers learning to code, but while it is unfortunate at that stage, complexity and clarity are great rewards for simply needing to type a little more to ensure quality.

Appreciating Complexity: TypeScript as a Powerful Extension

In learning TypeScript, I’ve come to appreciate the complexity that it adds to JavaScript. JavaScript itself is a rather simple language, and that has its merits, but with the addition of TypeScript syntax, a developer may use the complex tools that it offers—similar to a painter acquiring different brushes to paint with or a cook having different knives to cut with. The addition of these tools, while sometimes requiring a more thorough understanding of how to utilize the options available, is a marked improvement to JavaScript.

Conclusion: The Power of Precision in TypeScript

In reflection on everything I have learned in TypeScript, it is a wonderful incorporation that not only enhances a developer’s attention to detail through its strict typing system but also functions to ensure code reliability. This all, of course, adds complexity to the simple JavaScript language, but more tools to ensure a quality product are always worth it. With great precision comes great power.