To Type or Not To Type(Script)

It seems like everyone has an opinion these days about TypeScript. When recently interviewing, I expressed the interest in learning TypeScript, and the advice was “DON’T DO IT!” My nature is to take that as a challenge, and I jumped in to a course hours later. I quickly learned of the bane and the value, and that most people feel VERY strongly one way or the other.

What is TypeScript?

According to Wikipedia:

TypeScript is a programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. TypeScript is designed for the development of large applications and transcompiles to JavaScript. As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs.”

Dynamic Typing: In programming languages such as JavaScript, Python, and Ruby, the process of verifying the type safety of a program happens at runtime. In other words, the type of a variable is only known when the program runs.

Static Typing: In contrast, static type checking is when the type safety of a program is checked as the program’s code is written. The type of a variable must be known at compile time, either by being declared or inferred. The compiler will know whether it is a string, boolean, number, etc. Ideally, if a compiler can assume or prove that a program is well-typed, it does not need to use dynamic safety checks, which can allow the compiled binary to be smaller and run faster. Programming languages such as Java, C, C++, and Swift are statically typed.

So what about TypeScript? What category does it fall into?

TypeScript supports optional static typing, and doesn’t force declaring types everywhere. You can change the level of type strictness in different parts of a program. You can find the sweet spot between the flexibility of dynamic typing and correctness of static typing.

To Type…

There are some very clear benefits of using TypeScript, however controversial.

Bugs

Bugs can be painful. One of the most impressive (or unimpressive, depending on your sentiment) statistics is that a study showed that 15% of all JavaScript bugs can be detected by TypeScript.

Bugs decrease efficiency. It’s that simple.

Worst case, it can bring the lifespan of a program’s development to an untimely demise, especially in server side code in enterprises and large code bases. As their tagline says, TypeScript is JavaScript that scales.

Use TypeScript and say farewell forever to this old friend, “Uncaught TypeError.”

Predictability

In TypeScript, a variable stays the way it was defined when it was created. If it is declared as an integer, it will forever be an integer. Integers won’t turn into strings and strings won’t turn into a boolean. The upside? Functions are more likely to work the way they were intended to work.

Because types catch the silly, little bugs that can creep into code, unbeknownst by the devs, code written with types make it more predictable. There is a very tight feedback loop that is created allowing to correct type errors before the code is even run.

Readability

Finding your way through large, complex systems can be a nightmare. For distributed teams, this can be akin to a night terror. We ultimately all want code that speaks for itself.

Explicitly defining types forces your attention to how the system is built and how each part interacts with the others. Types allow for the ability to abstract away the system while keeping the context in mind. Types make code more self-expressive and explicit. When used well, you can see the design intent of the developers who originally wrote the code.

This is the soi-disant syntactic sugar that makes code sweeter for human use.

Refactoring

Refactoring in TypeScript is easier to do without breaking the code significantly. A lot of mistakes are caught automatically which makes it simpler and faster. The benefits are exponential when working with large parts of a system. Forgetting to change the name of a function somewhere in your code, doesn’t go overlooked.

Refactoring with TypeScript is armed with very useful tools. Since the IDE knows a lot about your code, you can “find all references” and “go to definition.”

TypeScript is a Superset of JavaScript

JavaScript transformed front-end forever. And now, any browser or device that runs JavaScript is also compatible with TypeScript, as it is transformed into JavaScript before it is run.

Because TypeScript is transcompiled into JavaScript, you can use all JavaScript libraries and code that your heart desires in your TypeScript code.

The Definitely Typed is a resource that makes this simple. It provides types for a lot of different JavaScript libraries that you can use to make your interactions with them more type-safe. It allows you to gradually adopt TypeScript in a JavaScript codebase, so go on, give it a try!

So why do some people hate it so much? Stay Tuned for Part 2: …Or Not To Type(Script)!

References

Software Engineer, NYC