Nick Tsitlakidis
Software Developer @ AMD Telecom, Routee team
JavaScript with types and a helpful compiler
var counter:number = 0;
var counterStarted:boolean = (counter != 0);
var counters:Array<string> = [];
counters.push(counter.toString());
counter will be treated as numeric
The good!
The complicated!
The code
function addTwoNumbers(one,two) {
return one+two;
}
var result = addTwoNumbers(3,6); //this will work
var anotherResult = addTwoNumbers("3",6); //returns "36"
function addTwoNumbers(one:number,two:number):number {
return one+two;
}
var result:number = addTwoNumbers(3,6); //this will work
var anotherResult:number = addTwoNumbers("3",6); //your IDE shows an error at compile time
The good!
The complicated!
The code!
interface CoffeeConsumer {
drinkCoffee();
}
class Developer implements CoffeeConsumer {
private name:string;
constructor(name:string) {
this.name = name;
}
public drinkCoffee() {
this.takeABreak();
this.prepareCoffee();
}
private takeABreak() {
console.log("hurray, a break");
}
private prepareCoffee() {
console.log("where is the sugar?");
}
}
var exhaustedDev:CoffeeConsumer = new Developer("Dev");
exhaustedDev.drinkCoffee();
var Developer = (function () {
function Developer(name) {
this.name = name;
}
Developer.prototype.drinkCoffee = function () {
this.takeABreak();
this.prepareCoffee();
};
Developer.prototype.takeABreak = function () {
console.log("hurray, a break");
};
Developer.prototype.prepareCoffee = function () {
console.log("where is the sugar?");
};
return Developer;
}());
var exhaustedDev = new Developer("Dev");
exhaustedDev.drinkCoffee();
interface Person {
name:string;
age:number;
}
var p:Person = {
age:30,
name:"Nick"
};
interface Person {
name:string;
age:number;
}
class User implements Person {
public name:string;
public age:number;
}
var p:Person = new User();
p.age = 30;
p.name = "Nick";
function addPerson(person:{ name:string; age:number; }) {
console.log(person.age);
}
Direct object creation
Inline interface usage
Implemented by a class
@Getter
@Setter
public class ContactDto {
private String id;
private String firstName;
private String lastName;
}
Your API response
$.getJSON("/contacts/345", function(data) {
//do something with the response data
});
Your AJAX call
interface ContactDto {
id:string;
firstName:string;
lastName:string;
}
$.getJSON( "/contacts/345", (data:ContactDto) => {
console.log(data.firstName);
console.log(data.lastName);
console.log(data.id);
});
interface LoDashStatic {
forEach<T>(
collection: T[],
iteratee?: ListIterator<T, any>,
thisArg?: any
): T[];
groupBy<T, TKey>(
collection: List<T>,
iteratee?: ListIterator<T, TKey>,
thisArg?: any
): Dictionary<T[]>;
...
}
declare var _:LoDashStatic
interface Angular {
controller(name:string, controllerClass:Function);
}
declare var angular: Angular;
angular.controller("MyController", MyControllerClass);
npm install -g typings
typings install lodash --save
///<reference path="typings/browser.d.ts"/>
_.forEach([1,2,3],(value)=> {
console.log(value);
});
No compiler errors + code hinting
interface User {
firstName;
}
class UserProfileController {
public user:User;
private httpService:ng.IHttpService;
constructor($http:ng.IHttpService) {
this.httpService = $http;
this.httpService({method: 'GET', url: 'http://my-api.com/users/356'})
.then((user:User)=> {
this.user = user;
});
}
}
angular.module("Profile").controller("UserProfileController",UserProfileController);
No more $scope! (most of the times...)
<div ng-controller="UserProfileController as controller">
<p>{{controller.user.firstName}}</p>
</div>
class UserService {
private httpService:ng.IHttpService;
constructor($http: ng.IHttpService) {
this.httpService = $http;
}
public getUser(userId:string):ng.IPromise<User> {
return this.httpService({
method: 'GET',
url: 'http://my-api.com/users/'+userId
});
}
}
angular.module("Services").service("UserService",UserService);
class UserService {
private restangular:restangular.IService;
constructor(Restangular:restangular.IService) {
this.restangular = Restangular;
}
public getUser(userId:string):ng.IPromise<User> {
return this.restangular.one('users', userId).get();
}
public updateUser(user:User):ng.IPromise<User> {
return this.restangular.one("users", user.id).customPUT(user);
}
}