My Obsession with TypeScript Syntatic Sugar

The last time I tried to immerse myself into the controversial debate on which programming language rocks; I almost lost a limb, literally. Instead of getting irrational, I would rather handpick a specific quality, which makes one language supersede the others and use such quality to argue my choice between any set of preferred programming languages. Say for example you love the procedural programming paradigm; C should come handy. If you are an Object Oriented nerd, Java and C++ are still at your disposal. Nonetheless, these paradigms can shift and overlap so I am not going to dwell much into them but would prefer to spare the energy and use it to yap at my recent obsession for Typescript, a superset syntactic sugar for JavaScript.

Reassembling the Game Changer:

Did you know that the brain behind Mozilla is actually the same brain that brought us JavaScript? Brendan Eich was born in Pittsburgh Pennsylvania and is an Alumnus of both Santa Clara and University of Illinois at Urbana-Champaign. He worked at Netscape where he created JavaScript for the Netscape Navigator Web Browser. It all started as Mocha, and then LiveScript before being coined JavaScript; some prefer to call it ECMAScript. Whichever name you pick, JavaScript has the love of most developers. It has been my greatest companion for front-end hacking. The new ninja (TypeScript) is just an icing on the already sweet cake. It’s static typing ability makes codes easier to read, than you would a magazine.

Strangely as it may sound, I came to know about TypeScript at a time when my KungFu kicks in JavaScript were still harmless to any computer. The training by John Resig unleashed through “Secrets of the JavaScript Ninja” was yet to bear any fruits. I know sometimes my angle of approach to programming languages is just weird. I tend to fight from whichever direction until it somehow works. It feels like trying to get into a conversation halfway and still expect to understand the story. However, I must admit that was it not for my hopping attitude, dangling with hybrid mobile apps, maybe I wouldn’t have known TypeScript existed. The point is, “if you don’t dig, you won’t see the root.”

Syntactic Sweetening

Sincerely speaking, TypeScript is just some syntactic sugar built on top of JavaScript. When you think about syntactic sugar in the context of Computer Science, you have to think about the sweetening aspect of a code. It is some sort of syntax, which can make a code easy to read or express. It simplifies coding by making a sample code more “sweeter” (appealing to humans).

I know the love for JavaScript is wild but as a dynamically typed functional language, I must admit that the challenges in defining classes, integers, strings, numbers etc cannot be overruled. Sometimes I just feel pushed to enforce my own rules with strong typing and TypeScript has always been at the fore of my typing struggles. If you are close and candid with Java, you will love the structure of TypeScript. After all, we still compile the .ts to .js and you can still use plain JavaScript in TypeScript and you will be forgiven. Can I get an Amen?

Sugar Coated Codes:

We have talked too much and it is time to demonstrate what this craze is all about. I will sample .ts alongside the compiled .js code.

Pulling the index and values:

Raw Output:

Icing the For Loop

Iced output

ECMAScript 6 Sugar Coated Class

Look at the contrast on .ts and .js sample codes

Phewks! The Final Class Example:

Output

I hope you enjoyed this article and please don’t log me out yet. Tap the ❤ icon below as a sign of love!

All comments are welcome!!