The Good,
The Bad,
&
The Type Safe
TypeScript is a language for application-scale JavaScript development. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open Source.
JavaScript for C# developers, because untyped languages are scary and J# isn't doing so well
- Me, just now
$(document).on('click', 'a', function (e) {
e.preventDefault();
window.open(this.href);
});
var add = function(x: number, y: number): number {
return x + y;
};
var sum = add(1, 2);
console.log(sum); //3
add('a', 'b'); //TypeScript compiler error
interface Person {
firstName: string;
lastName: string;
dob: Date;
}
var canDrink = function(person: Person): Boolean {
return person.dob.getFullYear() < new Date().getFullYear() - 19;
};
var person = {
firstName: 'Aaron',
lastName: 'Powell',
dob: new Date(27, 04, 1984)
};
if (canDrink(person)) {
console.log(person.firstName + ' grab yourself a beer!');
} else {
console.log(person.firstName + ' baby needs his bottle');
}
Yet another class implementation!
class Person {
constructor(public firstName: string, public lastName: string){
}
greet() {
console.log('Hello, my name is ' + this.firstName + ' ' + this.lastName);
}
marry(partner: Person) {
console.log('Let\'s celebrate the marriage of ' + this.firstName + ' and ' + partner.firstName);
}
}
class Parent extends Person {
constructor(public firstName: string, public lastName: string, public children: Person[]) {
super(firstName, lastName)
}
}
var dad = new Parent('Ian', 'Powell', [new Person('Aaron', 'Powell')]);
dad.greet();
The trinity of constructor functions, prototypes, and instances are more than adequate for solving the problems that classes solve ... The intent ... is not to change those semantics. Instead, it’s to provide a terse and declarative surface for those semantics...
module Collections {
export class LinkedList{
root: any;
constructor() {
this.root = null;
}
add(item: any) {
//add item
}
print() {
//print items
}
}
}
var list = new Collections.LinkedList();
list.add(1);
var sum = function (...numbers: number[]) {
return numbers.reduce(function (x, y) {
return x + y;
});
}
console.log(sum(1, 2, 3, 4, 5));
class DoStuff {
what: string;
stuff() {
$.ajax({
type: 'get',
url: '/foo',
success: data => {
this.what = data;
}
})
}
}
enum Languages {
JavaScript = 0,
TypeScript = 1,
CoffeeScript = 2,
Sweetjs = 3,
Dart = 666
}
.proj
file.csproj
with a post-build event
npm install -g typescript
tsc modules.ts
tsc -sourcemap foo.ts
Apples and Oranges
CoffeeScript hides JavaScript behind a new syntax
TypeScript extends the existing syntax
CoffeeScript doesn't attempt type safety
I hope it stays ES6 compliant
I'm more inclined over CoffeeScript
Debugging needs buy-in from either VS or IE team
Seems to have provided some real benefits already