Blog post cover
Arek Nawo
30 Nov 2018
4 min read

The Pros, Cons & TypeScript

JavaScript development has recently made a huge step forward in performance, features, syntax and development comfort in general. But still, it leaves a lot to be desired. One of the most important aspects of how good is the programming experience is mainly the editor support (at least for me). Autocompletion or Intellisense (if you are on to these Microsofty-kind-of things) is what I want to focus on. Face it, it’s easier to write something when countless suggestions just bump up from under your cursor.

via Giphy.com

But, as you might expect, and want them to, these cannot be just random things. These are based on types - a term not so popular in JS community. If you don’t know what these are then google it and see you later. So, as you may already know types are awesome! (You may agree or not but I don’t care.) They provide editor support and performance optimizations for typed languages if used correctly. But, (at least as far as I know), JS is not a typed language and it’s kind of one of its main selling points. Of course, untyped languages have their own totally different pros but it’s definitely not the editor support. For the time being it’s not so easy to implement good autocompletion support without types. And because JS isn’t becoming a typed language in the near future, some alternatives have appeared. One of which is TypeScript, a language or as I can call it “a thin typed layer” built on top of JS. This creation, with a name created by a nice game of words, is aiming to provide typed-like language experience without changing the base JS syntax. In addition, it adds support for some syntax sugar available in ES-next.


So… - “Long live TypeScript!” - right?

Well… not exactly. You see, TS looks just great from the top - no new syntax (if not counting the types, of course), editor support (VS-Code especially) and “backward” compatibility with JS.

via Giphy.com

You know what it means, right?

Backward compatibility is a property of a system, product, or technology that allows for interoperability with an older legacy system, or with input designed for such a system, especially in telecommunications and computing.

Wikipedia

In this situation, JS isn’t old or legacy - it’s not important. The word interoperability is the key. In TypeScript, you can import the JS library, but it won’t obviously magically generate typing for this particular code. That may not seem so, but becomes quite problematic when you want to use TS “full-potential” in its strict mode. Then you get some nifty errors (unless you hack your way through TS configuration) and no type-safety, which is pretty bad for my lacking. But don’t worry d.ts (TypeScript Declaration) files are here to help! Of course only if you’ve got time to do a thing such as writing TS binding for some enormous library. Well, you’re not alone here as well. Say hello to DefinitelyTyped where other people sharing the need with you have already covered many popular JS libraries. Sadly, not all (so unexpected) and that’s the main problem. You can type something that is untyped from the beginning without doing anything. But apart from this and the fact that TS type system lack some (not really needed but helpful) features, then yes - “Long live TypeScript!” and actually, this statement is becoming reality.


Other options

At the time of writing this post, there are only two main competitors to TypeScript - Flow & JSDoc itself. As this wasn’t meant to be about them, I’ll make it clear:

Flow

JSDoc


Personal preference

Personally, since I discover TypeScript I can’t stop using it. The editor autocompletion support it provides is just mandatory for me. I tried to use Flow and JSDoc but honestly these weren’t any better than TS (just my opinion). Of course, lack of typings for some libraries can be problematic but it isn’t really something I can blame TS for. So my recommendation? Just pick one of the mentioned projects, whether it’ll be TypeScript, Flow or just JSDoc if you haven’t already and if you have, then don’t forget to share this post to those who haven’t. Have a nice, typey day.

If you need

Custom Web App

I can help you get your next project, from idea to reality.

© 2024 Arek Nawo Ideas