by Jeremy Mason
TypeScript is a programming language
(Full spec available at typescriptlang.org)
Superset of Javascript
(pure JS is valid TypeScript)
Transcompiles into pure Javascript
Offers other language features
Transcompiling to Javascript from existing languages is difficult because they have different design principles
TypeScript is a whole new language designed to harness the implicit power of Javascript
CoffeeScript provides a way to be more expressive
In other words, do more with less code
TypeScript is more verbose but provides type safety
TypeScript is just a language
It's not a framework for building applications
It is possible to build web applications with TypeScript using any existing framework
class Person { FirstName: string; LastName: string; constructor(firstName: string, lastName: string) { this.FirstName = firstName; this.LastName = lastName; } getFullName() { return this.FirstName + ' ' + this.LastName; } } ... var person = new Person('Tom', 'Smith'); console.log(person.getFullName()); // 'Tom Smith'
class Person { private FirstName: string; private LastName: string; constructor(firstName: string, lastName: string) { this.FirstName = firstName; this.LastName = lastName; } getFullName() { return this.FirstName + ' ' + this.LastName; } } var person = new Person('Tom', 'Smith'); // OK console.log(person.getFullName()); // ERROR!! console.log(person.FirstName);
class Logger { static Instance = new Logger(); constructor() { } write(message: string) { console.log(message); } } // Sample usage Logger.Instance.write('Hello, world!');
class Employee extends Person { Title: string; constructor(first: string, last: string, title: string) { super(first, last); this.Title = title; } getFullName() { return super.getFullName() + ', ' + this.Title; } } ... var employee = new Employee('Tom', 'Smith', 'VP of Engineering'); // 'Tom Smith, VP of Engineering' console.log(employee.getFullName());
interface IPerson { FirstName: string; LastName: string; getFullName: () => string; } class Person implements IPerson { FirstName: string; LastName: string; constructor(firstName: string, lastName: string) { this.FirstName = firstName; this.LastName = lastName; } getFullName() { return this.FirstName + ' ' + this.LastName; } }
declare var $: (selector: string) => any; interface JQuerySpin { spin: () => void; } (<JQuerySpin>$('#spinner')).spin();
interface Greeter { sayHello(): void; sayHello(name: string): void; } class ConsoleGreeter implements Greeter { sayHello(name) { if (name) { console.log('Hello, ' + name + '!'); } else { console.log('Hey you!'); } } } var greeter = new ConsoleGreeter(); greeter.sayHello(); // Hey you! greeter.sayHello('Tom'); // Hello, Tom!
var x: string = 'Hello, world!';Function arguments, return values
function sum(x: number, y: number): number { return x + y; }Function types
var sum: (x: number, y: number) => number; sum = function (x, y) { return x + y; }
interface Person { Name: string; } var personsById: { [id: string]: Person; } var person = personsById['user']; console.log(person.Name);
class Person { Name: string; Title: string; constructor(name: string, title: string = 'Mr.') { this.Name = name; this.Title = title; } getFullName() { return this.Title + ' ' + this.Name; } } var person1 = new Person('Tom Smith'); var person2 = new Person('Susie Johnson', 'Ms.'); console.log(person1.getFullName()); // 'Mr. Tom Smith' console.log(person2.getFullName()); // 'Ms. Susie Johnson'
function format(fmt: string, ...args: any[]) { var result = fmt; for (var i = 0; i < args.length; i++) { result = result.replace('{' + i + '}', args[i]); } return result; } // Hello, Susie and Jim console.log(format('Hello, {0} and {1}!', 'Susie', 'Jim'));
var items = [ { Name: 'Susie', Age: 27 }, { Name: 'Jim', Age: 34 } ]; var result = _.find(items, a => a.Name == 'Jim'); // Multiple parameters var sum = (a, b) => a + b; // Multi-line var abs = (a) => { if (a < 0) { return -a; } return a; }
class Model { person: Person; theHardWay() { var that = this; $.ajax(...).done(function (data) { that.person = data; } } theEasyWay() { $.ajax(...).done(data => this.person = data); } }
module App.Views { export class MainView { private model: any; constructor(model) { this.model = model; } render() { ... } } } var model = {}; var view = App.Views.MainView(model); view.render();
module App.Views { class PrivateView { ... } export class PublicView { ... } } // OK var public = new App.Views.PublicView(); // Error! var private = new App.Views.PrivateView();
/// <reference path="../../lib/jquery.d.ts" /> module App.Models { export class MainModel { data: any; init() { // $ is defined in jquery.d.ts $.ajax(...).done(data => this.data = data); } } }
> npm install -g typescript
> tsc app.ts
> tsc -w
> tsc --sourcemap app.ts