My introduction to TypeScript

I started exploring TypeScript when I started my experimentation with Angular2. Angular2, one of the awesome front end developer frameworks out there by Google, which is taking the front end web development world by storm. I started my experimentation with Angular2 during one of my projects. As soon as I entered the Angular 2 website, I was caught up with the work TypeScript. I got a bit curious on knowing what this is all about. So I started exploring.

So, what is TypeScript?

To keep things in a simpler manner, TypeScript is a much more mature way of writing JavaScraipt. TypeScript enables JavaScript developers to adopt more mature patterns of programming in the JavaScript environment.JavaScript language even though supports OOPS concepts, the pattern was entirely different and tricky from what the other more mature OOPS programming languages like Java, .Net, PHP use. Some of the confusing OOPS concepts in JavaScript are as follows

  • No Class keyword. Still classes are there in the language
  • Function represent both classes and functions based on the context in which they are used
  • Functions can be assigned to variables and behave as variables
  • No well-defined package and module based programming patterns
  • Inheritance, Polymorphism parts are very tricky and very confusing for new comers from other OOPS languages

Why TypeScript?

Microsoft introduced TypeScript to enable smoother transition of the developers from other OOPS programming environment to the JavaScript world. In the recent years, JavaScript saw an unprecedented growth in its usage. More number of JavaScript frameworks and libraries are being released and updated every day in GitHub. JavaScript is the most widely used language in GitHub today. Because this explosive growth in JavaScript other language developers started to flock into JavaScript development. The initial transition to JavaScript was very tough because of the difference of implementation in OOPS concepts in JavaScript. This was a hurdle to most of the developers. In order to smoothen this transition, TypeScript was born.

Features of TypeScript

TypeScript enabled the use of OOPS programming patterns as seen in mature OOPS programming languages in the world of JavaScript. Apart from this OOPS concepts, TypeScript also introduces the following features into the JavaScript world

  • Strict type checking to detect data type related errors during the development phase itself rather than during the runtime which is a huge time saver
  • Class based programming style which is prevalent among other OOPS languages like Java, .NET, PHP, Python etc.
  • Able to interact with the existing JavaScript code without any conversion needed.
  • Compiles to plain JavaScript during runtime that is compatible with all the browser environments.
  • Ability to use ES6 features currently in JavaScript development phase. ES6 features are still in the implementation phase and no browsers support it currently.
  • Ability to opt in and out of type checking which avoids some problems in the case of working with external third party libraries.
  • Ability to integrate well with existing third party JavaScript libraries.
  • Basic JavaScript can be made to behave like TypeScript with the help of corresponding typings file

With so many exiting features, developing modern web applications using TypeScript is awesome. Especially it makes the developers who shifted from other programming languages feel like they are at home, because they can use the same pattern of programming in the JavaScript world too. I came from the PHP domain.

Setting it up

JavaScript is available in your system by default. But you need some setup operations to make TypeScript available in your system. The first and the foremost component is NodeJs. TypeScript is available as a NodeJs Package. The NodeJs website provides the necessary documentation on installing TypeScript in your system either from the installer or compiling from the source files. NodeJs installs a tool called NPM (Node Package Manger), which helps in installing NodeJs packages in our system. Typing the following command in the terminal will install TypeScript in our System.

npm install -g typescript

Once this installation is done, typing the command tsc in the terminal will print some basic info regarding TypeScript and it’s help options. This will ensure that TypeScript is installed in our system. Now for running TypeScript files, we can run the files individually or assign a TypeScript configuration file to a project, that will compile all the containing TypeScript files. It’s up to us and the scope of our project. There are lot of tutorials out there that explains setting up the TypeScript configuration file properly. Once everything is set, invoking the tsc command will convert all the available TypeScript files to JavaScript that runs on all the available browsers, which is awesome.

Conclusion

Currently my exploration with TypeScript continues along with Angular 2. Since TypeScript is a superset of JavaScript, it is applicable in all locations where JavaScript is applicable too. Can’t wait to try TypeScript in almost all my JavaScript Projects.




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *