Last Updated: May 25, 2017
·
11.44K
· timpietrusky

TypeScript from Microsoft

Microsoft released a new programming language named TypeScript which is a superset of JavaScript. So unlike CoffeeScript you don't have to learn any new syntax, because TypeScript just extends JavaScript with some features:

  • Type annotations
  • Classes
  • Interface
  • Modules
  • Pointer function

The project is Open Source (Apache License) and you can compile the code with the TypeScript compiler for Node.js.

Example: Create a class

TypeScript

class Greeter {
    greeting: string;
    constructor (message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}   

JavaScript

var Greeter = (function () {
    function Greeter(message) {
        this.greeting = message;
    }
    Greeter.prototype.greet = function () {
        return "Hello, " + this.greeting;
    };
    return Greeter;
})();

Example: Interface

TypeScript

// Definition
interface Picture {
    src: string;
}

function getUrl(myImg: Picture) {
    return 'http://i.mg/' + myImg.src;
}

JavaScript

You don't need to specify that you implement the interface, it just needs to match the interface’s definition!

var hey_jeah = {src: 'a_leet_img.svg'},
    hey_jeah_url = getUrl(hey_jeah);

Further reading

Official page
http://www.typescriptlang.org

TypeScript: JavaScript Development at Application Scale
http://blogs.msdn.com/b/somasegar/archive/2012/10/01/typescript-javascript-development-at-application-scale.aspx

The obligatory TypeScript reaction post by Mark Rendle
http://blog.markrendle.net/2012/10/02/the-obligatory-typescript-reaction-post/

2 Responses
Add your response

Well I think, that what makes CoffeeScript so sexy is its syntax (and brevity) and that's the reason most developers decide choose it.
On the other hand, the biggest problem with CS is its incompatibility with ES6 syntax, using same keywords but with different meanings (take class for an example). In other words - CoffeScript is not futureproof. How does TypeScript handle that?


PS I really DO like TypeScript, but somehow coding in it feels like going back to my ActionScript times.

over 1 year ago ·

I like TypeScript a lot. I come from C# background and haven't really used JS a lot, just for a few client-side tweaks. I find TypeScript very close to C#.
TS incorporates proposed ES6 changes in JS so if ES6 really adopts all those features, JS will eventually become TS itself. In other words, there may not be any difference in TS and JS after features adoption.

PS - Please correct me if I am wrong.

over 1 year ago ·