Better JavaScript programming with TypeScript

TypeScript helps you write better JavaScript, tame large JavaScript projects, and prepare for ECMAScript 6 -- and getting started couldn't be easier

Better JavaScript programming with TypeScript

If there’s a lingua franca of the modern development world, it’s JavaScript. Netscape’s browser scripting language has come a long way in almost 20 years and now can be found almost everywhere. It’s at the heart of much of our development tooling, and with server-side implementations like Node.js, it’s also driving much of the move to microservices. JavaScript is even at the heart of much of Microsoft’s development technologies. Want to extend Microsoft Office? You’re using JavaScript. Want to write a Windows 10 UI? You’re using JavaScript.

But JavaScript is not perfect, especially when you’re building a large-scale Web application that includes a lot of client code. That’s where TypeScript comes in. TypeScript is a superset of JavaScript that adds features intended for the upcoming ECMAScript 6, and it can be transcompiled into JavaScript for use in current browsers -- and on Node.js.

TypeScript was designed by a team at Microsoft led by Anders Hjelsberg (perhaps best known for his work on Turbo Pascal, Delphi, and C#). The Microsoft team has designed TypeScript to use a compiler that’s written in JavaScript. You write code in TypeScript and pass it through the compiler. The resulting JavaScript either runs server side or can be called on the client from your HTML.

TypeScript adds a lot of ECMAScript 6 features to JavaScript, including classes and modules, and the team intends to bring the two language dialects closer together as the ECMAScript 6 standard approaches ratification. It’s a sensible approach. You can use TypeScript to get your code ready for ECMAScript 6, while taking advantage of TypeScript’s static typing to make your code safer and more secure.

If you’ve used languages like C or Fortran, you’ll be familiar with static typing, which lets you declare types for variables to ensure, for example, A is always an integer and C is always a string. While TypeScript’s type safety isn’t as comprehensive as, say, Fortran’s, the ability to define number, string, and Boolean types can significantly help with debugging code. There’s also the option of letting TypeScript infer types, reducing the risk of errors when, for example, you assign an object to a string without an appropriate conversion. If your code is adding two numbers together, TypeScript will assume the result is always a number.

You can also use TypeScript to apply types to arrays, or use enums to set values to specific variable names. Enums are a useful tool, as they’re two-way, so you can use them to look up a variable name by number -- giving you a simple way of building a key/value record in your code. If you’re not sure of the type you might be using, you can set a variable to any, in which case TypeScript won’t infer type and you won’t trigger errors or warnings. TypeScript types are optional, so you don’t need to go through your existing code adding types before it will compile or run. That simplifies migrating existing code. You can start with pure JavaScript and add types as you add other TypeScript features.

To continue reading this article register now

How to choose a low-code development platform