The Benefits of TypeScript
:: By Burke Holland, Telerik Platform ::
Context: Typed Systems
In programming languages, a type system is a collection of rules that assign a property called “type” to the various constructs a computer program is composed of, including variables, expressions, functions or modules. The main purpose of a type system is to reduce the possibility of bugs by defining the interfaces that exist between different parts of a computer program and then checking that the parts have been connected in a consistent way.
The fundamental problem with typed systems is that they force developers to know – in advance – the exact format of every incoming data structure they will be working with, which is often impossible. So if strong typing is so difficult, why do so many developers use it? Two words – increased productivity.
An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. Types are what make an IDE more than just a text editor. More often than not, developers are working with code that they did not write, most commonly via the use of a framework. The framework has an API that developers (at least initially) know nothing about. This means that in order to be productive with a framework, developers not only have to know how to code but also how to master someone else’s code.
Learning an entire framework is no small task. This is where the IDE comes in to make it possible to use frameworks as enormous as .NET. Developers don’t need to refer to the string documentation, they just need to start using a string. When that happens, the IDE will let the developer know what methods can be used and will tell the developer what they do without the developer ever leaving the editor and losing context.
And, this is where TypeScript comes in.
Up until now, strongly typed languages were in an all or nothing category. TypeScript is different. It brings the best parts of typing – specifically the increased productivity via IDE integration – and leaves the ridiculous casting part as optional. This is especially useful for those who are developing in NativeScript.
The Value of TypeScript
For those working in NativeScript, Typescript is almost a necessity. In fact, the entirety of the cross-platform modules in NativeScript (tns_modules) are written in TypeScript.
One of the benefits of using TypeScript is it implements quite a bit of ES6 where it can and uses proprietary syntax where the spec is incomplete or missing. ES6 modules allow developers to import any property or method exposed by a module as a single import. This allows developers to cherry-pick exactly what they want from a module.
Not only is the use of ES6 an advantage, but TypeScript also allows developers to know, without a shadow of doubt, that when they run their code it’s going to work. The browser is forgiving, which is incredibly beneficial since native applications are not. Errors aren’t just shelled to the console. They result in nasty app crashes and in the case of mobile specific apps, the app just crashes and doesn’t tell the user anything at all. For developers using TypeScript, this isn’t as much of a concern – the developer already knows quite a bit about the quality of their code by relying on the compiler as a most basic form of unit testing.
Should You Use TypeScript?
However, TypeScript also encourages developers to learn ES6. This is imperative as the importance of ES6 is growing at an exponential rate. TypeScript is a great way to ease into some of the essential ES6 concepts, such as modules.
In any case, TypeScript has made a fan out of me. I know that I prefer to use it in my NativeScript projects, and I’m looking forward to doing more with it, especially as Angular 2 looms on the horizon.
Burke Holland is a Web developer living in Nashville, TN and the Director of Developer Relations at Telerik. He enjoys working with and meeting developers who are building mobile apps with jQuery/HTML5 and loves to hack on social APIs. Burke works for Telerik as a Developer Advocate focusing on Kendo UI.