Microsoft Delivers TypeScript: A Better JavaScript for Big Apps

Microsoft does JavaScript one better with a new programming language, TypeScript, that enables JavaScript developers to build application-scale apps. The TypeScript project was led by Anders Hejlsberg, the father of Microsoft’s C# language.

Microsoft has announced a new programming language, known as TypeScript, which is aimed at being a better JavaScript for building large-scale, enterprise-class applications.

Microsoft aims to tackle big problems for developers, and taking down one of the major knocks against JavaScript is a big deal. So the company enlisted some big names led by Ander Hejlsberg, father of the C# language, Steve Lucco, creator of Microsoft’s “Chakra” JavaScript engine, and Luke Hoban, a programming language expert now focused on ECMAScript standards and JavaScript development.

Microsoft has taken strong strides in its development of TypeScript, including ensuring it adheres to ECMAScript standards, making it easy for developers to use, and opening up the language, just to name a few.

“We want to see what the community has to say,” Hejlsberg said.

As Soma Somasegar, corporate vice president of Microsoft’s Developer Division, said in a blog post on TypeScript, Microsoft is “introducing a new programming language that solves a very specific problem—getting JavaScript development to scale.” He adds that you can learn more about the TypeScript project, download the bits, read and discuss the language specification, explore the online playground and peruse the source of the compiler on the TypeScript project site on CodePlex.

Somasegar spells out the Microsoft strategy. “In addition to supporting industry-standard programming languages, such as C++, Python and JavaScript, Microsoft has always been at the forefront of creating great programming languages—Visual Basic, C#, and F# being the most recent examples,” he said. “We create programming languages to solve problems and to enable a broad set of people to build software.”

Moreover, Somasegar wrote, “During the past five years, JavaScript speed has doubled every nine months (as measured by the WebKit SunSpider JavaScript benchmark). With HTML5, the standard Web platform has become significantly more compelling for delivering rich user experiences. At the same time, the reach of JavaScript has continued to expand, going beyond the browser to include native device apps (e.g. Windows Store apps for Windows 8), applications in the cloud (e.g., node.js running on Windows Azure), and more. With these developments, we’re starting to see applications of unprecedented size written with JavaScript, despite the fact that creating large-scale JavaScript applications is hard. TypeScript makes it easier.”

Hejlsberg told eWEEK that over the last five years Microsoft has increasingly heard from customers and internal teams that it is increasingly hard to build application-scale JavaScript solutions. And there are several reasons for that, he said. “JavaScript was invented as a scripting language and never really had any of the large-scale code structuring concepts like classes and modules. But also JavaScript is entirely dynamically typed, which means it lacks the static typing information that really is what powers today’s full-featured IDEs [integrated development environments] with things like refactorings and statement completions, find all references, rename, etc.”

To overcome some of the issues with JavaScript, "developers (including some teams at Microsoft) have taken to using tools like Script# for static typing, jslint and other lint tools for code checking, and the closure compiler to define explicit contracts between components," Somasegar said. “They’ve also adopted best practices like the module pattern to achieve encapsulation.”

So two or three years ago when Microsoft was thinking about where to go next with JavaScript development tools, “we thought it would be really valuable if we could strengthen JavaScript with these missing features for application-scale development—classes, modules, static typing—without sacrificing any of the benefits that are inherent to JavaScript—open standards, Web, cross-platform, works anywhere. And that’s what TypeScript is about. It’s basically a language for application-scale JavaScript development. Or if you go the next level down, it is a typed superset of JavaScript that compiles to plain JavaScript. And that JavaScript and the compiler itself works on any browser, on any host, on any OS. And the compiler and any associated language services are all open-source.”

Indeed, Microsoft says TypeScript starts with JavaScript.

“All JavaScript code is TypeScript code—you can just copy and paste JavaScript into a TypeScript file,” Hejlsberg said. “And all existing JavaScript frameworks work with TypeScript. So Node.js, jQuery, Underscore.js, Backbone.js, Express, Prototype—there’s a new one every day—all work with TypeScript. On top of that, TypeScript adds optional static types and classes and modules, and those are things that empower tools to be great at application-scale development. But these concepts then compile away. So when you compile TypeScript code, we produce idiomatic JavaScript code—basically the JavaScript code that you would have had to write in order to do class-based programming or in order to do modules. And that final code runs in any browser, in any host, on any OS.”

In a demo, Hoban showed the TypeScript Playground where you can experiment with TypeScript. He also demonstrated how TypeScript begins with JavaScript, showing how you can add type annotations and classes to JavaScript and how code completion works in the language.

TypeScript allows us to provide rich tooling experiences, like code completion,” Hoban said. “TypeScript is available as a plug-in for Visual Studio, but it’s also available as an NPM (Node Package Manager), which allows you to run the TypeScript compiler in any environment where Node.js is supported—which includes Mac OS, Linux and Windows. We’ll be providing a plug-in for Visual Studio that provides some of this rich tooling experience inside the Visual Studio 2012 toolset.” Hoban also demonstrated an example of an ASP.NET MVC 3 application leveraging TypeScript and using jQuery.

But why do Typescript when Google has been working on Dart, which attempts to solve many of the same problems and is championed by Gilad Bracha, who often speaks about the shortcomings of JavaScript. Dart is an open-source Web programming language developed by Google. It was unveiled at the GOTO conference in October 2011. The goal of Dart is "ultimately to replace JavaScript as the lingua franca of Web development on the open Web platform,” Google said. Dart is intended to solve JavaScript's problems while offering better performance, the ability "to be more easily tooled for large-scale projects" and better security features. Google is also working on Dart to help them build more complex, full-featured client-side Web apps.

However, TypeScript is a better fit for JavaScript developers, Hejlsberg said. “It’s a completely different design point with completely different design goals,” Hejlsberg told eWEEK. “Dart ultimately is about replacing JavaScript. It’s a new language and a new virtual machine. You can cross-compile to JavaScript, but when you do, you’re basically taking one language's semantics—the semantics of Dart—and trying to emulate that on JavaScript. Just like Script# or GWT tried to emulate C# or Java on JavaScript. And associated with that is some cost and some complexity.”

Furthermore, Hejlsberg said all the existing JavaScript libraries out there don’t work with Dart, “unless you cross-compile to JavaScript and unless you first provide some sort of Dart view or encapsulation of these libraries. And this contrasts with TypeScript where, first of all, we start with JavaScript, so you don’t have to go relearn a new language; you just have to learn how to put type annotations on your parameters, and that’s not all that hard to learn. Second, we align extensions with ECMAScript 6, so the classes, modules, etc., that we implement in TypeScript are aligned with where the ECMAScript 6 committee is going with the JavaScript standard. So even learning TypeScript, you’re actually learning what ultimately will be JavaScript. And all of these third-party libraries work with our technology.”

Meanwhile, TypeScript is open. The TypeScript language is made available under the Open Web Foundation’s OWFa 1.0 Specification Agreement, Somasegar said. “And we invite the community to discuss the language specification,” he added. “Microsoft’s implementation of the compiler is also available on CodePlex under the Apache 2.0 license. There you can view our road map, and over the next few weeks and months you’ll see the TypeScript team continue to develop on CodePlex in the open.”