0 comments

I had the privilege of attending the ANUG User Group event at GOTO this afternoon, and there to present the latest addition to our web language toolbox, was Anders Helsberg. The topic of the talk was a new language called TypeScript. 


What is TypeScript?

TypeScript is at its core a language that enables enterprise-scale application development in JavaScript. Because it is a superset of JavaScript, TypeScript is JavaScript. We aren't being forced to re-learn all the things we already know about the language. 

Some of the key points I picked up from the talk are: 

  • TypeScript is a superset of JavaScript
  • Provides typed parameters 
    function greeter(person: string) {
        return "Hello, " + person;
    } 
  • Provides "interface types" 
    interface Person {
        firstname: string;
        lastname: string;
    }
    
    function greeter(person : Person) {
        return "Hello, " + person.firstname + " " + person.lastname;
    }
    
    var user = {firstname: "Jane", lastname: "User"};
  • Optional parameters
    function Greeter(name?: string) {
        ... 
    }
    
  • Class definitions
    class Greeter {
    	greeting: string;
    	constructor (message: string) {
    		this.greeting = message;
    	}
    	greet() {
    		return "Hello, " + this.greeting;
    	}
    }   
    
    var greeter = new Greeter("world");
     
  • At compile time, all type info is erased and thus you are left with pure JavaScript. The type info is used solely as a help to you and your tools during development time.
  • Type information for libraries like jQuery, underscore etc. is provided through type declaration files. A few are shipped with the product, and Microsoft expects that the community will be helpful in building declaration files for all the commonly used libraries. 
  • Class definitions are based on the ECMAScript 6 proposal
  • You can provide default values for parameters
  • Lexical scoping of the "this" keyword using the fat arrow (=>)
  • Supports the module concept
    module Sayings {
        export class Greeter {
            greeting: string;
            constructor (message: string) {
                this.greeting = message;
            }
            greet() {
                return "Hello, " + this.greeting;
            }
        }
    }
    results in this code:
    var Sayings;
    (function (Sayings) {
        var Greeter = (function () {
            function Greeter(message) {
                this.greeting = message;
            }
            Greeter.prototype.greet = function () {
                return "Hello, " + this.greeting;
            };
            return Greeter;
        })();
        Sayings.Greeter = Greeter;    
    })(Sayings || (Sayings = {}));
     
  • An important point is that the compiler never changes method bodies. They are left untouched. This means you don't have to worry that the compiler does things to your code that you don't expect. 

All in all I think this looks really exciting. I must admit I was a bit skeptical when I heard that Anders Hejlsberg was going to announce a new language that compiles to JS. My first thought was that this was either a new CoffeeScript or a new Dart. But in reality, this is neither of those two. This language provides us developers with some tools to help us with some of the "thousand paper cuts" that JavaScript development can be. For example, refactoring becomes a lot easier since the compiler is context-aware, and because Visual Studio continuously compiles the code in the background. This means we can rename variables without risking renaming the wrong ones. 

If you want to have a look at TypeScript, head over to http://typescriptlang.org and download it. And the best part is that it's open source under the Apache 2.0 License. 

comments powered by Disqus