What is TypeScript ?

On the 27th of April 2017 you wake to begin your morning routine, Its 4:05 am,so you don’t see it yet. No screen time till 6:00 am. Personal policy. You meditate. You work out, you make breakfast. This time you decide to slide in book reading for a little while in there. You surely haven’t done that in a while.

Its 8:00 am you haven’t checked that phone screen still. Your mind is fixed on getting to the office first. And again you don’t see it. Finally the morning meetings are over and you have your sanity back for what ? An hour? Half an hour maybe before another meeting?

Then you pick up your phone to check for what you believe is a Twitter notification. That’s when your eyes catch it.

Announcing TypeScript 2.3

Today we’re excited to bring you our latest release with TypeScript 2.3!

For those who aren’t familiar, TypeScript is a superset of JavaScript that brings users optional static types and solid tooling. Using TypeScript can help avoid painful bugs people commonly run into when writing JavaScript by type-checking your code. TypeScript can actually report issues without you even saving your file, and leverage the type system to help you write code even faster. This leads to a truly awesome editing experience, giving you time to think about and test the things that really matter.


You wonder what in Gods name? You had just gone through fire with React/Redux, you are still grappling at Vue.js. Somehow you have figured Angular out, you did not really have a choice, you were learning on the job. No results meant no salary. And so you learnt Angular.

You think of the stress you have to go through to learn this new thing. You check up the usage of this strict syntactical superset of JavaScript, a few days later and you find out it has an army of developers behind it. People love this thing? Now as a smart developer you know you have no other job but to keep up with the market as Chad Fowler mentioned in his still very relevant book The Passionate Programmer. I understand perfectly the way you feel. But I personally feel like TypeScript has come to stay.

But what is JavaScript ?

JavaScript the very dynamic programming language that everyone was drawn to because it didnt “house them in” like C++ did. Its untyped nature and its unique prototypal inheritance was perfect bait.

To justify my gut feeling about this just days after TypeScript was released by Microsoft, Angular picked it up and abandoned the plain ES6 that was another available options.

Initially their idea was to go with AtScript, a javascript scripting language that extended Typescript and got published by Google’s AngularJS team exactly two years after the forerunner TypeScript. In fact it was going to run on the TypeScript of course as an extension. 
With all that being said since then a decorator proposal has appeared for ES and it seems to be quite close to what original AtScript did. But before we jump the gun here really. What is this decorator proposal ES utilizes ?

A decorator is:

  • an expression
  • that evaluates to a function
  • that takes the target, name, and decorator descriptor as arguments
  • and optionally returns a decorator descriptor to install on the target object

Read more here. But if you don’t to branch out unto another page until you are done with this article, a decorator is basically an high order function?

An high order function ‘takes in’ another funtion and when it run extends the behavior of the latter function without explicitly modifying it.

UA very simple python decorator

So if our function myFunction(as an higher function) here takes in a function it won’t necessary modify that function, though the output will turn out differently.

One of the first things that put me off with decorators was the ‘@’ sign. It immediately felt like I was reading another language and I know its like thar for a lot of other people too.

Either ways the ‘@` tells the parser that everything (in that block of code) after that sign is a decorator. Sounds easy now? It is simply just talking to the computer directly what is about to happen which is that a function will be decorated by another function and the results will be passed back up.

And the computer says; ‘Fine go on’ . The computer then treats that block of code as a different kind of function, a function that can do things like memoization, authentication, logging and so on.

Again I wont go deep into the details of this for now but when you think of decorators think of them as this instead

A decorator at work

of this train wreck

Imagine you have a couple of functions you want to wrap around themselves. It would be a nightmare to code and maintain such program.

All said and done about ES6 of course you would still have to deal with typing somehow in ES. And TypeScript is a natural fit there(when it comes to typing). You can complement ES with a solution like Flow to get gradual typing after all. Maybe we’ll see two bigger camps. One for ES (Babel guys) and one for TS.

What is Static vs dynamic typing ?

Typing has always been about the variables the program. If I create a static typing language I don’t have to immediately tell the compiler that this particular integer and will always be an integer. It will be exactly what I stored in the program even If I forget while coding and changed its value by passing it to another class or something. A number of good language examples are : C, C++, Java.

Dynamic typing on the other hand is the direct opposite of what the above is about. Ruby, Perl and of course our poster boy. JavaScript. he

This doesn’t mean anyone is inherently weak or bad. And that should be the focus.

Example of Java (Static and Strongly Typed)

There is no way the system can go wrong in evaluating the value of ‘foo’ ever.

Example of Javascript (Dynamic and Strongly Typed)

Opposed to Java or C++, which are both strongly typed languages, Javascript is intelligent enough to know/interpret what variable type you want to use. but because of the lax that this creates or can create in code quality it is also called a weakly typed language.

But we know better. Especially if you follow best practices.

Example of PHP (Static and Weakly Typed)

Obviously this will lead to serious issues down the road. Terrible just terrible.

Between the two up there they are just paradigm ways of coding, it doesn’t mean Static typing is bad or Dynamic typing is good. The devil that should be your sole attention should weak and strong type languages,

Here is what you loose by not coming on a scripting language that is focused on improving the life its users.

  • Generic functions & polymorphism
  • Higher order functions
  • Object composition


Dynamic Types Don’t Mean No Type Checks.

Dynamic Typing is NOT Weak Typing

But what if we combined both sides of the coin, that is that thing that made C++ be C++.

  • Speed: Static languages are faster
  • Efficiency/Effective Static languages make the programmer more accountable. Dynamic programming sometimes creates a moral hazard, and encourages bad programming habits

Here is Javascript Dynamically typed

Here is Javascript Strongly Typed

And that is TypeScript!

You can delve more into Typescript. This is a very brief article talking about these tools in a very peripheral manner. You are free to Google better and deeper examinations of this topic.

Source link