Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

TypeScript: Industrial-strength JavaScript

Jon Udell | Jan. 20, 2015
The TypeScript language is most succinctly described as an optionally typed superset of JavaScript. Since existing JavaScript is valid TypeScript, you can begin using the TypeScript compiler — and TypeScript-aware tools — by simply changing filename extensions from .js to .ts.

Historians who reflect on JavaScript's emergence as a dominant programming language in the 21st century may find themselves quoting Donald Rumsfeld: "You go to war with the army you have, not the army you might wish to have."

For growing numbers of programmers, JavaScript is the army we have. As we send it into the field to tackle ever more ambitious engagements both client and server side, we find ourselves battling the language itself.

JavaScript was never intended for large programs built by teams that use sophisticated tools to manage complex communication among internal modules and external libraries. Such teams have long preferred strongly typed languages like Java and C#. But those languages' virtual machines never found a  home in the browser. It was inevitable that JavaScript alternatives and enhancements would target the ubiquitous JavaScript virtual machine.

Among these is TypeScript, the brainchild of Anders Hejlsberg. I would not have predicted that the creator of Turbo Pascal and C# would, in 2015, be working in the open on GitHub, for Microsoft, to strengthen the Web's native programming language. But it's a delightful outcome. TypeScript is an important project that delivers useful benefits now and is moving forward nicely.

The TypeScript language is most succinctly described as an optionally typed superset of JavaScript. Since existing JavaScript is valid TypeScript, you can begin using the TypeScript compiler — and TypeScript-aware tools — by simply changing filename extensions from .js to .ts.

That turns out to be a good way to start. TypeScript can be immediately useful, even before you add any type annotations to your own code, thanks to the type definitions collected at definitelytyped.org. The definitions available there, for hundreds of JavaScript libraries (including jQuery, Angular, and Bootstrap), describe the interfaces exported by those libraries. TypeScript-aware tools use those definitions to flow type awareness into TypeScript programs that import the annotated libraries.

That same kind of type awareness is also available, in a portable way, for JavaScript's core library and for the browsers document object model (DOM). It's portable because TypeScript is written in TypeScript, targets JavaScript VMs on any platform, and provides not only a compiler but also a set of language services that support code intelligence. Visual Studio is one consumer of those services. Others include WebStorm, Eclipse, and Sublime Text. There's also the TypeScript playground, an interactive Web page that completes statements, prompts for parameters and their types, and warns about type errors.

Second time's the charm

When I took my first run at TypeScript in 2013, I wanted to stick with the language but didn't for a few reasons. The compiler was a bit slow back then, and a delay of even a few seconds frustrates a JavaScript developer accustomed to instant results. Type definitions were scarcer and less mature. Plus, some basic JavaScript idioms required workarounds.

 

1  2  3  4  Next Page 

Sign up for CIO Asia eNewsletters.