TypeScript

will finally bring peace
to your troubled soul

Tim Perry
Tech Lead & Open-Source Champion at Softwire
Softwire

TypeScript

JavaScript + types

navigator.geolocation.getCurrentPosition(function onSuccess(position) {
  var lat = position.latitude;
  var long = position.longitude;
  
  var lastUpdated = Date.parse(position.timestamp);
  var now = new Date();
  var positionIsCurrent = now.getYear() === lastUpdated.getYear();
  
  if (positionIsCurrent) {
    var div = document.createElement("div");
    div.class = "message";
    div.style = "width: 100%; height: 100px; background-color: red;";		
    div.text = "Up to date position: " + lat + ", " + long;
    document.body.append(div);
  } else {
    var messageDivs = document.querySelectorAll("div.message");
    messageDivs.forEach(function (message) {
      message.style.display = false;
    });
  }
}, { enableHighAccuracy: "never" });

Type Inference

Type Annotation

Type Annotation

Interfaces

Classes

class Greeter {
  hello(name: string): string {
    return "hi " + name;
  }
  
  bye(name: string): string {
    return "bye " + name;
  }
  
  language = "English";
}

var greeter: Greeter = new Greeter();
var Greeter = (function () {
  function Greeter() {
    this.language = "English";
  }
  Greeter.prototype.hello = function (name) {
    return "hi " + name;
  };
  Greeter.prototype.bye = function (name) {
    return "bye " + name;
  };
  return Greeter;
})();
var greeter = new Greeter();

DefinitelyTyped

interface JQueryStatic {
  getJSON(url: string, success?: (data: any, status: string, xhr: JQueryXHR) => any): JQueryXHR;
  ...
}

interface JQueryXHR extends XMLHttpRequest, JQueryPromise {
  responseJSON?: any;
  then(doneCallback: (data: any, status: string, xhr: JQueryXHR) => void,
       failCallback?: (xhr: JQueryXHR, status: string, error: any) => void): JQueryPromise;
  ...
}

...

declare var $: JQueryStatic;
declare module "jquery" {
  export = $;
}

Optional Typing

ES2015 (ES6)

ES2015 Compatibility

Compilers/polyfills Servers/runtimes
TypeScript +
core-js
51%
Traceur59% Babel +
core-js
71%
Closure30% JSX18% PJS11% Node 0.1217% Node 4.053% Node 5.059%
Desktop browsers Mobile
IE 1116% Edge63% Firefox 4271% Chrome 4663% Safari 954% iOS954% Android 5.129%
(See kangax.github.io/compat-table/es6/ for full details)

ES2015 Backward compatibility

var f = (x, y) => x * y

var {a, b} = {a:1, b:2}

var iterable = [3, 4];
var abc = [1, 2, ...iterable, 5, 6];

for (var i of abc) {
  f(i, 10);
}


let x = 1;
const y = "hi"

var z = `hello ${y}`
var f = function (x, y) { return x * y; };

var _a = { a: 1, b: 2 }, a = _a.a, b = _a.b;

var iterable = [3, 4];
var abc = [1, 2].concat(iterable, [5, 6]);

for (var _i = 0; _i < abc.length; _i++) {
  var i = abc[_i];
  f(i, 10);
}

var x = 1;
var y = "hi";

var z = "hello " + y;

TypeScript Roadmap

(See github.com/Microsoft/TypeScript/wiki/Roadmap for full details)

Downsides

Other options

TypeScript

will finally bring peace
to your troubled soul

Tim Perry
Tech Lead & Open-Source Champion at Softwire