TypeScript Tools and Framework Options
JavaScript is a truly ubiquitous language—the more you look, the more JavaScript you find running in the most unlikely of places. Just about every website that you visit in the modern world is using JavaScript to make the site more responsive, more readable, or more attractive to use. Even traditional desktop applications are moving online. Where we once needed to download and install a program to generate a diagram, or write a document, we can now do all of this on the web, from within the confines of our humble browser.
This is the power of JavaScript. It enables us to rethink the way we use the web. But it also enables us to rethink the way we use web technologies. Node, for example, allows JavaScript to run server-side, rendering entire large-scale websites, complete with session handling, load balancing, and database interaction. This shift in thinking regarding web technologies, however, is only the beginning.
Apache Cordova is a fully-fledged web server that runs as a native mobile phone application. This means that we can build a mobile phone application using HTML, CSS, and JavaScript, and then interact with the phone's accelerometer, geolocation services, or file storage. With Cordova, therefore, JavaScript and web technologies have moved into the realm of native mobile phone applications.
Likewise, projects such as Kinoma are using JavaScript to drive devices for the Internet Of Things (IoT), running on tiny microprocessors embedded in all sorts of devices. Espruino is a microcontroller chip purposefully designed to run JavaScript. So, JavaScript can now control microprocessors on embedded devices.
Desktop applications can also be written in JavaScript, and interact with the filesystem using projects such as Electron. This allows a write-once, run on any operating system approach, out of the box. In fact, two of the most popular source code editors, Atom and Visual Studio Code, have been built using Electron.
Learning JavaScript, therefore, means that you have the ability to build websites, mobile phone applications, desktop applications, and IoT applications to run on embedded devices.
The JavaScript language is not a difficult language to learn, but it does present challenges when writing large, complex programs, particularly in a team working on a single project. One of the main challenges is that JavaScript is an interpreted language—and therefore has no compilation step. To check that all written code has no minor mistakes means that you have to run it in an interpreter. It has also been traditionally difficult to implement object-oriented principles natively in the language, and it takes great care and discipline to build good, maintainable, and understandable JavaScript. For programmers that are moving from other strongly typed, object-oriented languages, such as Java, C#, or C++, JavaScript can seem like a completely foreign environment, especially when targeting earlier versions of JavaScript.
TypeScript bridges this gap. It is a strongly typed, object-oriented language that uses a compiler to generate JavaScript. The compiler will identify errors within the code base even before it is run in an interpreter. TypeScript also allows you to use well-known, object-oriented techniques and design patterns to build JavaScript applications. Bear in mind that the generated JavaScript is just JavaScript, and so will run wherever JavaScript can run—in the browser, on the server, on a mobile device, on the desktop, or even in an embedded device.
This chapter is divided into two main sections. The first section is a quick overview of some of the benefits of using TypeScript, while the second section deals with setting up a TypeScript development environment.
If you are an experienced TypeScript programmer, and already have a development environment set up, then you might want to skip this chapter. If you have never worked with TypeScript before, and have picked up this book because you want to understand what TypeScript can do, then read on.
We will cover the following topics in this chapter:
- The benefits of TypeScript:
- Compilation
- Strong typing
- Integration with popular JavaScript libraries
- Encapsulation
- Public and private accessors
- Setting up a development environment:
- Node-based compilation
- Visual Studio Code
- Visual Studio 2017
- WebStorm
- Other editors and Grunt