Developing with TypeScript 2.x

TypeScript

I am back with TypeScript, and as you might know, it is one of my favorites languages (Javascript is the one that I hate the most). Although I wrote several entrances related to the basic (and not so basic) of this language, including CATS, an IDE written in TypeScript, I wrote also how to include jQuery, Bootstrap, and I even show you what my template looks like.

Time has passed by, and I keep on refreshing my knowledge on TypeScript, focus on new versions.

As you can imegine, all previous entrances have been written while I was learning it, solving small exercises to later on, apply it on bigger developments. It has not been an easy task, because some steps took me a lot of effort. The most, RequireJS, a library you must know for large developments.

TypeScript 2.0

There is a new update in TypeScript a few months ago with cool new features (you can read here at Microsoft’s blog). The evolution of TypeScript is not casual, and the language has been design with a lot of effort and inserting previous experiences, making TypeScript into a language to sort out Javascript. for me, it is important the type of variables, and this aspect has done to a better understanding of Javascript, and make better code with lees bugs and errors.

The challenge of TypeScript 2.x is being close to EMACScript, the new versión of Javascript, and in orer to do that, updating has been neccesary. In April 2017, you can work in production with versión 2.2, and in Septembre 2017, version 2.3 is perfectly working. But the important features appear since versión 2.0.

the first big change is how to get definition files, mainly from DefinitelyTyped, just by using the Node console:

npm install –save @types/librery

Another important change is the not null types, avoiding the bug problem that Javascript has with null and undefined. Now, simple types cannot be null or undefined, and in order to admit those values, you need to redefine them by using join of types.

var variable: string | null

Las important change is control flow analyzed types.

Personally, the use of let instead of var when declaring variables is an improvement to must apply to your code, because the declarations with let allow the developer to have more control of the environment of a variable, only being able to work insede the brackets where they are.

for (let x = 0, x<5 , x++)

{ console.log(x) }

// console.log(x) da ERROR

To end with, classes in TypeScript allow you to create read only properties just by adding readonly . The new way of modifying values is by using the set method, clase.set_property(newvalue), where set_property if a new functions that must be defined by the user. You can think it is wierd, but it is not. The goal of this feature is to avoid value modifications just by writing clase.propiedad = newvalue.

TypeScript as a language

since I did the course of TypeScript and I understood the advantages of the language, TypeScript is one of the few language I normally use when developing (of course, Python is the one I use the most). As a developer, TypeScript has change my life because Javascript was too complex and wierd when used on large projects, and although I spent more time learning, now I am confident with it, and it is the language I use when developing web apps at work. I didn’t spend my time, for sure!

And, that’s all for now. Happy coding!